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

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.

Related

"Fill" function in Matlab

I was given:
x = 2:0.1:10
y = sin(x)
and was asked to plot this function and fill it with green colour for sin above the x-axis and magenta for sin below the x-axis.
Since we have just started with matlab the colouring did not have to be exact but should cover most of the area between the function and axis.
My question is, why is this correct:
fill([2 2 2.6 pi], [0 sin(2) sin(2.6) 0], 'g');
why do I have to put two 2's in for the x-value and why does the y-value start and end with 0?
I am finding it difficult to understand where these numbers come from because for the second area,
fill([pi 3.9 3*pi/2 5.5 2*pi], [0 sin(3.9) -1 sin(5.5) 0], 'm');
I do not put in the same number twice for the x-values but still have to put in zeros for the y-values.
I would appreciate if someone could please explain how and why this data is chosen.
There are repeated x values wherever the author of these examples has chosen to draw a vertical edge (by definition, vertical means x doesn't change). There are zero y values wherever the author has chosen to fill all the way down to the x axis (where by definition, the y value is 0). Or up to it, in the second example.
To see this, for each of your examples, take the x values and the y values and look at them as a series of x,y pairs like this (in your first example):
>> [x(:), y(:)]
ans =
2.0000 0
2.0000 0.9093
2.6000 0.5155
3.1416 0
Now understand that each row gives you the coordinates of one vertex of a polygon that you are filling. Compare each coordinate point against the figures that are plotted.
If the object of the exercise is always to fill up to, or down to, the x axis, then you would generally expect repeated x values at both beginning and end---but in the two specific examples you've given, y seems to have already gone to zero (at the end of your first example, and at both beginning and end of your second example). It would be permissible (and perhaps more consistent, less confusing) to draw extra zero-length edges to the x axis—but it's redundant, and the author has chosen to omit them.
The first field is for all the x-values and the second field is for all the y-values. These values form the ordered pairs that define the co-ordinates of the corners of the shape that you want to fill.
For example, your second example
fill([pi 3.9 3*pi/2 5.5 2*pi], [0 sin(3.9) -1 sin(5.5) 0], 'm');
has the corners
(pi, 0)
(3.9, sin(3.9))
(3*pi/2, -1)
(5.5, sin(5.5))
(2*pi, 0)

How to change axis limits and tick step of a MatLab figure?

I have a simple plot of y against x.
y = [6,-1.3,-8,-11.7,-11,-6,1.3,8,11.7,11,6,-1.3];
x = 0:0.3:3.3;
plot (x,y)
As the result, the x-axis of the figure is ranging from 0 to 3.5, with scale of 0.5. I have used the XLimit = [0 3.3] to limit the axis, but it seems like not working.
I wish to make the x-axis range from 0 to 3.3 with steps of 0.3.
axis tight % removes the empty space after 3.3
set(gca,'XTick',0:0.3:3.3) % sets the x axis ticks
With XLimit = [0 3.3] you just define a vector called XLimit. In order to use this vector as horizontal limit, you should use xlim:
xlim(XLimit)
% or directly:
xlim([0, 3.3])
Read more about xlim here. Similarly you can set the vertical limit with ylim.
Since you are trying to set the limits equal to the range of x, you will probably find the following command most helpful:
axis tight
But note that it changes both x- and y-axis limits.
To set the tick step, as AVK said, you should set the 'XTick' to 0:0.3:3.3:
set(gca,'XTick',0:0.3:3.3)
gca is the handle to current axes.

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.

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!

How to set x axis values in MATLAB

I'm working with some signal. The signal has some length (time) and I devided it into 200 pieces and made some operations over them. The results are saved in matrix, so the matrix has one of the dimensions 200 and if I use imagesc() on it, to make results visualy readable, the x axis is from 0 to 200. But that does not correspond to time. The time is function of the values in x axis.
t = 640 * x
I need to make values in x axis to correspond to time. Is there any way, how to do this?
Use set and set the XTick and the XTickLabel properties accordingly. Assuming your image is already open, do this:
set(gca, 'XTick', 0:20:200);
set(gca, 'XTickLabel', 640*(0:20:200));
I used increments of 20 so that you don't clutter the x-axis. Modify the 20 to suit your tastes.