Is it possible to plot more than one bar
or barh
on the same axes, using a different BaseValue
for each?
When I try to set the BaseValue
property of the second bar graph, it changes the BaseValue
for the first bar graph as well! See the example below.
A solution using multiple axes is here, but can it be done without the creating additional axes? Specifically I'd like to set the BaseValue
in relation to other data I'm plotting, so overlaying another axes isn't practical since I can't (or I don't know how to?) refer to the coordinates from the original axes.
subplot(1,3,1);
bar(1:10,'BaseValue',5,'FaceColor','b');
hold on; plot([0 11],[5 5],'k-','LineWidth',2);
plot([0 11],[15 15],'k-','LineWidth',2); axis([0 11 0 21]);
subplot(1,3,2);
bar(11:20,'BaseValue',15,'FaceColor','r');
hold on; plot([0 11],[5 5],'k-','LineWidth',2);
plot([0 11],[15 15],'k-','LineWidth',2); axis([0 11 0 21]);
subplot(1,3,3);
bar(1:10,'BaseValue',5,'FaceColor','b'); hold on;
bar(11:20,'BaseValue',15,'FaceColor','r');
plot([0 11],[5 5],'k-','LineWidth',2);
plot([0 11],[15 15],'k-','LineWidth',2);axis([0 11 0 21]);
答案 0 :(得分:2)
这似乎是最近在matlab更新中出现的一个变化,因为它在2013a中表现出所需,但在2014b中没有。这可能是由于切换到HG2图形,似乎是准故意的。 Bar
返回的bar(...)
个对象具有只读的BaseLine
属性。您可以调整Baseline
对象的属性,甚至可以复制它们,但每个轴似乎只有一个(例如,轴对象属性YBaseline
)。
要在新版本中解决此问题,您可以使用'hist'
样式标志,这会导致bar()
创建补丁对象而不是条形图对象:
h(1) = bar(1:10, 'hist', 'b', 'BaseValue',5); hold on;
h(2) = bar(11:20,'hist', 'r', 'BaseValue',15);
由于生成的对象不是Bar
个对象,因此它们不受父轴'YBaseline
属性的影响,但您也不能像{{1}那样以标准方式更改属性对象。
(这是Matlab R2014b中的状态;我不确定R2015a可能带来的变化......)