Change colour of points in plot3 with increasing z value

조회 수: 36 (최근 30일)
Mahi Nazir
Mahi Nazir 2016년 2월 10일
댓글: Voss 2024년 10월 14일
Hi I have a set of x values, set of y values and for each x,y I have a z value. I have to plot them as shown in the attached figure. I am using plot3 to plot that graph. I want to change the colour of the points according to the value of z. The more the value of z, more is the intensity. Any help will be appreciated.

답변 (2개)

Mike Garrity
Mike Garrity 2016년 2월 10일
No, plot3 only supports a single color. You can use other graphics functions. The "traditional" one for this purpose is actually rather surprising. It's the patch function, which is designed for drawing filled polygons.
Consider this example:
x = cos(theta);
y = sin(theta);
z = theta;
plot3(x,y,z)
I can draw it colored by Z like this:
cla
patch([x nan],[y nan],[z nan],[z nan],'EdgeColor','interp','FaceColor','none')
What's going on here is the following:
  • The 4th arg says to use Z for color data
  • The EdgeColor=interp says to interpolate the color data as the edge color
  • The FaceColor=none says to not fill the polygon. Just draw the edges
  • The nans say not to connect the last point to the first point.
There are a couple of other options, but that's probably what most old-school MATLAB programmers use.
  댓글 수: 1
Mahi Nazir
Mahi Nazir 2016년 2월 10일
Thanks Mike. I want discrete data points in z, so I use plot3(x, y, z, '*') How will I get these discrete points using patch?

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2016년 2월 10일
Based on the picture, which did not have lines connecting the points, I think SCATTER3 is the function you're looking for.
  댓글 수: 4
John Dzielski
John Dzielski 2024년 10월 14일
Hi,
This example does not work in 2023b. The syntax [x nan] now produces an error. I tried using [x, nan(size(x))] and that produces an almost correct plot. The feature that is missing is that the first and last points are connected. Thanks.
Voss
Voss 2024년 10월 14일
@John Dzielski: It could be that your x, y, and z are column vectors (instead of row vectors as in the example), in which case you'd use the syntax [x; nan] to vectically concatenate x with a NaN (instead of [x nan] to horizontally concatenate x with a NaN)
theta = linspace(0,4*pi,100).';
x = cos(theta);
y = sin(theta);
z = theta;
whos x y z % column vectors
Name Size Bytes Class Attributes x 100x1 800 double y 100x1 800 double z 100x1 800 double
patch([x; nan],[y; nan],[z; nan],[z; nan],'EdgeColor','interp','FaceColor','none')
view(3)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by