This question already has answers here:
3D scatter plot with 4D data
(2 answers)
Closed 7 years ago.
I have a an nx4 matrix where each row is an observation.
The three three columsn represent by variables, and the fourth a 'fitness' parameter.
I would like to show this in a 3D scatter plot, where each axis is one of my variables and then color each point depending on how close to one of the extremes in my fourth column it is.
For example, say fitness ranged between 0 and 1. I would want observations with fintess 0 to be blue, those with fitness 1 to be red and those in-between some corresponding shade.
Any advice on how best to do this?
Thanks!
The function scatter3 has a color input argument. But you need to define the size of the markers also.
% Generate example data,
X=rand(10,1)*10;
Y=rand(10,1)*3;
Z=rand(10,1)*5;
fit=rand(10,1)*3+10;
scatter3(X,Y,Z,ones(size(X))*40,fit,'fill')
Use scatter3 with an appropriate colormap:
scatter3(data(:,1), data(:,2), data(:,3), 10, data(:,4), '*')
colormap(hsv)
colorbar
where
data is your matrix
10 is markersize
'*' is the marker shape
hsv is the selected colormap
Related
This question already has an answer here:
Density plot in Matlab
(1 answer)
Closed 6 years ago.
Mathematica's ListDensityPlot[] can take a list of 3D coordinates, where the 3rd coordinate is a scalar, call it temperature or concentration etc. and return a density plot. The list would look something like
temperature={{0,0,20},{0,1,25},...,{9,9,35}}
in Mathematica notation.
Mathematica automatically interpolates (I guess) over the values so it returns a continuous, smooth picture with a color scale to describe the scalar values on the 2D plane.
Is there anything equivalent to this in Matlab?
Thanks
You can use the Delaunay triangulation function of your planar points, then generate a surface over the triangulation.
It would look something like this:
Points=[xpoints, ypoints, zpoints];
tri=delaunay(Points(:,1), Points(:,2));
figure
trisurf(tri, Points(:,1), Points(:,2), Points(:,3))
shading interp
colormap jet
colorbar EastOutside
I have several thousands of points to plot (about 10k) and I would like to plot them with Matlab but deciding a diferent size for each of the points (and a different color if possible). I tried to make a scatter plot for each point, but it is extremely slow compared to a single scatter call for all the points. Is there a way to plot several points in Matlab with different properties for each point, that works in a reasonable amount of time?
In case it is not possible to do it with Matlab, is there a way to do it with gnuplot?
scatter(x, y, a, c) takes arguments x and y, and then a for size, and c for colour. a can either be a single scalar, or a vector with a size for each (x,y) point. c can be an RGB triplet, or a vector, the same size as x and y. For example:
x = 1:4;
scatter(x, x, 10*x, x);
results in
So in your case, perhaps
scatter(xData, yData, [], 1:10000)
will result in your data having a different colour determined by its position in the data array.
For gnuplot it's easy, suppose you write your datafile with 3 columns, all you have to do is
plot 'data.dat' u 1:2:3:3 with circles lc palette
HERE you can find some examples (for help type help circles).
If you want just what is called variable pointsize (pointsize is not related to the real axis) you can use:
plot 'data.dat' with points ps variable pt 7
HERE you can find some examples (for help type help pointsize).
For gnuplot you can combine pointsize variable and linecolor variable or linecolor palette:
set xrange [0:10]
set samples 21
plot '+' using 1:1:(0.2*$1):1 with point pointsize variable linecolor palette pt 7 notitle
When I use spy to check a sparsity pattern, it doesn't distinguish certain elements from others. Is there any way to do this? Say, for example, elements that are equal to 10 are red and all elements equal to 9 are blue. Can I get this in one spy plot?
I've only been able to change the size and style of the plot points.
Here is how you can do it:
spy(a,'k')
hold on
spy(a==10,'r')
spy(a==9,'b')
hold off
Another way is to use scatter instead of spy :
[x,y] = find(a);
clr = a(a~=0);
scatter(x,y,[],clr)
set(gca,'YDir','rev')
In this case the points will be colored by a values according to current figure colormap.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
matlab: scatter plots with high number of datapoints
I have 3 vectors of 315,000 elements each. X,Y, and Z. X & Y are coordinates and Z is a value. I must plot the coordinates as points in a 2D graph, the Z is a color indicator at each coordinate of X and Y. I've tried the "scatter" command, but it extremely slow. Would anybody suggest a better way?
thanks!
Depending on what kind of color map you are looking for, you can try something like
zmin=min(Z);
zmax=max(Z);
map=colormap;
color_steps=size(map,1);
hold on
for i=1:color_steps
ind=find(Z<zmin+i*(zmax-zmin)/color_steps & Z>=zmin+(i-1)*(zmax-zmin)/color_steps);
plot(X(ind),Y(ind),'o','Color',map(i,:));
end
The finding is a little expensive but it seems to be quicker than scatter. I'm sure you could optimize this further.
Try cline from MATLAB file exchange here. It looks like it does exactly what you want.
Your code is slow because of the large size of the vectors, not because of the SCATTER function. Try breaking them down into vectors of smaller size (say, 10 elements each) and putting each vector into a cell of a cell array. Then loop through the cell array and scatter each smaller vector individually to avoid loading too much into the memory.
hold on
for i=1:numel(XcoordCellArray):
scatter(XcoordCellArray{i},YcoordCellArray{i},S,ZcoordCellArray{i})
end
This question already has answers here:
Plotting 3-D Matrix *values* in MATLAB
(2 answers)
Closed 7 years ago.
Ive created a 3d matrix in MATLAB. The values of the matrix are the velocity at that point in a rectangular section. I would like a plot with colours showing the values at each position, is this possible?
Phrasing this another way, I have a matrix of size 100x100x200. Id like a graph that has 100x100x200 points and the colour of each of those points is related to its value.
This question is very similar to this question. You might want to check it out.
UPDATE:
Suppose you have a 3D matrix A:
A = rand(100,100,200);
You want to plot each entry of A mapped to a color at its 3D coordinates. First generate the coordinates:
[x,y,z] = meshgrid(1:100,1:100,1:200);
Now you are ready to use scatter3:
scatter3(x(:),y(:),z(:),5,A(:))
Here the : indexing vectorizes the coordinates column-wise.
Hope this helps.