필터 지우기
필터 지우기

How to assign different scales between different ticks in axis?

조회 수: 1 (최근 30일)
Said Kemal
Said Kemal 2023년 4월 15일
편집: VBBV 2023년 4월 15일
I have 2 exponential functions to plot them in the same axis.
t = 0:1:10;
ax = axes;
f1 = exp(0.5 * t);
f2 = exp(t);
plot(ax, t, f1)
hold on
plot(ax, t, f2)
grid on
If I plot it in logarithmic scale in Y axis,
ax.YScale = "log";
then, the figure becomes linear. It is useless for me.
In addition, what I want to do is to see the curve like in first figure, but in different scales between Y-Axis ticks. To do so, I added specific ticks,
ax.YScale = "linear";
ax.YTick = [75, 150, 10000, 22000];
and I got the figure below.
However, what I want to do is arrange the spaces, which is shown in below figure in red arrows, between ticks.
Is there any way to perform this?

답변 (2개)

VBBV
VBBV 2023년 4월 15일
t = linspace(0,10,5);
ax = axes;
f1 = exp(0.5 * t);
f2 = exp(t);
plot(ax, t, f1)
hold on
plot(ax, t, f2)
grid on
ax.YScale = "linear";
ax.YTick = linspace(0,max(f2),5);
ax.YTickLabels = {'0','75', '150', '10000', '22000'};
  댓글 수: 2
Said Kemal
Said Kemal 2023년 4월 15일
Thank you for your response. However, the problem is that the blue line is still not obviously visible. Also, blue line does not represent the true values of its function. Blue line should end about 150.
VBBV
VBBV 2023년 4월 15일
편집: VBBV 2023년 4월 15일
As the values produced by each function vary by a significantly large range, plotting them on same axes would make it difficult to interpret the graphs. Instead you can use subplot to view them clearly where the scales are used seperately.
t = 0:1:10;
f1 = exp(0.5 * t);
f2 = exp(t);
subplot(211)
plot(t, f1);title('f1'); grid on;
subplot(212)
plot(t, f2);title('f2'); grid on;
An alternative way to view them together on same graph is normalize the data obtained by both curves and plot them as you wanted. e.g
figure
plot(t,f1/max(f1),t,f2/max(f2)); title('Combined');grid
Finally, you can also use yyaxis right option for one of the plots which makes y-axes appear on right side of graph

댓글을 달려면 로그인하십시오.


Domenico Filannino
Domenico Filannino 2023년 4월 15일
If you want to plot two curves with very different y values, you can use two different y-axis. Using the same number of division for both y-axis is useful for making a cleaner figure. Hope this will work for you.
t = 0:1:10;
ax = axes;
f1 = exp(0.5 * t);
f2 = exp(t);
Ny = 6;
yyaxis left
plot(ax, t, f1)
ylim([0 150])
ymin = ax.YLim(1);
ymax = ax.YLim(2);
newyticks = linspace(ymin,ymax,Ny);
newYTickLab = strings(1,Ny);
for i = 1 : length(newyticks)
newYTickLab{i,1} = num2str(newyticks(i));
end
ax.YTick = newyticks;
ax.YTickLabel = newYTickLab;
hold on
yyaxis right
plot(ax, t, f2)
ylim([0 22000])
ymin = ax.YLim(1);
ymax = ax.YLim(2);
newyticks = linspace(ymin,ymax,Ny);
newYTickLab = strings(1,Ny);
for i = 1 : length(newyticks)
newYTickLab{i,1} = num2str(newyticks(i));
end
ax.YTick = newyticks;
ax.YTickLabel = newYTickLab;
grid on

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by