Hello everyone,
I have a matrix of 500x3 in which columns are x,y,z values of a single point respectively. I am plotting this with scatter3 function and use the Z values as the colormap. But now, I want the every Z value increase in some interval(for ex. from 5 to 6) but X and Y are constant for every point. So I used to have 500 points in the plot but now I want to have 500 lines instead of points(with constant x,y and changing z means line to me). I realized I can do that with plot3d but I cant use the Z values as colormap in that.
Is there any other way to do this?
Thank for you answers

 Akzeptierte Antwort

Adam Danz
Adam Danz am 21 Apr. 2020
Bearbeitet: Adam Danz am 21 Apr. 2020

0 Stimmen

plot3(x,y,z) will produce 1 line object for each column of the inputs. Each line object can have its own color. So, all you have to do is arrange the data properly.
h = plot3([x(:).'.*[1;1]], [y(:).'.*[1;1]]), [z1(:).', z2(:).'], '-');
set(h, {'Color'}, mat2cell(jet(numel(x)),ones(numel(x),1),3));
The syntax [x(:).'.*[1;1]] produces two rows of identical x values.
The mat2cell() syntax converts your color matrix to cell array of 1x3 values.

4 Kommentare

Umut Ege Ulucay
Umut Ege Ulucay am 21 Apr. 2020
Thanks a lot for your answer. I understood the plotting part but the coloring part does not work for me. May you explain further? or there might be a function in which I can just color every line with respect to the values I have and use colormap.
I have read from somewhere that, it is not possible to assign colors in plot3d. Further answers will be welcomed. Thank you a lot.
Adam Danz
Adam Danz am 21 Apr. 2020
Bearbeitet: Adam Danz am 24 Apr. 2020
"I have read from somewhere that, it is not possible to assign colors in plot3d."
My answer does exactly that. But the colors are assigned to line objects after they are plotted.
"I understood the plotting part but the coloring part does not work for me."
The 2 lines in my answer are a basic overview. They do not assign a color based on the z-value. Here's how to take that step.
1) Produce a colormap (there are lots of options, see documentation). I chose jet and I chose 100 values. You can choose any map and any number of values. It does not have to match the number of data points or lines.
cmap = jet(100);
2) normalize the z values to match each z value with a row of the color matrix.
zNorm = round((z - min(z(:)))/range(z(:)) * (size(cmap,1)-1)) + 1;
zNorm should span from 1 to 100 or however long your colormap is.
3) Assign the appropriate color value. Note that zNorm must have the same number of elements as the number of handles (h). isequal(size(h(:), zNorm(:))
% Convert color matrix to cell array of 1x3 vectors
cmapCell = mat2cell(cmap,ones(size(cmap,1),1),3);
set(h(:), {'Color'}, cmapCell(zNorm(:)))
Umut Ege Ulucay
Umut Ege Ulucay am 25 Apr. 2020
Sorry for late answer. It totally worked. Thanks a lot!
Adam Danz
Adam Danz am 26 Apr. 2020
Glad I could help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Color and Styling finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by