I am trying to add error bars to a line plot, using MATLAB R2015a.
I've gotten so far that dots are plotted as error bars - not lines that go up and down from the points.
YLim is 0 to 100.
One of the standard deviations (length of error line) is 10, so it is impossible for it to be plotted as only a dot.
This lead me to think that something is bugged in my errorbar() function.
Copying the following code from Matlab's errorbar help:
x = 0:pi/10:pi;
y = sin(x);
e = std(y)*ones(size(x));
figure
errorbar(x,y,e)
produces an empty figure, not a sine wave with error bars as in the help file...
Any idea what might be going on?
Related
I am trying to create histograms in a loop. I am creating two figures and want a suptitle above them, but when I do that the title of the subplots doesn't work anymore . This is my code
suptitle('Observation')
for i=1:c:b
i
MagObs1=[];
subplot(b,1,i);
MagObs1=MagObs(:,i);%0 and 1s
minMagObs1=min(MagObs1);
MagObs2=MagObs1(MagObs1>0.001);
h1=histogram(MagObs2,NumberBins,'Normalization','probability');
title([num2str(DepthObs(i)),'m']);
h1.BinLimits=[bottomVel topVel];
xlabel('Current speed (m/s)');
ylabel('Frequency');
end
figure(2);% clf;
suptitle('Model')
for i=1:c:b
subplot(b,1,i);
h2=histogram(MagMatrixH1(i,:),NumberBins,'Normalization','probability')
title([num2str(DepthObs(i)),'m'])
h2.BinLimits=[bottomVel topVel]
xlabel('Current speed (m/s)')
ylabel('Frequency')
end
and this is the error I get
Error using axes
Invalid axes handle
Error in suptitle (line 98)
axes(haold);
Error in Histogram (line 118)
suptitle('Observation')
This is my output. Normally, every figure has multiple histograms underneath each other, but for this example I only show one.
As you can see in the second picture. "Im" should be '300m', could you help me fix this subplots title?
In the (very little) documentation that suptitle has, it says:
"Use this function after all subplot commands."
Try adding it in the end of your plotting
I have this code in Matlab R2015a:
figure
set(gcf,'color','w')
hax = axes;
errorbar(f_cc/(1e6), abs(Z_cc),err_Z,'o-');
hax.XScale = 'log';
grid on
xlim([3.6846 3.6900])
xlabel('f(MHz)')
ylabel('|Z(f)|')
where f_cc, Z_cc and err_Z are vector with 31 components and double precision (Z is in addition complex). However, I get this result:
I thought the problem could be the 'capsize' of the errorbars, but when I try to add the attribute 'capsize':
errorbar(f_cc/(1e6), abs(Z_cc),err_Z,'o-','Capsize',1);`
then Matlab gives me an error:
There is no Capsize property on the ErrorBar class
so I don't known how to change it (if that's the problem).
The oldest version of MATLAB I have is R2016a, and I could reproduce your error bar bug with it. In R2017a it has been fixed.
The problem arises with the combination of errorbar and the logarithmic x-axis with a (relatively) small spread. The error bars work perfectly fine if you keep the x-axis linear, or if the x-values have a wider spread. For example:
x = linspace(3.6846, 3.6900, 20);
y = rand(size(x));
e = linspace(0.01, 0.5, numel(x));
errorbar(x, y, e, 'o-')
set(gca, 'XScale','log')
xlim(x([1,end]))
Shows the ridiculous error bars, but if we set x = linspace(3.6846, 3.6900*10, 20); (i.e. having the x-axis span a decade) the plot looks perfectly fine.
Workaround #1
Since the data has such a small spread, there is no point in using a logarithmic axis. With set(gca, 'XScale','lin') your plot will look almost the same, but with normal error bars.
Workaround #2
Use a linear x-axis, but simulate logarithmic plotting:
errorbar(log(x), y, e, 'o-')
ticks = 3.684:0.002:3.690;
set(gca, 'XTick',log(ticks), 'XTickLabel',ticks);
xlim(log(x([1,end])))
But honestly, this doesn't even look like logarithmic axis because the range is so small, I can't really tell the difference... :)
Regarding the error setting the 'capsize' property: As Sardar Usama mentioned in a comment, this property was introduced in R2016b, so it is expected that you would get that error message.
I have two graphs, one is the exact graph of a solution, the other is a numerical approach. I have 4 specific points in my figure (t=0.25,0.5,0.75,1), where I want to illustrate the difference between the two graphs with a straight line. I found the errorbars function but i don't see any use there. Hope you can help me!
Edit:
this is the example figure:
t = [0:0.25:1];
y = t.*4;
x = t.^2+3;
plot(t,y,t,x)
I have 4 points now, t=0.25; t=0.5; t=0.75; t=1; At this points, I just want a vertical line between the two plots. I already have tried this: plot([t(1),y(1)],[t(1),x(1)])
but it just creates a line over the whole figure.
✶ It seems that you're not using hold on before using plot command the second time because otherwise you'd have got the desired result (which is actually not a correct way of plotting a vertical line).
✶ You're mixing up the values of x and y for plot(x,y). To plot a vertical line, it should be used like this: plot([x,x], [y1,y2])
For your case, you may not notice the difference between plot([t(1),y(1)],[t(1),x(1)]) (which is incorrect) and plot([t(1),t(1)],[x(1),y(1)]) (which is correct) because it is by chance that the values are same. Plot it for some other points and you'll realize the difference.
Fixed Code:
t = [0:0.25:1];
y = t.*4;
x = t.^2+3;
plot(t,y,t,x)
hold on
plot([t(1) t(1)],[x(1) y(1)])
% You have 't' on your horizontal axis and 'x'and 'y' values on the vertical axis
axis equal % just for better visualization
Output:
I am using "stem" command to plot discrete values into figure. I dont know how to remove the line connecting the points to the x-axis. I found a lot of parametres that changes the shape and color of the line but none that would remove it.
stem(X, Y)
Is there a way how to do this or am I using wrong command? Plot and bar is not what Im looking for...
I somewhere saw this:
stem(X, Y, 'none')
but it doesnt work.
t = linspace(-2*pi,2*pi,10);
h = stem(t,cos(t),'LineStyle','none');
Probably a too special question, but just in case someone has faced a similar problem.
I am using plot2svg to get plots from Matlab to Inkscape. It works fine, except for one pretty annoying problem. If I want to show just a zoomed part of a curve, plot2svg "captures" the whole curve and then clips it. As a result, the SVG file is much larger than it could be, and sometimes Inkscape even crashes on complex curves.
What would help is some way to remove the data outside the axis before exporting SVG. I've tried brushing (remove unbrushed), but then plot2svg fails:
Attempted to access parts(1); index out of bounds because numel(parts)=0.
Error in plot2svg>line2svg (line 2237)
if parts(1)~=1
Error in plot2svg>axchild2svg (line 1365)
line2svg(fid,groupax,axpos,x,y,scolorname,linestyle,linewidth)
Error in plot2svg>axes2svg (line 1042)
group = axchild2svg(fid,id,axIdString,ax,group,paperpos,axchild,axpos,groupax,projection,boundingBoxAxes);
Error in plot2svg (line 221)
group=axes2svg(fid,id,ax(j),group,paperpos);
Can anyone suggest any solution? I would like to avoid using the EPS or PDF export in Matlab because their quality is much worse than that of the plot2svg.
Matlab 2011b 64bit, plot2svg 10-Nov-2010, Win 7 Pro 64.
All values of your curve that lie outside the (zoomed in) axes should be set to NaN. Matlab will not draw these points and, hence, plot2svg will not export them. However, this involves redrawing the curve after zooming.
Make sure to save a handle to your curve when plotting it:
figure(1)
h = plot(yourXdata, yourYdata);
Then zoom in on your area of interest and afterwards run the following code:
figure(1)
hold on
myXlim = xlim;
myYlim = ylim;
% crop your data
yourXdata(yourXdata < myXlim(1)) = NaN;
yourXdata(yourXdata > myXlim(2)) = NaN;
yourYdata(yourYdata < myYlim(1)) = NaN;
yourYdata(yourYdata > myYlim(2)) = NaN;
% delete old curve and draw the new one
delete(h);
plot(yourXdata, yourYdata);