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

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

Related

Matlab: Format the decimals in contour labels

I want to cut the number of decimals in the following contour plot. I do:
[cc,hh] = contour(X,Y,Z,levels,'LineColor','k');hold on
texth = clabel(cc,hh,'FontSize',8);
which gets me the first contour with long labels. Then in order to cut the decimals I do:
for i = 1:size(texth); textstr=get(texth(i),'String'); textnum=str2double(textstr); textstrnew=sprintf('%0.0f', textnum) ; set(texth(i),'String',textstrnew); end
And this gives the second plot. As you see, there is a wide gap between the label and the contour lines which looks awful. Any ideas how to solve this?
Instead of modifying the result, create a contour plot with the levels you want, so you don't need to cheat the data.
Define levels as e.g. levels=997:1010
and then
contour(X,Y,Z,levels,'LineColor','k','ShowText','on');
Will create a contour plot with the text included and the levels being specifically the ones in the variable levels, in this case 997,998,999,...,1009,1010
If, as #David suggests, your levels variable already is a vector, then replace it by round(levels) as himself suggested.

Smooth Excel-like plot with correct legend in Matlab

I have some sparse data and want to plot them as markers connected by a smooth, interpolated line - like the default behaviour of Microsoft Excel.
There are solutions to this problem easily found on the internet, but I find them unsatisfactory. What they do is: plot the sparse data as one data set drawing it as markers without lines, interpolate it with a method of choice and plot the interpolation as the second data set, with lines without markers.
The problem with these tricks is that in the legend the two data sets will be listed separately. I would expect a single data set depicted in the legend as a line crossing through a marker.
Is it possible in Matlab?
If you want to plot an interpolated line there are lots of ways to do that. You can try generating an interpolated line using the matlab interp1() function.
Let's create x and y data with no NaN.
x = randn(1,10)
y = randn(1,10)
If you want 1000 data points where previously you only had a few, that's pretty easy:
x2 = min(x):(max(x)-min(x))/1000:max(x)
y2 = interp1(x,y,x2,'cubic')
and you can plot your data and spline using
plot(x,y,'r+')
hold on
plot(x2,y2,'r-')
A custom legend is straightforward when you use handle graphics. You can plot a dummy data set with a red line passing through a marker using
h(1) = plot(NaN,NaN,'r-+')
lstring{1} = 'Data';
You can then add a legend that points to this data set using
legend(h,lstring)
You'll end up with something that looks roughly like this:
The nice thing about using handle graphics (i.e. the h) is you can throw whatever data series you want into the legend as h(end+1) and lstring{end+1}.

MATLAB: Plotting Contours at Specfic Points (x,y)

I'm currently producing a contour plot using
contour(x,y,z)
However, I would like to specify some additional contour lines to the ones provided.
I understand that I can use contour(x,y,z,v) where v is some vector containing values of the contour levels I would like but I don't really want to use this since I don't know exactly the levels.
Instead is it possible to plot the contour that goes through a specific point (x,y)?
Thanks.
You can overplot a second contour with a single, specific value for the contour, optionally specifying parameters like line width to make it obvious:
contour(x,y,z)
hold on
lev = z(n,m); % find the value you want in z
contour(x,y,z,lev,'Linewidth',2);

How to plot 2D data with different colors and markers

Im faced with a problem where i need to plot a two dimensional data with different colors and markers.
We are given with 2 array, namely points (n x 2 dimension) and Label (n x 1 dimension). Im not sure about the number of unique values in the Label array but maximum could be 10. I would like to plot the points with different color and markers based on its corresponding Label value.
Can any one help me in this regard
Use gscatter, which does a scatter plot, using a group (Label in your case) to plot in different colours/makers.
GSCATTER(X,Y,G,CLR,SYM,SIZ) specifies the colors, markers, and
size to use. CLR is either a string of color specifications or
a three-column matrix of color specifications. SYM is a string
of marker specifications. Type "help plot" for more information.
For example, if SYM='o+x', the first group will be plotted with a
circle, the second with plus, and the third with x. SIZ is a
marker size to use for all plots. By default, the marker is '.'.
So you can specify colours like 'rgcmykwb' to do red for the first group, green for the second, etc or [] just to have Matlab sort it out.
By default Matlab uses the same marker for each group, so you need to specify what markers you want to be used for each group. If you do '.ox+*sdv^<>ph' you'll just cycle along all the markers that Matlab has.
n=50;
% make nx2 matrix of random points.
points = random('unif',0,1,n,2);
% make nx1 matrix of random labels from {1,2,...,5}
labels=round(random('unif',1,5,n,1));
% plot. Let Matlab sort out the colours and we will specify markers.
gscatter(points(:,1),points(:,2),labels,[],'ox+*sdv^<>ph.')
It looks a bit like this:

Scatter with different colors

If I have
matrix=
0.0494 2.3691
-0.0973 0.8026
-0.3040 -0.0861
-0.0626 2.5688
-0.4144 0.7054
0.0633 -0.0991
-0.8386 -1.2229
1.8929 2.6260
1.7687 2.3963
1.8243 -0.5543
1.9272 -0.3946
-0.0682 1.7404
-0.1180 2.2323
0.4071 -0.1878
0.6406 2.5602
-0.2144 2.0014
0.1091 -0.1874
-0.1102 0.2922
How Would you plot one column in a color and other in a different color, or some of them in one color
scatter(matrix(:,1),matrix(:,2), 'b','+');
scatter does not plot each column separately. It is column 1 vs column 2. So, each point on the scatter plot is made up of both columns. In other words, there is no difference between scatter(x,y) and plot(x,y,'o'). However, scatter has other features, which is why it is available as a different function. If you were just trying to plot each column separately with two colors, you can simply do plot(matrix,'o') and MATLAB should automatically assign blue for the first column and green for the second.
scatter also takes a colormap as an argument. So if you intended to plot half your data (both columns) one color and the rest another, you can try this
nRows=size(matrix,1);
red=repmat([1,0,0],fix(nRows/2),1);%# use fix so that you don't get an error if nRows is not even.
green=repmat([0,1,0],nRows-fix(nRows/2),1);
scatter(matrix(:,1),matrix(:,2),[],[red;green]);