Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Define a function as a graphics directive?

3 views
Skip to first unread message

Porscha Louise McRobbie

unread,
Nov 18, 2009, 6:58:09 AM11/18/09
to
Hi,

I have a very simple contour plot:


ContourPlot[ z Exp[-Sqrt[z^2 + x^2]/2.], {x, -10, 10}, {z, -10, 10},
ContourShading -> None, Contours -> 15,
ContourStyle -> {{Black, Thick}}]


I do not want the plot shaded, but I want to color the contour lines
according to a function of z (e.g., Sign[z], where z>0 is black, z<0
is red). Can this be done within ContourStyle?

I tried putting a function definition for the color inside
ContourShading, but I get an error since it's not a graphics directive
(and it's my understanding that ColorFunction is only for shading the
regions between contours).

Thanks for any input!
Porscha

Bob Hanlon

unread,
Nov 19, 2009, 5:24:08 AM11/19/09
to

Show[
ContourPlot[z Exp[-Sqrt[z^2 + x^2]/2],
{x, -10, 10}, {z, -10, 0},
ContourShading -> None,
Contours -> 7,
ContourStyle -> {{Red, Thick}}],
ContourPlot[z Exp[-Sqrt[z^2 + x^2]/2],
{x, -10, 10}, {z, 0, 10},
ContourShading -> None,
Contours -> 7,
ContourStyle -> {{Black, Thick}}],
PlotRange -> {{-10, 10}, {-10, 10}}]


Bob Hanlon

---- Porscha Louise McRobbie <pmcr...@umich.edu> wrote:

=============

Sjoerd C. de Vries

unread,
Nov 19, 2009, 5:35:57 AM11/19/09
to
I don't know of an easy way to do this directly, but the following
workaround works for me.

Show[
Table[


ContourPlot[z Exp[-Sqrt[z^2 + x^2]/2.], {x, -10, 10}, {z, -10, 10},

ContourShading -> None, Contours -> {c},
ContourStyle -> {{Switch[Sign[c], 1, Black, -1, Red, 0, Blue],
Thick}}], {c, -1/2, 1/2, 1/10}]]

Cheers -- Sjoerd

On Nov 18, 1:58 pm, Porscha Louise McRobbie <pmcro...@umich.edu>
wrote:

Harrie Kraai

unread,
Nov 19, 2009, 7:25:02 AM11/19/09
to
Hi,

My quick solution would be this:

cpl = ContourPlot[


z Exp[-Sqrt[z^2 + x^2]/2.], {x, -10, 10}, {z, -10, 10},

ContourShading -> None, Contours -> 15, ContourStyle -> {Thick}];

cpl /. {tt_Tooltip :> Sequence[If[tt[[2]] > 0, Black, Red], tt]}

which makes use of the fact that the tooltip contains the value of the
contourline

Have fun,

HK

Harrie Kraai

unread,
Nov 19, 2009, 7:37:46 AM11/19/09
to
Hi,

My quick solution would be this:

cpl = ContourPlot[


z Exp[-Sqrt[z^2 + x^2]/2.], {x, -10, 10}, {z, -10, 10},

ContourShading -> None, Contours -> 15, ContourStyle -> {Thick}];

cpl /. {tt_Tooltip :> Sequence[If[tt[[2]] > 0, Black, Red], tt]}

which makes use of the fact that the tooltip contains the value of the
contourline

Have fun,

HK

fd

unread,
Nov 20, 2009, 6:39:15 AM11/20/09
to
On Nov 19, 11:37 pm, Harrie Kraai <hakr...@xs4all.nl> wrote:
> Hi,
>
> My quick solution would be this:
>
> cpl = ContourPlot[
> z Exp[-Sqrt[z^2 + x^2]/2.], {x, -10, 10}, {z, -10, 10},
> ContourShading -> None, Contours -> 15, ContourStyle -> {Thick}];
>
> cpl /. {tt_Tooltip :> Sequence[If[tt[[2]] > 0, Black, Red], tt]}
>
> which makes use of the fact that the tooltip contains the value of the
> contourline
>
> Have fun,
>
> HK
>
> Porscha Louise McRobbie wrote:
> > Hi,
>
> > I have a very simple contour plot:
>
> > ContourPlot[ z Exp[-Sqrt[z^2 + x^2]/2.], {x, -10, 10}, {z, -10, 10},
> > ContourShading -> None, Contours -> 15,
> > ContourStyle -> {{Black, Thick}}]
>
> > I do not want the plot shaded, but I want to color the contour lines =

> > according to a function of z (e.g., Sign[z], where z>0 is black, z<0=


> > is red). Can this be done within ContourStyle?
>
> > I tried putting a function definition for the color inside
> > ContourShading, but I get an error since it's not a graphics directive =

> > (and it's my understanding that ColorFunction is only for shading the =


> > regions between contours).
>
> > Thanks for any input!
> > Porscha

Hi Again..

Just forgot to suggest a solution that involves RegionPlot. You'll
need to work a few more details, but in essence it is


f[z_, x_] = z Exp[-Sqrt[z^2 + x^2]/2.];

lf = #1 < f[x, y] < #2 & @@@
Table[{ii, ii + 0.05}, {ii, -0.6, 0.6, 0.2}]


RegionPlot[lf, {x, -10, 10}, {y, -10, 10},
ColorFunction -> Function[{x, y}, Hue[f[x, y] + 0.5]],
PerformanceGoal -> "Quality", PlotPoints -> 100]


Best
F


fd

unread,
Nov 20, 2009, 6:50:01 AM11/20/09
to
Hi.. I was quite curious with your query..WRI should really introduce
a ContourColorFunction...

If you can express your equations in a ParametricPlot you can do what
you want.

Another way is to use StreamPlot, you'll have to define the vector
field of your function

f[z_, x_] = z Exp[-Sqrt[z^2 + x^2]/2.]


Then the vector field along which f[z,x] is constant

v[x_, y_] = {D[f[x, y], y], -D[f[x, y], x]}


and the stream plot with lines


StreamPlot[v[x, y], {x, -10, 10}, {y, -10, 10},
StreamColorFunction -> Function[{x, y, vx, vy, nxy}, Hue[f[x, y]]],
StreamStyle -> "Line"]


Many thanks for reminding me my undergrad calculus...

Best
F

0 new messages