Set y axis max value with Matlab - 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.

Related

How to set x and y values when using bar3 in Matlab?

Quick version
How can I control the x- and y-values for a 3-d bar plot in Matlab?
Details
Say we have an 10 x 20 data matrix and we plot it using bar3, and we want to set the x- and y-values. For instance:
foodat = rand(10,20);
xVals = [5:14];
yVals = [-3:16];
bar3(xVals, foodat);
xlabel('x'); ylabel('y');
Is there a way to feed it the yVals as well? Otherwise the y axes always defaults to [1:N].
Note I don't just want to change the labels using XTickLabel and YTickLabel. I need to change the actual values on the axes, because I am plotting multiple things in the same figure. It isn't enough to just change how the (wrong) axis ticks are labeled. So this is different from issues like this:
How can I adjust 3-D bar grouping and y-axis labeling in MATLAB?
Other things I have tried
When I try changing the xvals with:
set(gca,'XTick', xVals)
set(gca,'YTick', yVals)
The values are taken in, but actually show up on the wrong axes, so it seems x and y axes are switched using bar3. Plus, it is too late anyway as the bar graph was already plotted with the wrong x- and y-values, so we would end up giving ticks to empty values.
Note added
Matlab tech support just emailed me to let me know about the user contributed function scatterbar3, which does what I want, in a different way than the accepted answer:
http://www.mathworks.com/matlabcentral/fileexchange/1420-scatterbar3
I found a way of doing it. Ill give you a piece of code, then you'll need to "tidy up" , mainly the axis limits and the Xticks, as bar3 does set up the Xticks inside, so if you want others you'll need to set them manually yourself.
So the trick here is to get the Xdata from the bar3 handle. The thing here is that it seems that there is a handle for each row of the data, so you need to iterate for each of them. Here is the code with the current output:
foodat = rand(20,10);
xVals = [5:14];
yVals = [-3:16];
% The values of Y are OK if called like this.
subplot(121)
bar3(yVals, foodat);
subplot(122)
h=bar3(yVals, foodat);
Xdat=get(h,'XData');
axis tight
% Widdth of barplots is 0.8
for ii=1:length(Xdat)
Xdat{ii}=Xdat{ii}+(min(xVals(:))-1)*ones(size(Xdat{ii}));
set(h(ii),'XData',Xdat{ii});
end
axis([(min(xVals(:))-0.5) (max(xVals(:))+0.5) min(yVals(:))-0.5, max(yVals(:))+0.5])
Note: Y looks different but is not.
As you can see now the X values are the ones you wanted. If you'd want other size than 1 for the intervals between them you'd need to change the code, but you can guess how probably!

Matlab plotting through origin regardless of only negative or only positive data

this one should be really easy to answer, yet I am surprised it is nowhere described:
I have variable data, and often when I simply plot, I get a close-up on the y-axis (x-axis is time and starts always with a zero). So I get y-values from 16-17, but the zero is nowhere to be seen. I know you can give matlab an YLim value, yet sometimes because my data is variable, I get a range from -50-100 in y. If i had specified before that YLim is [0 20] I wouldnt see it at all, which is not what I want. I just always want to have the origin plotted with my data, how do I do that (without a rigid interval like [-1000 1000], since sometimes I have y values from -0,01-0,001 and wouldnt see it)? Please help if you have an idea!
Thanks!
UPDATE 1: perfectly solved!
Follow-up question:
There is one thing I didnt mention: I need to do this for all my 18 subplots. How can I avoid hardcoding it?
One way to do this is automatically is to use:
ylim([min([y 0]) max([y,0])])
this way, it will start or stop at 0 if 0 is not already in the range of y
or better, if you want to keep the rounded values matlab provides (eg plot on [0,6] and not on [0,5.872]), first plot your data with plot(x,y), then change the ylim values to 0 if needed:
ylim([min([ylim 0]) max([ylim 0])])
It's easy to manually modify axis size:
x = 1:10; %/ example x
y = 5 + rand(1,10); %// example y. Values between 5 and 6
plot(x,y) %// do the plot normally
ax = axis; %// get axis size
ax(3) = min(ax(3),0); %// if the y-axis lower limit is positive, make it 0
axis(ax) %// apply new axis size values
This also seems to work: just use
plot([x NaN],[y 0])
That is, include a point with x value set to NaN and y value set to 0. The point doesn't get plotted (because of the NaN value) but it forces the y axis to strech out to 0.

Distance between axis number and axis in MATLAB figure

I struggle a little bit with overlapping axis numbers of the y and x axis like it is shown in the image. I'd like to keep the size of the numbers and therefore think that simply shifting the numbers away from the axis itself would be an appropriate way to handle this issue.
Is there a possibility to do that?
Thanks in advance,
Joe
Here is a little workaround using text annotations. Basically you clear the current XTick labels and replace them with similar labels, but you can specify the distance from the axis:
clc
clear
close all
x = 1:20;
hPlot = plot(x,sin(x));
set(gca,'xaxisLocation','top');
set(gca,'XTickLabel',[]); %// Clear current XTickLabel
ylim = get(gca,'YLim'); %// Get y limit of the plot to place your text annotations.
for k = 2:2:20
text(k,ylim(2)+0.1,num2str(k),'HorizontalAlignment','Center') %// Play with the 'ylim(1) -0.1' to place the label as you wish.
end
Giving this:
Of course now it's exaggerated and you can do the same for the y axis if you want (using the 'XLim' property of the current axis,gca).

How to Adjust y axis plot range in 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)

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.