Matlab - Colour markers in plot according to criteria - matlab

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,

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.

Filling markers when plotting in a loop

I am trying to plot 31 different vectors in a single figure in MATLAB. I am using a circular marker 'o', for plotting every vector and I want to have every vector painted in a different color and also I want to fill the markers as well with the same color as the markers edges.
I am using the following code:
while (n<=31)
plot(x(n),y(n),'o',rand(1,3)) % Not filled markers
n=n+1;
end
The problem is that since I am using a random for the color selection, when I try to run the following code:
while (n<=31)
plot(x(n),y(n),'o','MarkerFaceColor',rand(1,3)) % Filled markers
n=n+1;
end
The marker edge and the marker filling have different colors. I don't know how to fix this, maybe I shouldn't use the random for selecting the color but I don't know how to fix it, to obtain the same color in the marker edge and in the filling.
I would recommend using scatter for this rather than creating n different plot objects that you then have to manage. Using the CData property it is possible to set a separate color for each of your data points.
colors = rand(31, 3);
x = rand(31,1);
y = rand(31,1);
s = scatter(x, y, 'filled', 'CData', colors);
'MarkerEdgeColor' is the property you want, in addition to FaceColor
while (n<=31)
c = rand(1,3);
plot(x(n),y(n),'o','MarkerFaceColor', c, 'MarkerEdgeColor', c);
n=n+1;
end

How to setup step colorbar in matlab?

I want to change the default color bar (jet color) generated by Matlab, especially the step of the color (just like the figure below). How to do that?
Here's my code
[hC hC] = contourf(interp2(sal,2,'spline'),[0:0.5:5]);
set(hC,'LineStyle','none','YTick',0:4);
colorbar;
If you are looking to reduce the number of colors in the contour plot and colorbar then you can set a new colormap with a reduce color set.
%Get 10 colors from jet
numColors = 10;
colormap(jet(numColors))
data = peaks;
contourf(data)
% Optionally you can set yTicks in conjunction with the number of items in your colormap to line up
colorbar('YTick',linspace(min(data(:)),max(data(:)),numColors+1))
EDIT:
If you want more control over where the contour lines are drawn then use the function in this form countourf(data,v) where v is an monotonically increasing vector of your desired contour levels. Example:
contourf(data,linspace(-7,8,numColors))
c = colorbar('YTick',linspace(-7,8,numColors+1));
The will draw 10 contour lines at -7, -5.33, -3.66 ... 8. Replace -7 and 8 with whatever you wish ex. min/max of data or whatever makes sense for your application
You can adjust the color bar properties by using:
c=colorbar;
c.Ticks=[vector of tick locations]
or alternately you could try
c.Limits=[min max]
See the MATLAB documentation for colorbar properties: http://www.mathworks.com/help/matlab/ref/colorbar-properties.html?refresh=true
this explains color bar customization in more detail

plotting a text file with 4 columns in matlab

I want to plot a text file with 4 columns that first column in longitude,second in latitude, third is depth and forth is amount of displacement in each point.(it's related to a fualt)
-114.903874 41.207504 1.446784 2.323745
I want a plot to show the amount of displacement in each point (like images that we plot with imagesc),unfortunately "imagesc" command doesn't work for it.
how can I plot it?
Thanks for your attention
A simple way would be to use scatter3 and assign your displacements to be the colours. Note that you have to supply a size for this to work - I'm using [] (empty matrix) which will set it to default. If your four sets of values are four vectors of the same size, then it's just something like:
scatter3(lat,lon,depth,[],displacement, 'filled')
Values in displacement will be linearly mapped to the current colormap. 'filled' gives you filled markers rather than open ones (default marker is a circle but can be changed).
You can plot each point using plot3(longitude,latitude,depth). You can color each point according to the displacement in a for loop. The easiest way to do this is create a colormap, e.g. using jet and chosing the color according to the displacement.
figure;
hold on;
cmap = jet(256);
dispRange = [min(displacement),max(displacement)];
for k=1:size(longitude,2)
c = cmap(1+round(size(cmap,1)*(displacement(k)-dispRange(1))/dispRange(2)),:);
plot3(longitude(k),latitude(k),depth(k),'o', ...
'MarkerEdgeColor',c,'MarkerFaceColor',c);
end

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: