Plot with the same Y-axis on both sides

70 ビュー (過去 30 日間)
Max1234
Max1234 2023 年 4 月 7 日
編集済み: Adam Danz 2023 年 7 月 14 日
Hi guys,
I have a graph with a very long x-axis. It would be good if I could show the same Y-axis on both sides. I have not been able to find an answer under yyaxis. There the right axis always has a different scaling. Is there a solution for this at all?
Thank you very much!

採用された回答

Star Strider
Star Strider 2023 年 4 月 7 日
Duplicating the y-axis on the right side is not an option, however writing the tick labels on the right axis definitely is.
Try something like this —
x = 1:10;
y = randn(size(x));
figure
plot(x, y)
grid
Ax = gca;
ytix = Ax.YTick;
ytl = Ax.YTickLabel;
text(ones(size(ytix))*max(xlim)+0.02*diff(xlim), ytix, ytl, 'Horiz','left', 'Vert','middle')
This should adapt to different plots without changing it much. Make appropriate changes to get the result you want.
See the documentation on the text function for details on using it.
.

その他の回答 (1 件)

Adam Danz
Adam Danz 2023 年 4 月 7 日
編集済み: Adam Danz 2023 年 7 月 14 日
You can use yyaxis but you need to link the left and right y rulers so when one changes, the other updates. This comes in handy when zooming or panning or adding data to the axes.
clf
ax = axes;
yyaxis(ax,'right')
yyaxis(ax,'left')
linkprop([ax.XAxis; ax.YAxis],'color')
linkprop([ax.YAxis(1), ax.YAxis(2)],{'Limits','TickValues'});
box(ax,'on')
plot(ax, rand(1,6))
grid(ax,'on')
For notes on this solution, see this answer.

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by