Cannot shorten capsize errorbar - matlab

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.

Related

Matlab: error bars broken

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?

Plotting shaded deviation of a line in Matlab

I would like to plot a line, and in grey-shaded X% deviation of a signal, in MATLAB. Then, I'd plot another signal and see (visually) how much of the second signal is outside the gret-shaded area.
The task I'd like to get help done is the shaded area: similar to the image attached below.
I am aware of similar solutions with errorbar, but I think this is a much clearer plot to visualize.
If for example I had:
x = 0:0.1:10;
y = 1 + sin(x);
What would the 5% grey-shaded plot of y look like? (that area?)
See this answer for an example: MATLAB fill area between lines
Do you have the error of y at each sample in x? Let's assume you have and the upper bound is in variable yu and the lower bound in variable yl. Then you could plot it using:
x = 0:0.1:10;
y = 1 + sin(x);
% I create some yu and yl here, for the example
yu = y+.1;
yl = y-.1;
fill([x fliplr(x)], [yu fliplr(yl)], [.9 .9 .9], 'linestyle', 'none')
hold all
plot(x,y)
fill(X,Y,ColorSpec,...) plots a polygon with edges specified in the first two parameters. You have to fliplr (flip left-right) the arrays, so that it correctly draws the shape of the area to be filled 'in a circle' around it. The [.9 .9 .9] is the colour specification, in this case a light grey. I removed the edge by setting no line, to make it even more similar to your desired plot. One detail: plot the filled area before plotting y, because the last plotted object goes on top of the others.

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)

Turning y axis upside down in MATLAB

Is there a way to turn the y axis upside down in matlab plots, so that the positive direction of the y axis, instead of up, points down ?
(I beg of you; please do not say, print it out, then turn the paper around ;-)
The 'YDir' axes property can be either 'normal' or 'reverse'. By default it is 'normal' for most plots, but some plots will automatically change it to 'reverse', such as the image or imagesc functions.
You can set the y-axis direction of an axes with either the set function or dot indexing (in newer MATLAB versions):
h = gca; % Handle to currently active axes
set(h, 'YDir', 'reverse');
% or...
h.YDir = 'reverse';
I'm baffled by some of the other answers saying that the 'YDir' property has somehow disappeared or is giving an error. I haven't seen any such behavior in versions of MATLAB from 2013, 2014, or 2016. There are only two potential pitfalls I came across:
The property can't be set with a cell array, only a character string:
>> set(gca, 'YDir', {'reverse'});
Error using matlab.graphics.axis.Axes/set
While setting property 'YDir' of class 'Axes':
Invalid enum value. Use one of these values: 'normal' | 'reverse'.
although this works:
set(gca, {'YDir'}, {'reverse'}); % Property name is also a cell array
The gca function can't be used interchangeably as a handle when performing dot indexing (which is why I first saved it to a variable h in the above example):
>> gca.YDir
Undefined variable "gca" or class "gca.YDir".
>> gca.YDir = 'reverse' % Creates a variable that shadows the gca function
gca =
struct with fields:
YDir: 'reverse'
Finally, if you want some code that will toggle the 'YDir' property no matter what its current state is, you can do this:
set(gca, 'YDir', char(setdiff({'normal', 'reverse'}, get(gca, 'YDir'))));
% or...
h = gca;
h.YDir = char(setdiff({'normal', 'reverse'}, h.YDir));
The command
axis ij
Will also reverse the Y-axis (negative above x-axis; positive below).
The solutions on the top of the stack did not work for me,
imagesc(x,y,data) % results in a flipped plot, the y axis is upside down
set(gca,'YDir','reverse'); % gives an error
axis ij; % still gives the flipped plot
what did work was the following:
imagesc(x,y,data); axis xy; % results in the correct plot
The YDir property has vanished in the matlab version (2013 and up) that I'm using.
To update this answer, since it is still a popular Google result:
As of R2014a, the correct way to flip the Y axis is the following:
>> axis ij
This change can be reversed through the following command
>> axis ji
To flip the X or Z axes, do the following
set(gca,'XDir','reverse');
set(gca,'ZDir','reverse');
Personally, I think it would have been easier to keep the YDir option, but what do I know.
As an alternative to YDir (for some reason I cannot currently see) you can rotate the axes with view. To turn the y-axis upside down, use
view(0,-90);

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