Matlab chart bar is grouped, but at different scales

I have two datasets, and I want to plot a graph using a histogram. But the problem is that these two data sets are on a completely different scale. If I just use bar(A) , it will look like this: grouped, but the second data set is barely noticeable, because the scale.

enter image description here

However, if I use plotyy(x,y1,x,y2) , the graph will be like this: the two data sets have different scales, but the histograms are not grouped, and the second data set overlaps with the first.

enter image description here

So, I am wondering if there is a way to plot a graph grouped as the first digit, but the two datasets use separate y scales? Or is there a way to adjust the horizontal offset of the histogram in the second chart so that it looks like a "grouped" one.

Thanks!

+4
source share
1 answer

The plotyy(x1,y1,x2,y2,fun1,fun2) 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})); 

enter image description here

If you prefer x-ticks to display 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) 
+8
source

Source: https://habr.com/ru/post/1501200/


All Articles