I am plotting x-y plot using plot function. I want to set only y-axis as log scale and x-axis as linear? How to do that? I used loglog function but it scales both axis but I want only Y.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 22 Sep. 2023
Bearbeitet: MathWorks Support Team am 22 Sep. 2023

58 Stimmen

The best way to create that type of axes is to use the semilogy function. Alternatively, you can set the ‘YScale’ property on the axes:
set(gca, 'YScale', 'log')
***Update from Mathworks Support Team - September 2023***
As of R2023b, you can also use the 'yscale ' function. 

23 Kommentare

Nick Durkee
Nick Durkee am 14 Jun. 2017
Note that this works for 3D plots as well. Not sure if there's an equivalence of semilogx for 3d.
set(gca, 'XScale', 'log')
Mahender Aroori
Mahender Aroori am 11 Jan. 2019
thank you
Elkin Javier Cepeda Ramirez
Verschoben: Adam Danz am 22 Jun. 2023
thank you , you help me too much
M S Rashed
M S Rashed am 20 Mai 2019
Is there any way to convert from log-scale back to linera scale?
set(gca, 'XScale', 'linear')
Hazem Mubarak
Hazem Mubarak am 30 Dez. 2019
Verschoben: Adam Danz am 22 Jun. 2023
Thanks ;)
In-chan Kim
In-chan Kim am 30 Apr. 2020
Is there a way to make set(gca, 'YScale', 'log') show negative values as well?
Walter Roberson
Walter Roberson am 30 Apr. 2020
The log of negative real values is equal to the log of the positive value, plus (pi*1i) -- that is the result is complex. When you set YScale to log, then internally it would take the log of the negative values you have, and would found that the log is complex.
If, hypothetically, it were able to display with complex coordinates, where would you want the values displayed? For example do you want the complex portion of the coordinate to be treated as time, and the display should automatically cycle through a time period (perhaps every 2*pi seconds) with the negative values being displayed for one frame on each occasion that the complex value projected down to real?
If you want negative values to be displayed where the corresponding positive value would be displayed, then plot with abs(y) instead of y.
Remember, when you use log, there is an infinite distance in log scale between y = 1 and y = 0, since it has to pass through y = exp(-1), y = exp(-2), y = exp(-3), and so on, each of which needs to be allocated the same screen distance as between y = exp(0) and y = exp(1) . To get to negative y, you would have to go "further than infinity" down the bottom of the plot.
Brad Norton
Brad Norton am 13 Sep. 2020
what is gca?
Walter Roberson
Walter Roberson am 13 Sep. 2020
ax = gca returns the current axes or chart for the current figure, which is typically the last one created or clicked with the mouse.
samane pahlevani
samane pahlevani am 26 Jun. 2021
Hi . I want to plot y-axis scale as log10. how can I do that? I tried your answer but log in matlab is based on 'e' not 10!
Walter Roberson
Walter Roberson am 26 Jun. 2021
How can you tell whether the y-axes scale is log10 or natural log?
The labeling is always done based on log10, no matter whether the underlying graph is log10 or log e.
Suppose the calculation were done log B for some base B. Then log[B](x) = log[e](x) * log[e](B) which is a constant multiple relative to log[e] . The constant multiple would alter how much height the graph would need, but as a magnification, not as a change to the shape of the graph. And the size available to plot into is fixed, so MATLAB is just going to rescale anyhow...
samane pahlevani
samane pahlevani am 30 Jun. 2021
I'm really grateful for your help. I completely figured out your answer. Just about the formula which you wrote, I suppose that the accurate form would be: log[B](x) = log[e](x) / log[e](B)
Walter Roberson
Walter Roberson am 1 Jul. 2021
You are correct, it should have been division not multiplication.
Samuel Pulpan
Samuel Pulpan am 15 Jan. 2022
Verschoben: Adam Danz am 22 Jun. 2023
Thanks
Weirong Sun
Weirong Sun am 9 Mai 2022
good answer! It's very helpful!!!
mingyang zhao
mingyang zhao am 2 Aug. 2023
Great answer! Thank you!
Mr Thadi
Mr Thadi am 3 Jul. 2024
can you please tell me what is significance of 'gca'?how it helps
Walter Roberson
Walter Roberson am 3 Jul. 2024
When you use set(), you need to indicate which object you want to set parameters for. We assume that you want to set parameters for the current axes. The short form of indicating the current axes is to use gca
Mr Thadi
Mr Thadi am 4 Jul. 2024
Thank you Mr walter,
i have attached one image,
in that my data file having values from 100 to 14000.
if i plot histogram without x axis set properties it showing like figure 'A', in that bins starts from 0-300,300-600...etc like that.
but if i use set (gca,'xscale','log') command to the x scale it showing initial bin from 300-600,600-900....etc.you can see in figure 'B'
my question is what about values in between 0-300 from figure 'B' , why these are not appearing how to solve it,if i want to get xscale in log values histogram initial bin from 0-300,300-600...etc are needed to change properties in that to command?can you please explain me
i can provide my data file also to you
This issue arises because the first bin edge is at x=0 and log(0)=-inf which cannot be represented graphically.
Assuming there are no data less than or equal to 0 and no bin edges less than 0, you could set the first bin edge according to the smallest value in the data.
x = rand(1,1000)*10000;
minPositiveValue = min(x(x>0),[],'all');
minbin = 10^floor(log10(minPositiveValue));
ax = axes();
h = histogram(ax, x);
ax.XScale = 'log';
if h.BinEdges(1) == 0
h.BinEdges(1) = minbin;
end
Mr Thadi
Mr Thadi am 29 Sep. 2024
Thank you Mr Adam Danz

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Toshia M
Toshia M am 20 Sep. 2023

6 Stimmen

Starting in R2023b, you can change the scale of any axis after you create the plot by calling the xscale, yscale, or zscale function.
For example, create a plot of two vectors x and y. Then set the scale of the y-axis to logarithmic.
x = 1:100;
y = x.^2;
plot(x,y)
grid on
yscale log
Rohit Sinha
Rohit Sinha am 27 Apr. 2022

4 Stimmen

The easiest way to do this is simply use the following command instead of plot
semilogy(x,y);
This will plot x axis on a linear scale and y axis on a log scale. Similarly, if you want to plot x axis on log scale and y axis on a linear scale, you can use
semilogx(x,y) ;

2 Kommentare

Walter Roberson
Walter Roberson am 27 Apr. 2022
semilogy() is the first thing I mentioned in my answer in 2016.
Nicholas Santiago
Nicholas Santiago am 4 Nov. 2022
yo i totally missed that I generally only read the bold stuff, thanks a ton!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Line Plots finden Sie in Hilfe-Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by