How can I combine two plots in one bar chart with their unique y-axis?

3 次查看(过去 30 天)
Hi all,
I am plotting data from two columns in one bar plot, but the results are not showing as I expected them. I get this result by using the following code:
y = [lr_flights2018, lr_income2018];
yyaxis left
b = bar(1:length(y),lr_flights2018);
ylabel('Life Rating/flights ratio')
yyaxis right
p = bar(1:length(y),lr_income2018);
ylabel('Life Rating/income ratio')
set(gca, 'XTick', 1:length(y))
set(gca,'XTickLabel',{Combined{:,1}})
xtickangle(90)
title('Correlations with life rating');
Which shows as:
What other output I want is
  • Bar plots next to each other for each bin
If anyone knows how I can do this, it will be much appreciated. Thanks!
Edit: the data and .m file are added
Edit2: I managed to remove the black mess
Edit3: the problem occures becuase of the combination of two seperate axis that split the bars. Combined, I know that two plots can be shown with
Y = [5,2
8,7
9,8
5,5
4,3];
figure
bar(Y)
  2 个评论
Adam Danz
Adam Danz 2019-11-8
Looks like you solved some of the problem based on your edits. Where are you at now and how can we help?
An unrelated suggestion: add some transparency to your right-axis bars so the left-axis blue bars can be seen.
Pratheek Punchathody
Hi
As per my understanding it is required to plot the two different columns as bar graphs using the slip Y axis. The bar should appear adjacent to each other for the X axis data.
I have considered two different vectors as the sample data. The code I have executed is as shown below. This will plot the bar adjacent to each other corresponding to the Y axis.
%% Code starts here
yDataRight=[0.8 0.1 0.8 0.6 0.7 0.3 0.2 0.9 0.2 0.5];
yDataLeft=[5 4 1 3 4 5 1 8 4 6];
a=[yDataLeft(:) zeros(10,1)];
b=[zeros(10,1) yDataRight(:)];
yyaxis left
bar(a,'BarWidth',0.5);
ylabel('Life Rating/flights ratio')
yyaxis right
bar(b,'BarWidth',0.5);
ylabel('Life Rating/income ratio');
%%Code ends here
The below diagram shows the output figure for the above code.
As per my understanding one of the ways to plot the bar adjacent to each other is when the data is in the matrix form.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Bar Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by