Matlab plot a threshold line - matlab

I want to add a horizontal threshold line to my graph of 0.7. But I can't seem to get it to work.
Code
figure(1)
plot(Variance);
hold on;
plot([1 frames], threshold, 'red')
Variance is an array to be plotted and the frames are the number of variances that are plotted. I've tried moving the hold and plots around but it doesn't seem to work either.
I know this is an easy question, but everything I've looked at online doesn't seem to work either, with regards to adding them to the same plot().
Thanks

You should change the second plot line to
plot([1 frames], [threshold threshold], 'red');

Related

Plotting multiple fitobjects in the same figure in MATLAB 2016b

I have multiple fitobjects that I want to plot in the same figure. All curves are fitted from the same five points on the x-axes (ranging from 0.25 to 2.25). If I plot them individually the curve correctly spans only between does values, however, as soon as I add hold on to plot them in the same figure the first fitobject is plotted correctly, while the second spans from -Inf to +Inf, if I change the order of fitobjects the problem remains. So the first curve is always shown correctly the following ones are not. I am not even setting any axes properties yet. Also the two fitobjects have the same Boundaries for the fit.
Example code:
plot(fitobj{1}, 'r'); hold on %curve from 0.25 to 2.25
plot(fitobj{2}, 'b'); %curve is shown from -Inf to +Inf
Do you know why this is happening? Thank you

Matlab scatter plot with straight lines connecting the points

Is there an easy command to have a plot like the blue line in the picture (excel)? Matlab defaults to produce something like the line in red. The only way I know to do this is to issue a plot command for each segment of the line:
for i=2:n-1
plot([data(i-1,1) data(i,1)],[data(i-1,2) data(i,2)],'-b'); hold on;
end
You can just plot the entire array and let plot automatically draw straight line segments between each of the points. This is the default behaviour when plotting things in MATLAB. MATLAB plotting smooth lines is not the default behaviour when the plot is produced, so I'm not sure where you're getting that information.
You would need to perform some sort of spline interpolation to get the red line, but you desire the blue curve and so plotting the entire array in a single plot command should suffice.
It's as simple as:
plot(data(:,1), data(:,2), '-b');
Just to be sure that we're on the same page, I'm going to reproduce your data then use the above command to plot the data so you can see for yourself that the behaviour you desire is achieved:
data = [0 0; 1 1; 2 4; 3 6; 4 4]; %// Your data reconstructed
plot(data(:,1), data(:,2), '-b'); %// Main plotting code
%// Some extras
xlim([0 4.5]);
ylim([0 7]);
grid;
I've added in some extra code to get the plot to look like your example. I've made the x-axis limits go up to 4.5 and the y-axis limits go up to 7. I've also placed a grid in the plot.
We get:

i am trying to plot "scatter" on top of "imagesc" which dosen't work.

i am trying to plot "scatter" on top of "imagesc" which dosen't work. However, i can plot "scatter" figure separately. I even tried "hold all" instead of "hold on". Can someone help me out? Thank you.
figure(2)
imagesc(lat1,height,scatter0')
hold on;
scatter(lat1,top2,'k')
title('2012_12_4')
colormap(colors)
axis xy
It is probably x-y limits that are not matched, or that the z-values that are not properly normalized to plot side by side. The normalization is important because imagesc and scatter will share the same colormap. Other than that, your code works nicely for me. For example, I'm normalizing in the range [0,1] the "z" values of both plots:
load seamount
m=peaks(200);
m=(m-min(m(:)))./(max(m(:))-min(m(:)));
imagesc([0.996 1.0005],[1,1.012],m);
hold on ;
z=(z-min(z(:)))./(max(z(:))-min(z(:)));
scatter(x./max(x(:)),y./max(y(:)),5,z);

How to show x and y axes in a MATLAB graph?

I am drawing a graph using the plot() function, but by default it doesn't show the axes.
How do we enable showing the axes at x=0 and y=0 on the graph?
Actually my graph is something like:
And I want a horizontal line corresponding to y=0. How do I get that?
This should work in Matlab:
set(gca, 'XAxisLocation', 'origin')
Options are: bottom, top, origin.
For Y.axis:
YAxisLocation; left, right, origin
By default, plot does show axes, unless you've modified some settings. Try the following
hold on; % make sure no new plot window is created on every plot command
axes(); % produce plot window with axes
plot(% whatever your plot command is);
plot([0 10], [0 0], 'k-'); % plot the horizontal line
The poor man's solution is to simply graph the lines x=0 and y=0. You can adjust the thickness and color of the lines to differentiate them from the graph.
If you want the axes to appear more like a crosshair, instead of along the edges, try axescenter from the Matlab FEX.
EDIT: just noticed this is already pointed out in the link above by Jitse Nielsen.
Maybe grid on will suffice.
I know this is coming a bit late, but a colleague of mine figured something out:
figure, plot ((1:10),cos(rand(1,10))-0.75,'*-')
hold on
plot ((1:10),zeros(1,10),'k+-')
text([1:10]-0.09,ones(1,10).*-0.015,[{'0' '1' '2' '3' '4' '5' '6' '7' '8' '9'}])
set(gca,'XTick',[], 'XColor',[1 1 1])
box off
#Martijn your order of function calls is slightly off. Try this instead:
x=-3:0.1:3;
y = x.^3;
plot(x,y), hold on
plot([-3 3], [0 0], 'k:')
hold off
Inspired by #Luisa's answer, I made a function, axes0
x = linspace(-2,2,101);
plot(x,2*x.^3-3*x+1);
axes0
You can follow the link above to download the function and get more details on usage
Easiest solution:
plot([0,0],[0.0], xData, yData);
This creates an invisible line between the points [0,0] to [0,0] and since Matlab wants to include these points it will shows the axis.

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