How to Adjust y axis plot range in Matlab? - matlab

I need to plot the following functions in matlab
y1=sign(x)
y2=tanh(x)
y3=(x)/(x+1)
The x-range is -5,5 with 0.1 spacing
The y-plot range should be between -1.5 to 1.5.
Each plot should have a labeled x and y axis and a legend in the lower right corner.
The only things I cant figure out is how to adjust the y plot range. Ive tried editing the actual figure but all that seems to do is distort the graph.
Is there a command within matlab that will let me adjust the y axis plot range?
The other thing I havent figured out yet is adding a legend, I can do it after the figure is created but I guess it needs to be done by matlab command.

Yes, use axis after the plot command:
axis([-5 5 -1.5 1.5])

If you only want to set the y-range without setting the x-range you can use
ylim([-1.5 1.5])
or alternatively axis([-inf inf -1.5 1.5]). I found this from the original MATLAB-source: https://de.mathworks.com/help/matlab/ref/ylim.html
PS: For trigonometric functions I would recommend to use axis equal to have equally spaced x and y-axis (see MATLAB)

Related

Set y axis max value with Matlab

I'm trying to draw plots with Matlab and the problem is that i want to fix the maximum value of y-axis to 8 . To help you understand me, look at this first example :
you can see that the maximum y value is 8. but when i try to draw this graph :
its maximum y value is 6 . i want to fix it for all examples to 8.
how can i do it?
here's my code for now :
data=importdata('C:/Users/Eden/Desktop/Calcul_accel/fichier_final.txt');
fig = figure(1);
x=data(:,2)
y=data(:,3)
p=plot(x,y)
set(p,'Color','red');
xlabel('Time(milliseconds)','FontSize',12,'FontWeight','bold','Color','b');
ylabel('Acceleration(g unit)','FontSize',12,'FontWeight','bold','Color','b')
thank you very much
Use ylim if you just want to modify the y axis.
Therefore, do this once your plot is already open:
ylim([0 8]);
This overrides the auto-scaling of the axes so that y always spans between 0 to 8.
In general, #eigenchris mentioned to use axis, which allows you to change the dynamic range of what is viewable in a plot for both the x and y axes. However, since you only want to change how the y-axis is visualized, one call to ylim is enough.

Matlab line plot overlaps axis when values are zero

