Matlab条形图已分组但以不同的y比例 [英] Matlab bar plot grouped but in different y scales
问题描述
我有两组数据,我想使用条形图进行绘制.但是问题在于这两套数据的规模完全不同.如果我只使用bar(A)
,它将看起来像这样:已分组,但由于比例尺,几乎看不到第二个数据集.
I have two sets of data, and I want to plot using bar graph. But the problem is these two sets of data are at quite different scale. If I just use the bar(A)
, it would look like this: grouped but the second data set is barely visible because the scale.
但是,如果使用plotyy(x,y1,x,y2)
,则绘图将如下所示:两组数据的比例不同,但条形图未分组,第二组数据与第一组重叠.
However, if I use the plotyy(x,y1,x,y2)
, the plot will be like this: two sets of data are in different scale, but the bar graphs are not grouped, the second data sets overlaps to the first.
所以我想知道是否有一种方法可以绘制像第一个图一样分组的条形图,但是两个数据集使用的是单独的y比例尺?或者有没有办法在第二个图中调整条形图的水平偏移,使其看起来像分组".
So I am wondering if there is a way to plot the bar graph grouped like the first figure, but the two datasets are using separate y scales? Or is there a way to adjust the horizontal offset of the bar graph in second plot so it looks like "grouped".
谢谢!
推荐答案
这使用plotyy
:
This uses the plotyy(x1,y1,x2,y2,fun1,fun2)
variant of plotyy
:
%// Set these three variables as desired
offset = (x(2)-x(1))/8;
width = (x(2)-x(1))/4;
colors = {'b','g'};
%// Do the plot
plotyy(x-offset,y1,x+offset,y2, @(x,y) bar(x,y,width,colors{1}), @(x,y) bar(x,y,width,colors{2}));
如果您希望x标记仅出现在使用过的x值上:
If you prefer x-ticks to appear only on used x values:
h = plotyy(x-offset,y1,x+offset,y2, @(x,y) bar(x,y,width,colors{1}), @(x,y) bar(x,y,width,colors{2}));
set(h,'xtick',x)
这篇关于Matlab条形图已分组但以不同的y比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!