Borrar filtros
Borrar filtros

Bar plot with two Y-axes

47 visualizaciones (últimos 30 días)
pietro
pietro el 29 de Nov. de 2014
Comentada: dpb el 14 de Nov. de 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 comentario
dpb
dpb el 29 de Nov. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Two y-axis en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by