2

Let's say we have 3 datasets of the same size, that we want to plot on the same histogram:

Dataset_1 = [1 1 1 1 1 1 2 2 2 2 3 3];
Dataset_2 = [1 1 1 2 2 2 2 2 2 3 3 3];
Dataset_3 = [1 1 2 2 2 2 3 3 3 3 3 3];

RGB = orderedcolors("gem");
H = rgb2hex(RGB);

figure
h1 = histogram(Dataset_1, 'FaceColor', RGB(1,:),'FaceAlpha',0.8);
hold on;
h2 = histogram(Dataset_2, 'FaceColor', RGB(2,:),'FaceAlpha',0.8);
h3 = histogram(Dataset_3, 'FaceColor', RGB(5,:),'FaceAlpha',0.8);

The output would be the following:

enter image description here

The leftmost bar is "prettier", since the blue bar (Dataset_1, highest value in the first bin) is plotted first, followed by the orange bar (Dataset_2, second highest value in the first bin) and then the green bar (Dataset_3, lowest value in the first bin).

I don't like how the other bars are displayed though, i.e., when the plotting order does not match the bars values.

I tried playing around with the FaceAlpha property but it does not give anything good.

What I want to have, ultimately, is a histogram in which, for each bin, the plot order is defined by the height of the bar for each dataset. First, plot the highest bar, etc. This seems impossible as the plot order is defined by the call order.

Question: Is there a magic trick in your sleeve for doing that?

6
  • 1
    Can you sort your values before plotting? Otherwise you might be able to calculate the histogram "by hand" before plotting and then plotting it in the desired order using bar() Commented Nov 24 at 9:54
  • 1
    It is possible, but there is no "magic" (simple) trick that, it's going to be quite convoluted. Besides that, this is a terrible way to visually present several histograms on a same graph (not saying that as a matter of personal preference but from experience). Especially if it involves more than 1 graph. You could spend a lot of effort to make one of them look "good"-ish and realise at the next set of data that you have to change most of your approach as it now looks rubbish on the next data set. I recommend you to look at other ways to overlap histograms. Commented Nov 24 at 12:04
  • 1
    For inspiration, have a read at another question and the few answers: How to better plot and compare overlapping histograms? Commented Nov 24 at 12:05
  • 1
    I have to agree with Hoki here, you should look for a better way to present your data. Separate the concept of the histogram and the visual representation of bars that is typically associated with histograms. A histogram can be plotted in many different ways. I like dot plots when there are few categories: [hist1,edges1] = histcounts(Dataset_1); bin1 = (edges1(1:end-1) + edges1(2:end))/2; scatter(hist1, bin1); xlim([0,7]); ylim([0.5,3.5]); yticks(1:3); xlabel("count"); ylabel("value");. Commented Nov 24 at 15:02
  • 1
    Nice things about dot plots: you can overlay many of them on the same axes, and you don't need to include the origin like you need to do with bars. Also, if the categories are along the y axis, you can read the labels without tilting your head. Commented Nov 24 at 15:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.