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

Workaround for Plot[ ] and color via PlotStyle

44 views
Skip to first unread message

Christopher O. Young

unread,
Apr 28, 2011, 6:34:16 AM4/28/11
to
As a previous poster showed, Plot is a little inconsistent when it comes to
mapping colors via PlotStyle.

Plot[
{
a x /. {a -> 1},
a x^2 /. {a -> 1},
a x^3 /. {a -> 1}
},

{x, 0, 2},

PlotStyle -> {Red, Green, Blue}
]


works OK but

Plot[
{
a x,
a x^2,
a x^3
} /. {a -> 1},

{x, 0, 2},

PlotStyle -> {Red, Green, Blue}
]


just produces blue plots.

So I tried to figure out to work from the fully written-out form:

Show[
Plot[y = x, {x, -5, 5}, PlotStyle -> {Thick, Red}],
Plot[y = x^2, {x, -5, 5}, PlotStyle -> {Thick, Yellow}],
Plot[y = x^3, {x, -5, 5}, PlotStyle -> {Thick, Blue}]
]


The above works fine, but is very redundant.

A Table will streamline the above commands:

Show[
Table[
Plot[
y = x^k, {x, -5, 5},
PlotStyle -> {Thick, Hue[0.5 (k - 1)/3]}
],
{k, 1, 3}]
]


Does anybody know how to go through a list of colors via the k index?

At any rate, if you're a big fan of postfix notation, the following can be
rewritten in stages, as:

Table[
Plot[
y = x^k, {x, -5, 5},
PlotStyle -> {Thick, Hue[0.5 (k - 1)/3]}
],
{k, 1, 3}
] // Show


Plot[
y = x^k, {x, -5, 5},
PlotStyle -> {Thick, Hue[0.5 (k - 1)/3]}
]~Table~{k, 1, 3}\
// Show

And, finally:

(y = x^k //
Plot[
#, {x, -5, 5},
PlotStyle -> {Thick, Hue[0.5 (k - 1)/3]}
] &)\
~Table~{k, 1, 3}\
// Show

Christopher O. Young

unread,
Apr 28, 2011, 6:34:58 AM4/28/11
to
I figured out how to add a coordinated list of colors. Just use
part-specification on a color list. Of course, this could be a list of RGB
specifications, etc.

(y = x^k //
Plot[
#, {x, -5, 5},

PlotStyle -> {Thick, {Red, Yellow, Blue}[[k]]}

Bob Hanlon

unread,
Apr 29, 2011, 7:30:28 AM4/29/11
to

This is a FAQ. As stated in the More Information for Plot:

- Plot has attribute HoldAll, and evaluates f only after assigning specific numerical values to x.

- In some cases it may be more efficient to use Evaluate to evaluate f symbolically before specific numerical values are assigned to x.

Attributes[Plot]

{HoldAll, Protected}

Plot[Evaluate[{a x, a x^2, a x^3} /. {a -> 1}], {x, 0, 2},


PlotStyle -> {Red, Green, Blue}]

You can also factor the a out

Plot[Evaluate[a {x, x^2, x^3} /. {a -> 1}], {x, 0, 2},


PlotStyle -> {Red, Green, Blue}]


Show[Table[Plot[y = x^k, {x, -5, 5},


PlotStyle -> {Thick, Hue[0.5 (k - 1)/3]}],

{k, 1, 3}],
PlotRange -> {-10, 10}]

Using a single Plot

Plot[Evaluate[x^Range[3]], {x, -5, 5},
PlotStyle -> ({Thick, #} & /@ {Red, Yellow, Green}),
PlotRange -> {-10, 10}]


Bob Hanlon

---- "Christopher O. Young" <cy...@comcast.net> wrote:

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


works OK but


just produces blue plots.

{k, 1, 3}
] // Show

Plot[
y = x^k, {x, -5, 5},
PlotStyle -> {Thick, Hue[0.5 (k - 1)/3]}

]~Table~{k, 1, 3}\
// Show

And, finally:

(y = x^k //
Plot[
#, {x, -5, 5},

PlotStyle -> {Thick, Hue[0.5 (k - 1)/3]}

DrMajorBob

unread,
Apr 29, 2011, 7:31:10 AM4/29/11
to
Plot[{a x, a x^2, a x^3} /. {a -> 1} // Evaluate, {x, 0, 2},

PlotStyle -> {Red, Green, Blue}]

Bobby


--
DrMaj...@yahoo.com

DrMajorBob

unread,
Apr 29, 2011, 7:31:42 AM4/29/11
to
Heroic, I suppose... but awkward.

Bobby

On Thu, 28 Apr 2011 05:35:06 -0500, Christopher O. Young
<cy...@comcast.net> wrote:

> I figured out how to add a coordinated list of colors. Just use
> part-specification on a color list. Of course, this could be a list of
> RGB
> specifications, etc.
>

> (y = x^k //
> Plot[
> #, {x, -5, 5},

> PlotStyle -> {Thick, {Red, Yellow, Blue}[[k]]}

0 new messages