Scatter with different colors - matlab

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]);

Related

Create plot between two points representing confidence intervals in R

I have a data frame similar to the one created in the example below:
Remove columns with zero values from a dataframe
Is it possible to create a whisker plot connecting these two points in R? I would want the variables on the x-axis and the points on the y-axis.

How to plot overlapping images with matlab

I have several dataset matrices x, y, andz, where z contains values at the positions x,y showing shifted (overlapping) parts of the same picture. x and y are rectangularly centered around different center positions for each dataset.
How can I combine the data in one plot using pcolor or similar? Note that it should be a rectangular plot in the end, but that not all data points are given due to the shift.
I now solved my own question by using the command hold on, which makes it possible to plot several times into the same figure. You just have to run it in between two plot commands.

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

MATLAB: Scatter plot with conditional coloring

I have a large data matrix with 5 columns. 5th column contains many zeros. I want to do scatter(data(:,4),data(:,5)) and set a different color for points/rows where value in 5th column is zero.
I will then draw scatter plot of different columns but with same condition i.e. different color where values in 5th column are zeros.
You can easily set the different color-flag as a fourth parameter in a function:
scatter(d(:,4), d(:,5), 7, d(:,5)==0);
Here d(:,4) and d(:,5) are the coordinates of points, 7 is the size of the point, and d(:,5)==0 is the color-flag (for different values of d(:,5), different colors are chosen).

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: