필터 지우기
필터 지우기

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 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