How can I compare the scale of two y axis in a subplot and adjust them to each other?

12 ビュー (過去 30 日間)
Hello MatLab-Pros,
I have the following code:
hAxes(1) = subplot(4,1,1);
yyaxis left
plot(x, y1)
ScaleR = get(gca, 'YTickLabel');
yyaxis right
plot(x, y2)
if strncmp(fname1, fname2, 5) == 1
set(gca, 'YTickLabel', ScaleR)
end
I should change the scale of the right axis to the scale of the left axis if fname1 and fname2 are the same (which they are for sure), but it doesnt do anything. Where am I wrong here? And is it possible to do this both-ways? Meaning, matlab should compare the two ranges of the axis and set the larger one for both sides.
Thanks in advance.
Mike
EDIT: With the changes that "Walter Roberson" suggested my code looks like this:
hAxes(1) = subplot(4,1,1);
yyaxis left
yaxL = gca;
plot(yaxL, x, y1);
yyaxis right
yaxR = gca;
plot(yaxR, x, y2)
if strncmp(fname1, fname2, 5) == 1
YLL = yaxL.YLim;
YLR = yaxR.YLim;
YLC = [min(YLL(1), YLR(1)), max(YLL(2), YLR(2))];
yaxL.YLim = YLC;
yaxR.YLim = YLC;
yaxR.YTick = yaxL.YTick;
end
The Problem is the output looks like this: (First graph is with the selfmade adjustments to the scale, second one is default matlab scaling)
  4 件のコメント
Michael Steinbuechler
Michael Steinbuechler 2017 年 6 月 20 日
Where can I assign handles to the different axis? yyaxis has no output and on plot I get the error: There is no YLim property on the Line class.
Jan
Jan 2017 年 6 月 20 日
Please post your code such that we can suggest an improvement.

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

採用された回答

Walter Roberson
Walter Roberson 2017 年 6 月 20 日
get(gca, 'YLim')
for each of the two axes, recording the values. Take min of the first component and max of the second component; set the YLim to the result for both axes.
  9 件のコメント
Michael Steinbuechler
Michael Steinbuechler 2017 年 6 月 20 日
I did this, but things are getting wierd now. Thats my new output:
If I check the variables I get this numbers:
YLL [1.8000 2.4000] YLR [1.5000 2.5000] YLC [1.5000 2.5000]
It seems the command to assign the new range to the axis does not work (but I dont get an error or anything)
hAxes(1) = subplot(4,1,1);
yyaxis left
plot(x, y1)
yyaxis right
plot(x, y2)
if strncmp(fname1, fname2, 5) == 1
YLL = hAxes(1).YAxis(1).Limits;
YLR = hAxes(1).YAxis(2).Limits;
YLC = [min(YLL(1), YLR(1)), max(YLL(2), YLR(2))];
hAxes(1).YAxis(1).Limits = YLC;
hAxes(1).YAxis(2).Limits = YLC;
end
Michael Steinbuechler
Michael Steinbuechler 2017 年 6 月 20 日
Okay I finally found a way that works as well. I can find the minimum and maximum value from y1 and y2 with the "min" and "max" commands and than just do it this way:
yyaxis left
plot(tData, x1a)
ylim([min1 max1])
yyaxis right
plot(tData, x1b)
ylim([min1 max1])

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

その他の回答 (1 件)

Jan
Jan 2017 年 6 月 20 日
  1 件のコメント
Michael Steinbuechler
Michael Steinbuechler 2017 年 6 月 20 日
I already saw this and it doesnt really help. Acoording to this site, it should work how I did it.

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

カテゴリ

Help Center および File ExchangeGraphics Object Properties についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by