Hide line from legend - matlab

I need to hide 4 lines from the legend. I'm using shadedErrorBar so I have 3 line for a sigle group (mean, upper edge of standard deviation and bottom edge) and if i use the classic code legend('line1','line2,'...) I'll have 3 label. I want in the legend only the mean for each group.
I have 2 groups.
shadedErrorBar(x,y1,e1);
shadedErrorBar(x,y2,e2);
y1 and y2 are my means, while e1 and e2 standard deviations.
I think I should use IconDisplayStyle but I don't understand how. I read this http://it.mathworks.com/help/matlab/creating_plots/controlling-legends.html but is like hide a plot to hide a line. With shadedErrorBar I have a graph with 3 plot on it for each group.

When you create the legend, you can specify only the plot objects that you would like to create legend entries for. The output of shadedErrorBars is a struct containing all of the plot objects so you can use these to grab the patch objects and create legend entries for only those.
h1 = shadedErrorBar(linspace(1,10), linspace(1,10), linspace(0,1));
hold on
h2 = shadedErrorBar(linspace(1,10), linspace(1,20), linspace(1,0));
legend([h1.patch, h2.patch], {'Item1', 'Item2'})

Related

How to plot contours with selected colors and formatted labels

I'm trying to plot contour using my computed data with limited contour labels and and colors as given in the top panel of this image:
But I ended up with a slightly different plot (see the plot in the bottom of the above image).
I want to modify my plot with the following three specifications
Restrict contour labels in 2 or 3 decimal places
Remove plot labels in the area where the contours are too close to each other.
Plot with two colors as in the first image
Here is my code:
f=load('fort.15');
ngridx=180;
ngridy=180;
x=f(:,3);
y=f(:,4);
z=f(:,5);
xlin=linspace(min(x),max(x),ngridx);
ylin=linspace(min(y),max(y),ngridy);
[X,Y]=meshgrid(xlin,ylin);
Z=griddata(x,y,z,X,Y,'linear');
[c,h] = contour(X,Y,Z,20);
set(h,'LineWidth',2,'LineColor',rgb('SteelBlue'),'ShowText','on',...
'LabelSpacing',800 )
axis([0 6 -5 7])
I'm not an expert in Matlab. Please help me get the right plot.
I'm attaching my data file here.
Well, I got only 2 of 3. Deine the level in which the color has to change (here scl) and you good to go:
scl = 6.5; % switch color level;
[c1,h1] = contour(X,Y,Z,scl:max(Z(:)),'Color','r');
hold on
[c2,h2] = contour(X,Y,Z,min(Z(:)):scl,'Color','b');
clabel(c2,h2);
axis([0 6 -5 7])
The idea here is to builed your plot from two contour objects, using hold on command. the vector scl:max(Z(:)) define the levvels to show in the first contour, and the get the red color and no lables. And a similar logic works for the secound contour.
If you want to lable some red contours, or remove lables from the blue ones, you need to replace h2 in the clabel function with a vector of the levels you want to lable. If you will be mo specific in the comments I'll update my answer.
Changing the formatting of the lables, is probably possible somehow, but it's really not trivial, so I left it by now.

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

Bar chart matlab by scalar vectors

I am trying to create a bar chart where my x axis shows only one data point, year of 2006, but for 3 different data related to 2006. I am doing the following, but I receive the
error of "X must be same length as Y."
Here is my code and I highly appreciate your help:
X=[2006] %year
% Create data for childhood disease cases
measles = [38556];
mumps = [20178];
chickenPox = [37140];
% Create a vertical bar chart using the bar function
figure;
bar(X, [measles' mumps' chickenPox'],'group');
% Set the axis limits
%axis([0 13 0 40000]);
% Add title and axis labels
title('Childhood diseases by month');
xlabel('Month');
ylabel('Cases (in thousands)');
% Add a legend
legend('Measles', 'Mumps', 'Chicken pox');
You are getting this error because X is only one element and you are using three different Y values.
I think this problem requires you to take a step back a bit. Why are you plotting all three against the same X value? If this worked as you specified, all three would be on top of each other.
Would it not be more informative to the viewer if the X-axis labeled each bar as measles, mumps or chickenPox?
Try:
bar([measles' mumps' chickenPox']);
set(gca,'XTickLabel',{'Measles','Mumps','Chicken Pox'});
Edit:
I see what you're after. You're almost correct. The problem is that since you only have one column of data, matlab assumes you DON'T want to create a group but just plot those 3 values against unique X values. So we need to trick it slightly by creating another data point.
% //Create two columns, one of them zeros
data = [[measles;mumps;chickenPox],[0;0;0]];
bar([X,X+1],data'); % //The 2007 entry will not be visible
This will create a group at 2006 and the 2007 entry will be zeros so you won't see it. You can then set your axes and such to be the same as the other charts.

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:

Grouping bar3 plots like bar

I'm wondering if it is possible to get the same grouping behavior of bar plots into bar3.
For example, if you plot
bar(rand(3));
you get 3 bars for each point; bar groups the different y values for each x. Now I would like to do the same with a 3D data. That is that I have several slices of 2D data that I want to visualize as groups of bars. Then, if my data is
data = rand(3,3,2);
I would like to see data(1,1,:) as a group of bars, and data(1,2,:) as another group and so on.
Is it possible? I cannot find a way of achieving this.
Edit: I'm adding more details, to explain it better.
Lets said that we have two, or more, sets of data {x_(i,j)^s}. What I need is to group in the same grid position (i,j), all the sets s. In this question, they are grouping the data sets side by side, not element-wise, like this:
x1(s1) x1(s2) x1(s3) x2(s1) x2(s2) x2(s3) x3(s1) x3(s2) x3(s3)
x4(s1) x4(s2) x4(s3) x5(s1) x5(s2) x5(s3) x6(s1) x6(s2) x6(s3)
x7(s1) x7(s2) x7(s3) x8(s1) x8(s2) x8(s3) x9(s1) x9(s2) x9(s3)
I would like the bar command behavior, it tends to group when putting more than one data set. I would like to know if it is possible.
I am not sure I fully understood, but if you are looking for grouped behavior like you mention with bar(rand(3)) then you can try
figure; bar3(rand(5),'grouped');
% or maybe
figure; bar3(rand(5),'stacked');
or try to rearrange the data in data so it can work better with bar3 with reshape:
data = rand(3,3,2);
% now each data(i, j, :) will be in single row
changeddata = reshape(data , [size(data , 1)*size(data , 2) 2]);
figure; bar3(changeddata);
figure; bar3(changeddata ,'grouped');
figure; bar3(changeddata ,'stacked');
Maybe if you can give a code example for how it's supposed to look in one group, it would help to understand your question better.
If you want to group bars in 3D bar plots, but you are happy o small groups (let's say 2 or 3 bars each group) then you can simply take advantage of the Y argument in bar3:
BAR3(Y,Z,WIDTH)
so you specify the location of the two groups of bars with two shifted Y vectors.
example:
bar3(0:3:9,rand(4,4),0.3)
hold on
bar3(1:3:10,rand(4,4),0.3)
then you can edit the label the way it suits you.