Bar plot with two Y-axes

47 次查看(过去 30 天)
pietro
pietro 2014-11-29
评论: dpb 2016-11-14
Hi all,
I need to plot data in a bar plot with two Y axis. Morevover the bar must be grouped, like in the following picture:
I have tried the following code but the result is far to be fine.
figure
a=rand(10,1)*100;
b=rand(10,1)*5;
[AX,H1,H2] =plotyy([1:10],a, [1:10]-0.5,b, 'bar', 'bar');
set(H1,'FaceColor','r')
the problems are:
  • Item one: There are two overlapped x-axis, but I need only one.
  • Item two: the bars are not each next to the other,
Thank you
best regards
  1 个评论
dpb
dpb 2014-11-29
I don't have time to play at the moment but I see two possible modes of attack
1) And probably(?) simplest -- put the data all in one array; scale them to fit on the one axes based on the desired scale for the two. Then put the second axes on manually with its scale for display only.
2) Make the x-axes commensurate; put the same number of bars in each but use NaN for the missing data in the LH/RH bars for the respective RH/LH axes.

请先登录,再进行评论。

采纳的回答

Orion
Orion 2014-11-29
Hi,
try this
figure
a=[rand(10,1)*100 zeros(10,1) ];
b=[zeros(10,1) rand(10,1)*5 ];
[AX,H1,H2] =plotyy([1:10],a, [1:10],b, 'bar', 'bar');
set(H1,'FaceColor','r') % a
set(H2,'FaceColor','b') % b
  3 个评论
dpb
dpb 2014-11-29
2) above w/ zeros rather than NaN...
pietro
pietro 2014-11-30
Using Orion's method I cannot set the legend. Here an example:
figure
a=[rand(10,1)*100 zeros(10,1) ];
b=[zeros(10,1) rand(10,1)*5 ];
[AX,H1,H2] =plotyy([1:10],a, [1:10],b, 'bar', 'bar');
set(H1,'FaceColor','r') % a
set(H2,'FaceColor','b') % b
legend('Absolute','Percentage')
Running this code I get only two red entries in the legend. How may I solve it?

请先登录,再进行评论。

更多回答(1 个)

dpb
dpb 2014-11-30
编辑:dpb 2014-11-30
...I get only two red entries in the legend. How may I solve it?
Use the handles, Luke... :) You've faked which bars are to be seen but have duplicates so have to use the handles associated with the two bars only of the ones that are wanted to be observed. Hn are each 2-vectors; use the first of each --
hLgnd=legend([H1(1) H2(1)],'Absolute','Percentage');
  6 个评论
Dirk
Dirk 2016-11-14
How could you place error bars onto the plot above?
dpb
dpb 2016-11-14
Depends on which release...HG2 broke the HG1 solution...here's a link to the issues... <barweb-in-2014b-doesn-t-work-any-more>
The links in the RH side in this case are useful as well...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Two y-axis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by