Plot with the same Y-axis on both sides

69 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by