Matlab bar plot grouped but in different y scales

允我心安 提交于 2019-12-18 17:50:10

问题


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.

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.

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".

Thanks!


回答1:


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}));

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)


来源:https://stackoverflow.com/questions/18688381/matlab-bar-plot-grouped-but-in-different-y-scales

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!