フィルターのクリア

Plot same data set on two different y axis scales

46 ビュー (過去 30 日間)
Elizabeth Yeap
Elizabeth Yeap 2020 年 2 月 5 日
コメント済み: Gabriel Felix 2022 年 4 月 6 日
NOTE: Question has been updated.
Hello all,
I want to plot a matrix (37 x 10) represented by sum_ET10 with 2 different y axis scales; km^3/year (original unit) on y axis lhs & mm/year (new unit) on y axis rhs. My code is shown below; I have also included an image below. Currently, my graph is still incorrect. What I want to do is to have a single plot as opposed to the two plots that I'm getting
%% Getting a 37 x 10 matrix of sum_ET10 & sum_area
decade = 10;
sum_area = squeeze(sum(reshape(area,size(area,1),decade,[]),2)/10);
sum_ET10 = squeeze(sum(reshape(ET,size(ET,1),decade,[]),2)/10);
% Plot the results
x_decade = 1:10;
f1 = figure(1);
x1 = axes;
plot(x_decade,sum_ET10(1,:),'-ok');
% Query the YTicks
yt = get(x1,'YTick');
% Create new axis based on existing axis
x2 = copyobj(x1,f1);
% Set new axis transparent
set(x2,'Color','none')
% Remove XTicks
set(x2,'Xtick',[])
% Display the YTicks on the right side
set(x2,'YAxisLocation','right')
% Converting values to mm/year
mm_sum_ET10 = yt/2199000*1000000;
% Update labels
set(x2,'YTickLabel',mm_sum_ET10)
grid on;
xlabel(x1,'Decade');
ylabel(x1,'km^3/year');
ylabel(x1,'mm/year');
untitled.jpg
Any help is appreciated. This is my first time doing different scaled axes, so I'm in need of help.
Thank you.
  1 件のコメント
Adam Danz
Adam Danz 2020 年 2 月 5 日
編集済み: Adam Danz 2020 年 2 月 5 日
Regarding the error, look at the size of sum_area and the size of yt. The ./ notation means that your dividing element i of the first variable by element i of the 2nd variable. That requires both variables having the same number of elements.
Update following your update
You need to copy the axis before plotting the line. But still, this is a bad solution. use yyaxis.

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

採用された回答

Adam Danz
Adam Danz 2020 年 2 月 5 日
編集済み: Adam Danz 2020 年 2 月 5 日
You should be using yyaxis instead of overlaying a 2nd axes which can lead to all sorts of problems.
Demo:
figure()
yyaxis left
plot(rand(1,20)*1000);
yyaxis right
set(gca, 'ytick', 0:10:100, 'ylim', [0,100], 'Ycolor', 'k')
See the link I provided for examples of yyaxis plots.
update
Applying that to your code would look something like this. The important part is to make sure the y limits of both y axes are set so that ylim(axis1) and ylim(axis2) are merely a conversion of each other. See the two lines below with the tag #important where you should make sure you're scaling the axes correctly.
% Plot the results
figure();
yyaxis left
plot(x_decade,sum_ET10(1,:),'-ok');
set(gca, 'YLim', [min(sum_ET10(1,:)), max(sum_ET10(1,:))]) % #important
yyaxis right
% Converting values to mm/year
mm_sum_ET10 = yt/2199000*1000000; % I assume yt is a vector
set(gca,'YTick',mm_sum_ET10, 'YLim', [min(mm_sum_ET10), max(mm_sum_ET10)]) % #important
ylabel('mm/year');
% go back to left axes (main axes)
yyaxis left
grid on;
xlabel('Decade');
ylabel('km^3/year');
  10 件のコメント
juan pedrosa
juan pedrosa 2021 年 3 月 8 日
great answer! thank you.
Gabriel Felix
Gabriel Felix 2022 年 4 月 6 日
Nice! Thanks!

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

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by