I have plotted a figure with multiple lines on it, and I have noticed that the lines for the plot overlap the x-axis when they are zero. Is there a way that I can essentially get the x-axis to plot on the top, rather than the lines?
Here is a MWE that does the same thing (I haven't put my exact code up as my dataset is quite big).
xdata=1:1:10;
ydata=[1;0.8;0.6;0.4;0.2;0;0;0;0;0];
line(xdata,ydata)
After I plot the lines (multiple per plot in my case), I do various other things with the axes so I get what I need (including adding a secondary set of axes). None of this seems to make any difference as to whether the x-axis is plotted on top of the lines or not.
I did have a search online but couldn't find anything to do with this.
The answer given by Luis is a nice workaround, but the official way to solve this problem is to use the layer property of the axis object, see the manual. To plot the axis on top of the data you do
set(gca,'Layer','top')
To automatically do this for all your plots, you can put the following line in your startup.m:
set(0,'DefaultAxesLayer','top')
This kind of answers you do not make up yourself, I only discovered this trick after asking more or less the same question on comp.soft-sys.matlab many years ago. See also this SO question.
After having plotted all your lines, plot a line on the x axis with the same color as the axis:
hold on
a = axis; %// gives xmin xmax ymin ymax
cx = get(gca,'Xcolor'); %// color of x axis
plot([a(1) a(2)], [a(3) a(3)], 'color', cx)
If the lines also overlap with the y axis and you also want that axis to appear on top, add the following:
cy = get(gca,'Ycolor'); %// color of y axis
plot([a(1) a(1)], [a(3) a(4)], 'color', cy)

Finding the limits of automatically-adjusted axes in a MATLAB plot

I'm creating a 2d MATLAB plot. I'm setting the limits of my x axis, and letting my y axis auto-adjust (by setting its limits to [-inf inf]). After creating my plot, I need to check what my y axis has auto adjusted to (as I'm going to create a heatmap to put under my plot).
Unfortunately, ylim (and similar functions) only produce [-inf inf], not whatever the axes have adjusted to.
Some code which reproduces this problem (much more simply than my actual code) is:
function createplot(xbounds)
x = xbounds(1):0.5:xbounds(2);
y = x.^2;
plot(x,y);
axis([xbounds,-inf,inf]);
createplot([0,10])
which produces a parabolic plot with y limits = [0,100]. However, ylim = [-inf, inf].
Any help would be appreciated!
/ Wilbur
As #Shai suggested, axis can give info regarding the ylimits without the need to set them to [-inf,inf] or use axis to set the x-axis bounds:
xbounds=[1 10]
x = xbounds(1):0.5:xbounds(2);
y = x.^2;
plot(x,y);
xlim([xbounds(1) xbounds(2)]);
v=axis
v =
1 10 0 100
Looking at #natan's answer I think the solution to your problem is
Do not use [-inf inf] for auto-adjusting axis limits.
If you want Matlab to auto adjust some of your axes limits and manually set others, then you should use either xlim, ylim or zlim for the specific axis you wish to set and leave all the other unchanged so Matlab can set them automatically.
This way you will not override the values Matlab assigns to those axes and you will be able to read them using axis, xlim, ylim or zlim.
Please see #natan's answer for corrected code.

Matlab - Continuous Plot and Semilogx on the same figure

I am trying to plot on the same figure the evolution of a function f, with argument x in ]0,1]. I would like to see both the evolution of f far away from 0 and close to 0, on the same figure, with one x axis.
For now I only have two different figures, one using plot with x=[0.1 ... 1], and one using semilogx with x=[1e-9 1e-7 1e-5... 0.1]. I would like to have both graphs on the same figure, with the x axis being logarithmic at the beginning, then linear after a certain x0 (let's say x0=0.1).
I do not want something using plotxx since I want only one x axis.
Do you know if this is possible?
Thank you for your time and help.
Just plot your y vector without specifying the x vector, this will get you a uniformly spaced plot, then use XTick and XTickLabel to make it work. For example,
x1=logspace(-10,-1,10);
x2=linspace(1,10,10);
y1=x1.^0.25;
y2=1./x2;
plot([y1 y2],'-x')
xlabels=num2cell([x1 x2]);
set(gca,'XTick',1:numel(x1)+numel(x2),'XTickLabel',xlabels)
If you want to use Latex to format tick labels, you'll need to download a function from the Matlab File Exchange.

Fixing the Radial Axis on MATLAB Polar Plots

I'm using polar plots (POLAR(THETA,RHO)) in MATLAB.
Is there an easy way to fix the range for the radial axis to say, 1.5?
I'm looking for something analogous to the xlim, ylim commands for cartesian axes. Haven't found anything in the docs yet.
this worked for me... i wanted the radius range to go to 30, so i first plotted this
polar(0,30,'-k')
hold on
and then plotted what i was actually interested in. this first plotted point is hidden behind the grid marks. just make sure to include
hold off
after your final plotting command.
Here's how I was able to do it.
The MATLAB polar plot (if you look at the Handle Graphics options available) does not have anything like xlim or ylim. However, I realized that the first thing plotted sets the range, so I was able to plot a function with radius range [-.5 .5] on a [-1 1] plot as follows:
theta = linspace(0,2*pi,100);
r = sin(2*theta) .* cos(2*theta);
r_max = 1;
h_fake = polar(theta,r_max*ones(size(theta)));
hold on;
h = polar(theta, r);
set(h_fake, 'Visible', 'Off');
That doesn't look very good and hopefully there's a better way to do it, but it works.
Simple solution is to make a fake graph and set its color to white.
fake=100;
polar(0,fake,'w');
hold on;
real=10;
polar(0,real,'w');
In case anyone else comes across this, here's the solution:
As Scottie T and gnovice pointed out, Matlab basically uses the polar function as an interface for standard plots, but with alot of formatting behind the scenes to make it look polar. Look at the values of the XLim and YLim properties of a polar plot and you'll notice that they are literally the x and y limits of your plot in Cartesian coordinates. So, to set a radius limit, use xlim and ylim, or axis, and be smart about the values you set:
rlim = 10;
axis([-1 1 -1 1]*rlim);
...that's all there is to it. Happy Matlabbing :)