loglog plot, change yticks to non 10^ values. IS there a straight forward way like for xticks

5 views (last 30 days)
Hi there
Is there an straight forward way to have the yticks on the loglog plot in terms of 100 instead of 10^2?
I have got it to work, from seeing answers to previous questions posed to the community, but I'm confused as to why the yticks are so much more intricate than the xtick.
Thanks as always
Gerard
z = [2 4 6 10 15 20 30];
q = 2:2000;
V_h = zeros(length(z), length(q));
for ii =1:length(z)
V_h(ii,:) = (q*3.6)/(4*z(ii));
end
loglog(q,V_h)
grid
xlim([2 2000])
ylim([0.1 1000])
% Option 1
% Works for xtick, not ytick
set(gca, 'XTick', [0.1 1 10 100 1000 2000])
set(gca, 'YTick', [0.1 1 10 100 1000])
% Option 2
% Works for both xtick and ytick
set(gca, 'XTick', [0.1 1 10 100 1000 2000])
yticks = get(gca,'YTick');
set(gca,'YTickLabel',yticks);
% Option 3
% Works for both xtick and ytick
xticks([0.1 1 10 100 1000 2000])
yticks = get(gca,'YTick');
set(gca,'YTickLabel',yticks);
% Option 4
% Works only for xtick, not ytick
% xticks([0.1 1 10 100 1000 2000])
% yticks([0.1 1 10 100 1000])

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 24 Aug 2023
Edited: Dyuman Joshi on 24 Aug 2023
"... but I'm confused as to why the yticks are so much more intricate than the xtick."
I suspect it is becase the values to x-ticks are not incrementally ordered on the log scale whereas the values to y-ticks are; if tick values to loglog plot are incrementally ordered, then the tick labels will be displayed in exponential format.
(I have not found any such information in the documentation yet, for reference, this is just what I think how it works currently. I'll attach a reference if and when I find it)
That's why the methods that work change the ticklabels to display the values in the default (for the lack of a better term) format.
Let's add a value to make the y-ticks exponentially non-linear and see the output -
z = [2 4 6 10 15 20 30];
q = 2:2000;
V_h = zeros(length(z), length(q));
for ii =1:length(z)
V_h(ii,:) = (q*3.6)/(4*z(ii));
end
loglog(q,V_h)
grid
xlim([2 2000])
ylim([0.1 1000])
set(gca, 'XTick', [0.1 1 10 100 1000 2000])
%Intermediate value added to y-ticks i.e. 500
set(gca, 'YTick', [0.1 1 10 100 500 1000])
As you can see, the y-ticks are now displayed in the default format.
Edit - Changing the exponent value of tick labels will not work in this case - "If the axis has a log scale, then the Exponent property has no effect."
  2 Comments
Gerard Nagle
Gerard Nagle on 24 Aug 2023
Moved: Voss on 24 Aug 2023
Hi Joshi,
Thanks for the answer, it does seem to be a functionality, that I ddint expect to be different, or need to have an intermediate/extra value to get it to work. I did play around a bit more, using
ax = gca;
ax.XTick
ax.XTickLabel
ax.YTick
ax.YTickLabel
and they sem to give a very suttle difference in the output. Notice how the ax.XTickLabel and the ax.YTickLabel are different.
YTickLabel seems to have a cell within a cell.
If I use your solution, with the 500, it works; Please see below.
clear; clc;
z = [2 4 6 10 15 20 30];
q = 2:2000;
V_h = zeros(length(z), length(q));
for ii =1:length(z)
V_h(ii,:) = (q*3.6)/(4*z(ii));
end
loglog(q,V_h)
grid
xlim([2 2000])
ylim([0.1 1000])
% Works only for xtick, not ytick
xticks([0.1 1 10 100 1000 2000])
% ax.YAxis.TickValues =
yticks([0.1 1 10 100 1000])
ax = gca;
ax.XTick
ans = 1×6
1.0e+03 * 0.0001 0.0010 0.0100 0.1000 1.0000 2.0000
ax.XTickLabel
ans = 4×1 cell array
{'10' } {'100' } {'1000'} {'2000'}
ax.YTick
ans = 1×5
1.0e+03 * 0.0001 0.0010 0.0100 0.1000 1.0000
ax.YTickLabel
ans = 5×1 cell array
{'10^{-1}'} {'10^{0}' } {'10^{1}' } {'10^{2}' } {'10^{3}' }
% Works only for xtick, and ytick
xticks([0.1 1 10 100 1000 2000])
% ax.YAxis.TickValues =
yticks([0.1 1 10 100 500 1000])
ax = gca;
ax.XTick
ans = 1×6
1.0e+03 * 0.0001 0.0010 0.0100 0.1000 1.0000 2.0000
ax.XTickLabel
ans = 4×1 cell array
{'10' } {'100' } {'1000'} {'2000'}
ax.YTick
ans = 1×6
1.0e+03 * 0.0001 0.0010 0.0100 0.1000 0.5000 1.0000
ax.YTickLabel
ans = 6×1 cell array
{'0.1' } {'1' } {'10' } {'100' } {'500' } {'1000'}
Dyuman Joshi
Dyuman Joshi on 25 Aug 2023
"... it does seem to be a functionality, that I ddint expect to be different ... "
Neither did I, but it seems the working of ticklabels is different when an axis has a log scale.
Feels like an anamoly. And we don't know why is that.
I'll report this to TMW.

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!