Problem with plotting two x-Axes and y-Axes

조회 수: 1 (최근 30일)
BN
BN 2020년 4월 29일
댓글: Adam Danz 2020년 4월 29일
Dear all,
Here is my plot:
I want to convert it to something like this picture below that I found on google:
Here is my code: I want to plot, plot() and area() as regular and plot bar() like the picture above (inverse)
h = plot(t);
set(h,'linewidth',1);
set(gca,'xtick',1:12,...
'xticklabel',{'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'});
newcolors = ['#77AC30'; '#0072BD';'#D95319';'#7E2F8E';'#EDB120';'#4DBEEE';'#A2142F'];
colororder(newcolors);
set(0,'DefaultLegendAutoUpdate','off');
box on
grid on
xlim([1 12]);
ylim([0 70]);
xticks(1:12);
hold on
hh = area(Obs,'LineStyle','none');
hh.FaceAlpha = 0.4;
hh.FaceColor = [0 0 0];
set(gca, 'YGrid', 'off', 'XGrid', 'off')
%VVVVV % HERE is second plot % VVVV
hold on % plot bar chart
cc = bar(t);
I tried:
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
But it isn't accomplished well.
Thank you all.
  댓글 수: 2
Tommy
Tommy 2020년 4월 29일
Rather than setting 'XAxisLocation' to 'top', what happens if you set 'YDir' to 'reverse'?
BN
BN 2020년 4월 29일
편집: BN 2020년 4월 29일
Dear Tommy,
When I used:
hold on
ax1 = gca; % current axes
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,'YDir','reverse','YAxisLocation','right',...
'Color','none');
cc = bar(t);
Here is the reults:

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2020년 4월 29일
Use yyaxis to plot the plot() and area() on the left axis and the bar() on the right axis. Then flip the direction of the right axis as Tommy recommended. It will look something like this.
yyaxis left
hold on
plot(. . .)
area(. . .)
yyaxis right
hold on
bar(. . .);
set(gca, 'YDir', 'reverse')
If you need any help implementing that, show me what you've got and I can help straighten it out.
  댓글 수: 2
BN
BN 2020년 4월 29일
Thank you so much,
Adam Danz
Adam Danz 2020년 4월 29일
Bingo! Looks good.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by