Bar plot with two Y-axes

64 Ansichten (letzte 30 Tage)
pietro
pietro am 29 Nov. 2014
Kommentiert: dpb am 14 Nov. 2016
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 Kommentar
dpb
dpb am 29 Nov. 2014
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Orion
Orion am 29 Nov. 2014
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 Kommentare
dpb
dpb am 29 Nov. 2014
2) above w/ zeros rather than NaN...
pietro
pietro am 30 Nov. 2014
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?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

dpb
dpb am 30 Nov. 2014
Bearbeitet: dpb am 30 Nov. 2014
...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 Kommentare
Dirk
Dirk am 14 Nov. 2016
How could you place error bars onto the plot above?
dpb
dpb am 14 Nov. 2016
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...

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Two y-axis finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by