Ticks on second y-axis

13 ビュー (過去 30 日間)
Markus R.
Markus R. 2020 年 5 月 8 日
コメント済み: Alan 2024 年 11 月 15 日
Hey there,
I'm one step away from reaching my wanted result. I have a bar chart and tow y-axis. The problem is that the right y-axis won´t accept my "ytricks" command.
Here is the code:
N=5; % number of bars
y1=[0,289.17; % first data set
0,507.71;
0,775.66;
0,1346.97;
0,1853.58];
y2=[2.36,0; % second data set
3.97,0;
5.52,0;
7.72,0;
8.51,0];
y1(:,1)=nan;
y2(:,2)=nan; % NB: column 2, then column 1 are set NaN for bar chart plotting
x=[1:N].'; % use the serial index to plot against; transpose
z=plotyy(x,y1,x,y2,@bar,@bar); % plot, save axes handles
yyaxis left
set(z(2),'xtick',[]) % turn off labels on RH x-axis; keep only one set
ylabel(['Time [s]']);
yticks(0:200:2000);
yyaxis right
yticks(0:1:10); % not working !
yl = ylabel('Temperature [°C]');
set(yl, 'Color', 'k');
set(z(1),'xticklabel',[0.5,0.75,1,1.5,2]) % tick labels on first...
xlabel(['Factor']);
l = cell(1,2); % legend
l{1}='Time'; l{2}='Temperature';
m = legend(l);
set(m,'FontSize',12);
and here is the result:
Can someone help me scaling the right y-axis (1:1:10)?
Thanks in advance
Markus R.
  3 件のコメント
Markus R.
Markus R. 2020 年 5 月 8 日
Thanks for your answer. It makes sense to me. But how do I plot a bar chart with plot(); I could only finde the comment bar(). Without the "z = plotyy()" function I can't scale the x-axis.
Alan
Alan 2024 年 11 月 15 日
Hi Marcus, you will get your desired result if you replace
set(z(2),'xtick',[])
with
set(z(2),'ytick',[])

サインインしてコメントする。

採用された回答

Mehmed Saad
Mehmed Saad 2020 年 5 月 8 日
Either use plotyy or yyaxis
I am demonstrating how to do it with plotyy
z=plotyy(x,y1,x,y2,@bar,@bar); % plot, save axes handles
% Access axes using set
set(z(1),'YTick',0:200:2000,'xticklabel',[0.5,0.75,1,1.5,2]);
% Access axes properties directly (you can also use set here)
z(1).YLabel.String = 'Time [s]';
% Access axes using set
set(z(2),'YTick',0:1:10);
% Access axes properties directly (you can also use set here)
z(2).YLabel.String = 'Temperature [°C]';
xlabel('Factor');
m=legend('Time','Temperature','Location','northwest');
set(m,'FontSize',12);
  1 件のコメント
Markus R.
Markus R. 2020 年 5 月 8 日
Thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by