How to assign different scales between different ticks in axis?

3 次查看(过去 30 天)
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 中查找有关 Specifying Target for Graphics Output 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by