Effacer les filtres
Effacer les filtres

Bar plot with two Y-axes

47 vues (au cours des 30 derniers jours)
pietro
pietro le 29 Nov 2014
Commenté : dpb le 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 commentaire
dpb
dpb le 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.

Connectez-vous pour commenter.

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 30 Nov 2014
Modifié(e) : dpb le 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 commentaires
Dirk
Dirk le 14 Nov 2016
How could you place error bars onto the plot above?
dpb
dpb le 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...

Connectez-vous pour commenter.

Catégories

En savoir plus sur Two y-axis dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by