X label cuts off in logscale

I assume the x numbers are not shown beacause the plot is squeezed too tight. How to fix it?
f = 1:0.01:20000;
db = zeros(length(f),1);
X = [f' db]; %StarStrider colormap
cm = colormap(jet);
X(end,2) = NaN;
c = (X(:,1));
p1 = plot(f,db,'b');
p2 = patch(X(:,1), X(:,2), c, 'EdgeColor','interp','LineWidth',4);
set(gca, 'XScale', 'log')
set(gca, 'FontSize',24)
legend('Frequenzgang einer idealen Box')
xlabel('Frequenz in Hz','Interpreter','latex')
ylabel(' Lautstaerke in dB','Interpreter','latex')
axis([1 20000 -20 20])
grid on

1 Comment

I I want to custom xticks it gives me error and closest match:
/Applications/MATLAB_R2020a.app/toolbox/matlab/graph3d/xticklabels.m

Sign in to comment.

 Accepted Answer

I do not understand what you want for the x-tick labels.
First, run your code with this to see what it does, and then adapt this to do what you want:
set(gca, 'XTick',logspace(0, log10(20000), 5), 'XTickLabel',1:5)
In general, it is necessary to define both the tick locations themselves, and the tick labels.
The labels can also be text (character arrays) or strings as cell arrays. That is sllightly more complicated, so I will not discuss it now, however I can help you with it if that is what you want to do.

10 Comments

Niklas Kurz
Niklas Kurz on 30 Jan 2021
Edited: Niklas Kurz on 30 Jan 2021
The issue consist of Matlabs Auto x-Scale:
I'm just getting the labels, if I'm stretching it out. The solution you presented tackles the core of this Problem, However the log-lines arn't equidistant anymore. Well, It's not that crucial anyhow. Btw. Thank you for your colormap-patch you might have recognized it in my code.
As always, my pleasure!
Yes, I did, actually! Thank you for quoting it!
I am not certain what the remaining problem is.
The problem persists in Matlab scaling malfunction, even if it is quiet neglectable reagarding the facilities of Matlab.
The only trifle with your workaround consisted in the log expression. In order to get evenly displaced lines:
set(gca, 'XTick',logspace(0, log10(10000), 5), ...
'XTickLabel',{'10^0','10^1','10^2','10^3','10^4'})
But that's all to it.
O.K. Thank you! I wanted to be certain that I provided an appropriate and reasonably complete solution.
I know it has been a while, but I just try using and understanding patch. It seems to be cleaner than scatter. Concering your piece of code:
X = [f' db]; %StarStrider colormap
cm = colormap(jet);
X(end,2) = NaN;
c = (X(:,1));
p1 = plot(f,db,'b');
p2 = patch(X(:,1), X(:,2), c, 'EdgeColor','interp','LineWidth',4)
How did you come up with:
X(end,2) = NaN; %?
Otherwise it is shading an area. So where did you know from this line of code will prevent it? I think you explained it in the original comment, where I copied it from, but I don't happen to find it.
I honestly don’t remember, although since NaN values in MATLAB don’t plot (and will cause patch to plot a line rather than an area), it’s a relatively common approach to creating certain effects. If you remember where you got that bit of code and can supply the URL, I’ll look it up to see if I can respond more fully.
For whatever reason, removing that line crashes MATLAB and it also will not work in the online Run feature, so I can’t explore it to see what the result is without it.
There I got it from:
U didn't extend your explenations there, but maybe it'll give u some remembrance.
Thank you!
Wow! That’s three years old, so I’m not surprised that I didn’t remember the details.
In that code ‘X’ is a (51x2) matrix. The last value in the second column (or the last values in the matrix) is set to NaN in order to create the line. (If that line is removed from the code, it plots a filled patch object between the curve and the y-origin (y=0) line.
Do that experiment to see the result:
X = [(0:50)' sin((0:50)*pi/10)']; % Create ‘X’
X(end,:) = NaN; % Set Last Value To ‘NaN’ To Create Line
c = flipud(X(:,1)); % Define Colours To Scale With ‘X(:,1)’
figure
patch(X(:,1), X(:,2), c, 'EdgeColor','interp')
cm = colormap(turbo); % Approximates Spectrum
title('Ending ‘NaN’ Creates A Line')
compared to:
X = [(0:50)' sin((0:50)*pi/10)']; % Create ‘X’
% % X(end,:) = NaN; % Set Last Value To ‘NaN’ To Create Line
c = flipud(X(:,1)); % Define Colours To Scale With ‘X(:,1)’
figure
patch(X(:,1), X(:,2), c, 'EdgeColor','interp')
cm = colormap(turbo); % Approximates Spectrum
title('Full Vector Creates A Filled Patch')
I corrected that code a bit, and bookmarked that URL so that I can refer to it more easily in the future.
Niklas Kurz
Niklas Kurz on 8 Jun 2021
Edited: Niklas Kurz on 8 Jun 2021
Thank you for laying emphasis on that difference. It still is a secret to my why that one line (NaN) causes Matlab to reduce patch to just the plotted line. It seems like the refecrence point is mimiced by the data points this way. What is kind of what we wanted.
As always, my pleasure!
I have long since surrendered to MATLAB being MATLAB and to just hang on and enjoy the experience!

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!