matlab: how to plot different errorbars in bar graph - matlab

i want to plot different errorbars std_a/b/c in my bar graph
a=5; std_a=0.9;
b=6; std_b=0.5;
c=7; std_c=0.2;
%plot
bar([a,b,c]);
errorbar([a,b,c],[std_a,std_b,std_c]);
somehow this is not working. how can get for each bar the correct errorbar?

You're close. errorbar by default plots a line and adds errorbars to it, and if you haven't called hold on or hold all it will overwrite what you already have. If you just want the error bars and not lines between them, give it a plot format that only plots points, like r.:
bar([a,b,c]);
hold on
errorbar([a,b,c],[std_a,std_b,std_c],'r.');

I had to improve location of the error bars on x axis because for more than one data series the bars appear side by side, while the error bars may appear one on top of the other.
for data with two columns and serr (standard error in my case) of the same size I used a shift to the left and right of +-1.4 (see gap in the code below).
gap=0.14;
X=1:length(data);
X=[X'-gap,X'+gap];
errorbar(X,data,serr,'k.');
you can specify a third input argument of zeros the same size of serr, if you want to minimise the bottom bars so that only top error bars show.

Related

Adding x error bars to a scatter plot in tableau

I have a scatter plot in tableau:
I am looking to add error bars to the points but cant find how to do this. I would need to specify the lower limits and upper limmits (i.e. not just a fixed percentage of the value). The only thing I can find is adding y errors in the form of a gant plot but this isnt helpuful (I dont think) as I only need x errors.
Its possible with a combination of using Measure Values (to show lines between the error ranges) and a dual axis (to overlay the X values on the lines)
See sample workbook

Matlab - Error bars added to column plot instead of regular line plot, and stacked 2x2

I would like to create a 2x2 plot with error bars, where each mean is represented by a column rather than by a marker (data point), something like this:
The errorbar function in Matlab seems to not have the option to make columns instead of simple markers, and also no option to stack the four entities as 2x2, instead giving me something that looks like this:
You can create a bar plot with the function bar(Y), which will draw one bar for each element in Y. So if your data has 2 columns, the plot will be 2x2. You can then add errorbars with the errorbar function, and specify the linespec 'x' per example, to avoid the lines between the markers.
figure
hold on
bar(data)
errorbar(data,'x')
hold off

Place MATLAB legend such that it does not overlap on the plot

I am generating multiple plots of different datasets in succession using MATLAB. I would like the legend positions to be such that they don't overlap on the plotted lines and it would be ideal if this placement could be done automatically.
I am aware of setting the 'Location' to 'best' to achieve this but the placement of the legend tends to be awkward when 'best' is used (below). Also, I would like the legend to be inside the plot. I also came across a way to make the legend transparent (here) so that it does not render the plotted data invisible, but explicitly placing the legend elsewhere is what I am looking for.
Is there a way to place the legend at the extremes of the image ('NorthWest', 'SouthWest' etc) automatically such that it does not overlap on the plotted data (apart from the methods suggested above)?
So, you have tried using Location instead of Position? For example:
x =1:100;
y = x.^2;
lgd = legend('y = x.^2');
set(lgd,'Location','best')
and you are getting odd results correct? A quick way of solving this would be to still use Location, with best, and extract the coordinates:
lgd.Position
You should get something like this:
ans =
0.7734 0.3037 0.1082 0.0200
which maps to:
[left bottom width height]
You will need to focus on left and bottom. These two values, left and bottom, specify the distance from the lower left corner of the figure to the lower left corner of the legend, and they are analogous to the grid frame you are using.
Then, depending on the size of the frame (I would suggest you use axis([XMIN XMAX YMIN YMAX]) for this, if possible), you can pinpoint the position of the legend within the grid. What you can do next, is check if and which of your graphs in the plot cross paths with the legend (maybe define a relative distance function based on some distance threshold) and if they do, then randomly reposition the legend (i.e. change the values of left and bottom) and repeat until your conditions are met.
If this still troubles you I can write a short snippet. Finally, know that you can always opt for placing the legend on the outside:
set(lgd,'Location','BestOutside')

Bar graph x-axis titles do not work with many bars

I'm using MATLAB and I want a bar plot.
I plotted the bars, and then give titles to the bars.
If I only using 10 bars it works (see pic1).
But often I need more, mostly 32 bars (see pic2).
In this case the titles are not under the bars. I found out that MATLAB don't overlap the titles. The max value is 15 bars.
Does anyone knows how I can get MATLAB to overlap the titles?
Here is my code for plotting the bars:
y = absSlopes(1, 1:size(absSlopes,2));
hb = bar(y);
set(gca,'XTickLabel',titles);
rotateXLabels(gca, 45);
ylabel('Anzahl');
absSlopes is a matrix and I use the complete first line.
You may want to try XTick in addition to XTickLabel. At least for regular plots you can enforce specific spacings through this setting.
You can do this by an additional setup-command:
set(gca,'XTickLabel',titles);
set(gca,'XTick',absSlopes(1,:));
Of course, you can set both properties in the same line, but I like to keep it simple in case you want to deactiviate/comment one or the other setting later on.

MATLAB: Plotting two different axes on one figure

The MATLAB surf plot below is essentially two plots plotted adjacent to each other. For clarity, I have included some code below used to prepare the plot:
band1 = horzcat(band1, eSurface2(:,:,1));
band2 = horzcat(band2, eSurface2(:,:,2));
surf(band2,'DisplayName','band2');
surf(band3,'DisplayName','band2');
I would like for the y axis numbering to restart at the start of the second graph. How do I go about doing that?
You can use the 'YTick' and 'YTickLabel' properties of the axis to control the ticks, this way you can make it start from zero for the second graph. It will require some trail and error to get it right. See the relevant doc here (you'll have to scroll all the way to the bottom of the page).
Take advantage of the following feature of 'YTickLabel': "If you do not specify enough text labels for all the tick marks, MATLAB uses all of the labels specified, then reuses the specified labels".