MATLAB focus plot and apply correct tick labels - matlab

I'd much rather be asking you about Bayesian analysis over this, but unfortunately, I am terrible with MATLAB plots (not due to a lack of effort though).
I have a plot attached. It contains 1739 days of data. I need to do the following three things:
Focus in on the plot so that the y axis limits are -1 and 1 (right now they are -4 and 14). I'm uninterested in any values beyond these limits.
Re-label the y axis from -1 to 1 in .10 increments.
Re-label the x axis so that it starts with 1, ends with 1739, and has enough points in between that it is still readable.
I've studied gca, datetick, ax=gca, etc. but I cannot accomplish any of these tasks without messing up the plot.
I am gracious for any assistance provided.

Changing the axis limits is very simple,
axis([1 1739 -1 1]) %// [xMin xMax yMin yMax]
And for the tick marks, do
set(gca,'XTick',-1:.1:1,'YTick',1:79:1739) %// 2*79 might be better than 79
after you create the plot. You can (should?) replace gca with the handle to the plot (do h=plot(... when you make the plot, then set(h,'XTick',...).
You should also go through Matlab Plotting Basics which goes through all this kind of stuff in good detail.

Related

I don't know hot to plot this nonlp's feasible reagion in matlab

I searched and watched how to plot the 3 dimensions of nonlp program's
but I still don't know how to plot these constraints.
x^2+y^2+z^2=1
x>=2*y
2*y>=3*z
x>=3*z
I programmed like this but it doesn't work and I think this is wrong program for upper constraints.
func1 = #(x,y,z) sqrt(1-x.^2-y.^2);
func2 = #(x,y,z) max(x-2*y,0);
func3 = #(x,z) max(x-3*z,0);
ezsurf(func1,[0 1 0 1]);
hold on;
ezsurf(func2,[0 1 0 1]);
hold on;
ezsurf(func3,[0 1 0 1]);
axis([0 1 0 1 0 1]);
A way of doing that is to work with volumetric data.
The idea is to create a 3D space, with a F value. F will be positive in one side of your main equation and negative in the other.
[X,Y,Z]=meshgrid(-2:0.05:2,-2:0.05:2,-2:0.05:2);
F=X.^2+Y.^2+Z.^2-1;
Then the conditions are applied to this F. If you want to see the object "cut"
then substitute the place where conditions are not met with nan. If you want to see a filled object, then substitute it with a positive number (it may need to be negative in another situation, so check this for other examples.
% Conditions
cond1=X>=2*Y;
cond2=2*Y>=3*Z;
cond3=X>=3*Z;
% If you want the boundaries to show
F1=F;
F1(~cond1)=1;
F1(~cond2)=1;
F1(~cond3)=1;
% If you want the boundaries not to show
F2=F;
F2(~cond1)=NaN;
F2(~cond2)=NaN;
F2(~cond3)=NaN;
Then the idea is to create an isosurface in the zero level (your original function). Here in this code I created 2 plots so you can see the differences between filling and not filling the boundaries. Also some fancy coding in order to get nice figures. However the isosurface and patch parts are relevant to obtain the surface and plot it.
subplot(121)
iso1=isosurface(X,Y,Z,F1,0);
p=patch(iso1);
isonormals(X,Y,Z,F1,p);
set(p,'FaceColor','red','EdgeColor','none');
daspect([1 1 1])
axis equal
camlight
lighting gouraud
subplot(122)
iso2=isosurface(X,Y,Z,F2,0);
p=patch(iso2);
isonormals(X,Y,Z,F2,p);
set(p,'FaceColor','red','EdgeColor','none');
axis equal
daspect([1 1 1])
camlight headlight
lighting gouraud
Result:
Additionally, You can calculate the isosurface of the initial boundary F and plot it with a low alpha value, so you can better understand what piece of the domain you are actually selecting.

Matlab Subplot of symbolic functions (with quite different range and domain), Want to achieve same axis scale for every subplot

I have 3 subplots in Matlab, each curve has quite different domain and range. I wish to have the same scale of (1 unit = 1 cm of paper) in x and y axes of all the graphs.
I cannot use linkprop for xlim or ylim, as the domain and range is quite different, and the complete graphs do not appear.
Kindly give some suggestion.
I can't say I'm proud of this, but it does seem to work!
x1=0:.1:1;
x2=50:1:90;
x3=0.043:0.001:0.089;
y1=sin(x1);
y2=x2.^2;
y3=-x3;
r=max([max(y3)-min(y3) max(y2)-min(y2) max(y1)-min(y1)]);
d=max([max(x3)-min(x3) max(x2)-min(x2) max(x1)-min(x1)]);
k=max(r,d);
subplot(1,3,1)
plot(x1,y1)
axis([min(x1)-(k-max(x1)+min(x1))/2 max(x1)+(k-max(x1)+min(x1))/2 ...
min(y1)-(k-max(y1)+min(y1))/2 max(y1)+(k-max(y1)+min(y1))/2])
subplot(1,3,2)
plot(x2,y2)
axis([min(x2)-(k-max(x2)+min(x2))/2 max(x2)+(k-max(x2)+min(x2))/2 ...
min(y2)-(k-max(y2)+min(y2))/2 max(y2)+(k-max(y2)+min(y2))/2])
subplot(1,3,3)
plot(x3,y3)
axis([min(x3)-(k-max(x3)+min(x3))/2 max(x3)+(k-max(x3)+min(x3))/2 ...
min(y3)-(k-max(y3)+min(y3))/2 max(y3)+(k-max(y3)+min(y3))/2])

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)

X-axis goes farther than last time interval

If I set a time interval, [0 10], and I plot something according to that time interval, plot(t, y()), then the graph will have its x-axis going all the way to 11.
It used to not do this. It used to produced a better fitting graph, but now it produces a graph with white space attached to the end of the graph between points 10 and 11.
I must have accidentally changed a setting. How do I get the plot function to best fit the graph?
If you want to make a polished plot, you almost always have to adjust the plot axis limits yourself. Some useful commands:
axis tight; %Squeeze the data as closely as possible
xlim([xMin xMax]); %Adjust the x axis limits, do not touch the y limits
ylim([yMin yMax]);
axis([xMin xMax yMin yMax]); %Adjust all axis limits
You your case you would probably add a line like this to your code:
xlim([min(t) max(t)]);
Or, if you know that t is sorted (very common)
xlim(t([1 end]));

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 :)