Comparing 2 histograms in a barh-plot

Hey,
I want to compare 2 histograms in a barh plot. Taking an arbitrary 'VaseValue', I want to plot one histogram to the left, and one to the right.
Doing this, MatLab starts grouping the histogram data within the same bin, which reduces the binwith, which I do not like. I mean it makes sence when both histogram data show into the same direction, but when one is plotted to the left and one to the right, its just a waste.
For both bin values of the same bin to be at the exact same position, I used 'stacked'. This works fine until the basevalue is larger than the highest value that should be plotted in to the left direction... How to I fix that??
Example:
This code does what I want it to look like:
x = [1 2];
y = 3+[ 5 5;-5 -5]';
barh(x,y,'stacked','BaseValue',3)
When I do not want the barh to appear at 3, but for example at 5:
x = [1 2];
y = 5+[ 5 5;-5 -5]';
barh(x,y,'stacked','BaseValue',5)
I doenst work. It does not show the barh into negative x direction, which should go from 5 to 0.
Checking the plot, it actually tells me that X and Y data for the first plot its:
BaseValue: 5
XData: [1 2]
YData: [10 10]
and for the second one its:
BaseValue: 5
XData: [1 2]
YData: [0 0]
So the bars should be there, but thery arent....
Any idea how to fix that??
Many Thanks in advance,
Best regards

回答(1 个)

hi
seems to me the way you build your y data matters
y = 5+[ 5 5;-5 -5]'; NO
y = 5+[ -5 -5;5 5]'; YES
x = [1 2];
y = 5+[ -5 -5;5 5]';
barh(x,y,'stacked','BaseValue',5)

5 个评论

No, since when you try it with a basevalue of 6 it's again not doing what it should.
I don't think barh is wrong, it's the way we use it which is incorrect
with your data y = 5+[ -5 -5;5 5]' , so y goes from 0 to 10 and if we ask basevalue = 6 then barh understand we want to plot y from 0 to 6 in one color and from 6 to 10 in the other color and this is exactly what we get :
x = [1 2];
y = 5+[ -5 -5;5 5]';
barh(x,y,'stacked','BaseValue',6)
this works too
x = [1 2];
y = 3+[ -5 -5;5 5]';
barh(x,y,'stacked','BaseValue',6)
I think i missed one thing, BaseValue and the offset on y have to be the same value
It only shows your results for offset<BaseValue, for offset>=BaseValue, the Value into Positive direction is plottet on top of the already existing negative direction bar plot, making it unvisible. One could then start playing with alpha channels, but it seems like a lot of work for something as easy as the desired result
I think i missed one thing, BaseValue and the offset on y have to be the same value
Correct

请先登录,再进行评论。

类别

产品

版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by