MATLAB scatter plots and legends - matlab

I have some data that I am looking to plot in a scatter plot. The data is gathered from two sensors, so I have channel1 and channel2. The sensors are placed in various locations, so, location1, location2 and so on, up to location 9.
The data looks something like this is the .csv file:
Channel1
Channel2
Location
4255
7513
1
4277
6978
1
...
...
...
975
7510
2
552
6587
2
...
...
...
2301
2145
3
The code below seems to plot the colours for the groups. Is this correct?
scatter(Channel1,Channel2,[], location)
But I am not able to get the legend correct to show which colour relates to which location, and I hope someone can help.

Use the legend function. Pass a list of strings as arguments to the legend function, with each string corresponding to the label for each location. The order of the strings should match the order of the groups in the scatter plot.
https://www.mathworks.com/help/matlab/creating_plots/add-legend-to-graph.html
legend('Location 1', 'Location 2', 'Location 3', ... )
You can also use the gscatter function in MATLAB to create a scatter plot with multiple groups and a legend. The gscatter function is similar to scatter, but it allows you to specify different colors and symbols for each group.

Related

Matlab: Boxplot with individual value markers

How can I produce boxplots that also show all the values by themselves?
e.g. like here in subfigure A and B of this:
I can just plot them after a "hold on" but then they are not nicely arranged according to the x-axis as in the example above. Isn't there a matlab standard function for that?

Matlab - Colour markers in plot according to criteria

I have a quick question on a matlab plot. I want to draw a line with round markers on each point, but I want the markers to have a different color according to some criteria. I managed to display on the markers the different criteria but I was not able to change their colours.
To be more specific on the code below:
d3 is a (1x240) vector of y values for the plot.
RiskierInd is a (1x240) matrix with the criteria (it has values from
1 to 12).
What I want is the markers with criteria 1 to be in one colour, markers with criteria 2 to be with another colour, and so on and so forth.
The code below plots the line with the markers and with the criteria inside the markers.
% plot the data
figure
d3 = vals;
n = 1:numel(d3);
plot(n,d3, '-ob','markersize',10,'markerfacecolor','w');
for idx = 1:numel(d3)
text(n(idx),d3(idx), num2str(RiskierInd(idx)),...
'FontSize',8,...
'HorizontalAlignment','center');
end
I did check this post which is similar, but couldn't figure out how to implement it.
Also, is it possible to add a legend with the color of the markers afterwards?
You can use scatter instead of plot for this. You can replace
plot(n,d3, '-ob','markersize',10,'markerfacecolor','w');
with
hold on
plot(n, d3,'b-');
scatter(n, d3, [], RiskierInd, 'filled');
caxis([1 12]);
Then, to display the correspondence between the colors and the values you can simply add colorbar to your code.
Edit If you want to define custom colors, you can use colormap with a custom n-by-3 array of RGB colors. For instance, to have exactly 12 colors you can do:
colormap(jet(12));
Best,

Erratic behavior of Matlab's legend function

I was working on a MATLAB project and found some strange behavior of the legend function. Here is the code to reproduce the issue:
X=rand(3,100);
a=rand(3,100);
b=rand(3,100);
figure(1); hold on;
plot(0.17663,'m');
plot(1.223,'y');
plot(X,a,'r');
plot(X,b,'g');
legend({'s1','s2','a','b'});
My question is: The legend in the picture shows the same color for plot 3 and plot 4. Instead it should show red and green respectively. Is there something wrong with legend?
The commands plot(X,a,'r') and plot(X,b,'g') are not what you might expect. As X is a 3-by-100 array they will plot 100 lines, each consisting of 3 points, start, middle, end. The legend entries will correspond to each single line, so you should expect 100 red legend entries. You will see different behavior if you pass transposed arrays: plot(X.',a.','r'). This will plot 3 lines, each consisting of 100 points.

Plotting a graph using matlab

I'm having problem with plotting a graph using data of a .dat file. Can someone help me out here?
The file is saved as /My Documents/filename.dat and has three columns of numbers representing variables X, Y and Z.
(X is from 45 to 90 with variation of .5
Z is from .4 to .95 with variation of .05
Y is the result of these 2 variables.)
Example: (a part of data in .dat file)
48.000000 -0.000010 0.400000
48.500000 -0.000004 0.400000
49.000000 0.000003 0.400000
49.500000 0.000010 0.400000
50.000000 0.000016 0.400000
I want to plot a 2d graph of X and Y, and color should change according to the Z variable.
Use textscan to read your data:
fid = fopen('/My Documents/filename.dat');
data = textscan(fid, '%f %f %f');
fclose(fid);
X=data{1};
Y=data{2};
Z=data{3};
Plot method #1
Then if you plot with
plot(X,Y)
you'll get an ordinary plot consisting of lines. However these all have the same color. Varying them is not possible unless you split them up in separate lines.
Luckily, there is also the scatter function which allows you to do this:
scatter(X,Y,[],Z)
This plots the points, with color based on Z.
Plot method #2
If you want the points to be connected with lines also having varying color, you'll have to plot them as separate lines, and providing color to each line separately:
plot([X(1:end-1)' ; X(2:end)'], [Y(1:end-1)' ; Y(2:end)']);
The lines now have default coloring, it becomes a bit a hussle to get the right colors in however, next up is an example. Unfortunately I don't now of any way to input the colors also in such a one-liner, so we'll have to loop.
Ncolors=10;
zmin=min(Z);zmax=max(Z);
dz=max((zmax-zmin)/Ncolors,eps);
clr_map=jet(Ncolors);
clr_ids=min(floor((Z(1:end-1)-zmin)/dz)+1,Ncolors);
figure;hold on;
for ii=1:numel(X)-1
plot([X(ii) X(ii+1)], [Y(ii) Y(ii+1)],'color',clr_map(clr_ids(ii),:))
end
All lines now have colors based on one of their end points.
To add a colorbar, use colorbar, weird huh? But of course, the labels of that bar are referring to the colororder. Luckily, we can change them:
colormap(clr_map);
h_cb=colorbar;
set(h_cb,'yticklabel',arrayfun(#num2str,linspace(zmin,zmax,numel(get(h_cb,'ytick'))),'uni',false));
Change Ncolors to use more/less resolution in coloring the lines.
Probably overkill: you can also change the number of labels on the colorbar, the following changes it to 10:
colormap(clr_map);
h_cb=colorbar;
set(h_cb,'ytick',linspace(1,Ncolors,10));
set(h_cb,'yticklabel',arrayfun(#num2str,linspace(zmin,zmax,10),'uni',false));
or now with the labels having only 2 decimals:
set(h_cb,'yticklabel',arrayfun(#(yi) sprintf('%.2g',yi),linspace(zmin,zmax,10),'uni',false));
Plot method #3
Another last method is to use patches (which are slower), which is explained for the 3d case here, so you can get started there if you want.

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: