Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Need help with plot3

Subject: Need help with plot3

From: Ali Abolhasani

Date: 12 Dec, 2010 04:06:04

Message: 1 of 6

Dear all

I have a simple problem. I want to use plot3 to plot a curve in 3-dimension with 3 vectors X,Y and Z but also I want the line color change with respect to Z. In other words I'd like something similar to "meshgrid" for a curve.

Thank you in advance!

Ali

Subject: Need help with plot3

From: Walter Roberson

Date: 12 Dec, 2010 04:37:52

Message: 2 of 6

On 11/12/10 10:06 PM, Ali Abolhasani wrote:

> I have a simple problem. I want to use plot3 to plot a curve in
> 3-dimension with 3 vectors X,Y and Z but also I want the line color
> change with respect to Z. In other words I'd like something similar to
> "meshgrid" for a curve.

You cannot do it with a single plot3() call. Each continuous plot line
is constrained to be the same color.

One of the things you will have to decide is how you want to color lines
that connect different Z values that map to different colors. Should it
be according to the "first" Z value, according to the "bigger" Z value,
should it shade between the two colors?

If the line color can be determined by one of the two endpoints and
ignoring the other end point, then you can plot3() and "hold on" and
plot3() each of the individual line segments. If you are going to do
that, though, you might as well just line() the line segments into place.

If you need shading between the levels of the endpoints, you will have
to use patch() objects.

Subject: Need help with plot3

From: Ali Abolhasani

Date: 12 Dec, 2010 09:42:04

Message: 3 of 6

Dear Walter

Thank you for you result. But it seems that the patch() command is just useful for producing surface patches. For example this command connect the end of my curve to it's initial point to create a surface! But I need a curve with different colors at different Z coordinate. For example the color change linearly from red to blue in z-direction.

Ali

Walter Roberson <roberson@hushmail.com> wrote in message <AkYMo.3804$5U7.3059@newsfe03.iad>...
> On 11/12/10 10:06 PM, Ali Abolhasani wrote:
>
> > I have a simple problem. I want to use plot3 to plot a curve in
> > 3-dimension with 3 vectors X,Y and Z but also I want the line color
> > change with respect to Z. In other words I'd like something similar to
> > "meshgrid" for a curve.
>
> You cannot do it with a single plot3() call. Each continuous plot line
> is constrained to be the same color.
>
> One of the things you will have to decide is how you want to color lines
> that connect different Z values that map to different colors. Should it
> be according to the "first" Z value, according to the "bigger" Z value,
> should it shade between the two colors?
>
> If the line color can be determined by one of the two endpoints and
> ignoring the other end point, then you can plot3() and "hold on" and
> plot3() each of the individual line segments. If you are going to do
> that, though, you might as well just line() the line segments into place.
>
> If you need shading between the levels of the endpoints, you will have
> to use patch() objects.

Subject: Need help with plot3

From: Sadik

Date: 12 Dec, 2010 11:13:04

Message: 4 of 6

Hello Ali,

Just as Walter described above, you can something like the following:

t = 0:pi/200:10*pi;
x = sin(t);
y = cos(t);
z = t;
N = length(z); % = length(x) = length(y)
colors = jet(N);
for k = 1:N
    plot3(x(k),y(k),z(k),'Color',colors(k,:))
    hold on
end

Best.

Subject: Need help with plot3

From: Ali Abolhasani

Date: 12 Dec, 2010 11:53:05

Message: 5 of 6

"Sadik " <sadik.hava@gmail.com> wrote in message <ie2ao0$pvn$1@fred.mathworks.com>...
> Hello Ali,
>
> Just as Walter described above, you can something like the following:
>
> t = 0:pi/200:10*pi;
> x = sin(t);
> y = cos(t);
> z = t;
> N = length(z); % = length(x) = length(y)
> colors = jet(N);
> for k = 1:N
> plot3(x(k),y(k),z(k),'Color',colors(k,:))
> hold on
> end
>
> Best.

Dear Sadik

Thank you very much! It does work goood.

Ali

Subject: Need help with plot3

From: Ali Abolhasani

Date: 12 Dec, 2010 12:17:04

Message: 6 of 6

"Sadik " <sadik.hava@gmail.com> wrote in message <ie2ao0$pvn$1@fred.mathworks.com>...
> Hello Ali,
>
> Just as Walter described above, you can something like the following:
>
> t = 0:pi/200:10*pi;
> x = sin(t);
> y = cos(t);
> z = t;
> N = length(z); % = length(x) = length(y)
> colors = jet(N);
> for k = 1:N
> plot3(x(k),y(k),z(k),'Color',colors(k,:))
> hold on
> end
>
> Best.
Dear Sadik

Thank you very much! It does work goood.

Ali

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us