How to assign different scales between different ticks in axis?
3 views (last 30 days)
Show older comments
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1356998/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1357003/image.png)
However, what I want to do is arrange the spaces, which is shown in below figure in red arrows, between ticks.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1357008/image.png)
Is there any way to perform this?
0 Comments
Answers (2)
VBBV
on 15 Apr 2023
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 Comments
VBBV
on 15 Apr 2023
Edited: VBBV
on 15 Apr 2023
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
on 15 Apr 2023
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
0 Comments
See Also
Categories
Find more on Specifying Target for Graphics Output in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!