Plot with the same Y-axis on both sides

124 vues (au cours des 30 derniers jours)
Max1234
Max1234 le 7 Avr 2023
Modifié(e) : Adam Danz le 14 Juil 2023
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!

Réponse acceptée

Star Strider
Star Strider le 7 Avr 2023
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.
.

Plus de réponses (1)

Adam Danz
Adam Danz le 7 Avr 2023
Modifié(e) : Adam Danz le 14 Juil 2023
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.

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by