Discussion:
ListPlot ColorFunction
(too old to reply)
Serych Jakub
2009-04-27 09:25:30 UTC
Permalink
Dear Mathematica users,
first let me thank you for many nice zigzag solutions from my previous post.

Today I have another question. I need to highlight some data points in
ListPlot by changing their color. How to do it? I tried the similar solution
which somebody have sent me time ago for the same problem which I had with
Plot, but it doesn't work with ListPlot.

In the example below I'm trying to make the zero points Red and all other
Blue.

ListPlot[{9, 4, 1, 0, 0, 0, 1, 4, 9}, Filling -> Axis,
PlotStyle -> PointSize[0.02],
ColorFunction -> (If[# == 0, RGBColor[1, 0, 0], RGBColor[0, 0, 1]] &)]


Thanks in advance for any help

Jakub
Jens-Peer Kuska
2009-04-28 08:43:03 UTC
Permalink
Hi,

ListPlot[#, Filling -> Axis, PlotStyle -> PointSize[0.02],
Epilog -> (If[#[[2]] === 0, {PointSize[0.02], RGBColor[1, 0, 0],
Point[#]}, {}] & /@ MapIndexed[Append[#2, #1] &, #])] &[{9,
4, 1, 0, 0, 0, 1, 4, 9}
]

?

Regards
Jens
Post by Serych Jakub
Dear Mathematica users,
first let me thank you for many nice zigzag solutions from my previous post.
Today I have another question. I need to highlight some data points in
ListPlot by changing their color. How to do it? I tried the similar solution
which somebody have sent me time ago for the same problem which I had with
Plot, but it doesn't work with ListPlot.
In the example below I'm trying to make the zero points Red and all other
Blue.
ListPlot[{9, 4, 1, 0, 0, 0, 1, 4, 9}, Filling -> Axis,
PlotStyle -> PointSize[0.02],
ColorFunction -> (If[# == 0, RGBColor[1, 0, 0], RGBColor[0, 0, 1]] &)]
Thanks in advance for any help
Jakub
M.Roellig
2009-04-28 08:43:35 UTC
Permalink
Dear Jakub,

I haven't found a way to assign different styles to selected points
from a list. But
you can assign a seperate PlotStyle to seperate lists. I split your
data lists into one list
per data point:

dat = {9, 4, 1, 0, 0, 0, 1, 4, 9};
lst = Partition[Table[{i, dat[[i]]}, {i, 1, ***@dat}], 1];
clrs = Table[
If[dat[[i]] == 0, {Blue, PointSize[0.02]}, {Red,
PointSize[0.04]}], {i, 1, ***@dat}];

ListPlot[lst, PlotStyle -> clrs]


Cheers,

Markus
Post by Serych Jakub
Dear Mathematica users,
first let me thank you for many nice zigzag solutions from my previous po=
st.
Post by Serych Jakub
Today I have another question. I need to highlight some data points in
ListPlot by changing their color. How to do it? I tried the similar solut=
ion
Post by Serych Jakub
which somebody have sent me time ago for the same problem which I had wit=
h
Post by Serych Jakub
Plot, but it doesn't work with ListPlot.
In the example below I'm trying to make the zero points Red and all other
Blue.
ListPlot[{9, 4, 1, 0, 0, 0, 1, 4, 9}, Filling -> Axis,
PlotStyle -> PointSize[0.02],
ColorFunction -> (If[# == 0, RGBColor[1, 0, 0], RGBColor[0, 0, 1]]=
&)]
Post by Serych Jakub
Thanks in advance for any help
Jakub
m***@gmail.com
2009-04-28 08:43:56 UTC
Permalink
Post by Serych Jakub
Dear Mathematica users,
first let me thank you for many nice zigzag solutions from my previous post.
Today I have another question. I need to highlight some data points in
ListPlot by changing their color. How to do it? I tried the similar solution
which somebody have sent me time ago for the same problem which I had with
Plot, but it doesn't work with ListPlot.
In the example below I'm trying to make the zero points Red and all other
Blue.
ListPlot[{9, 4, 1, 0, 0, 0, 1, 4, 9}, Filling -> Axis,
PlotStyle -> PointSize[0.02],
ColorFunction -> (If[# == 0, RGBColor[1, 0, 0], RGBColor[0, 0, 1]]=
&)]
Post by Serych Jakub
Thanks in advance for any help
Jakub
ColorFunction only works with Joined->True. Also your index should
have been #2 instead of # in any case.

I'm sure there must be an easier way to do this, but this works:

list = Flatten@
MapIndexed[{If[#1 == 0, RGBColor[1, 0, 0], RGBColor[0, 0, 1]],
Point[Reverse[{#1, #2[[1]]}]]} &, {9, 4, 1, 0, 0, 0, 1, 4, 9}];

Graphics[{AbsolutePointSize[6], list}, Frame -> True]

Mike
Bob Hanlon
2009-04-28 08:44:17 UTC
Permalink
"ColorFunction requires at least one dataset to be Joined"

data = {9, 4, 1, 0, 0, 0, 1, 4, 9};

data2 = Transpose[{Range[Length[data]], data}];

ListPlot[List /@ data2,
Filling -> Axis,
PlotStyle -> ({PointSize[0.02],
If[#[[2]] == 0, Red, Blue]} & /@
data2)]


Bob Hanlon

---- Serych Jakub <***@panska.cz> wrote:

=============
Dear Mathematica users,
first let me thank you for many nice zigzag solutions from my previous post.

Today I have another question. I need to highlight some data points in
ListPlot by changing their color. How to do it? I tried the similar solution
which somebody have sent me time ago for the same problem which I had with
Plot, but it doesn't work with ListPlot.

In the example below I'm trying to make the zero points Red and all other
Blue.

ListPlot[{9, 4, 1, 0, 0, 0, 1, 4, 9}, Filling -> Axis,
PlotStyle -> PointSize[0.02],
ColorFunction -> (If[# == 0, RGBColor[1, 0, 0], RGBColor[0, 0, 1]] &)]


Thanks in advance for any help

Jakub
Sjoerd C. de Vries
2009-04-28 08:47:36 UTC
Permalink
Hidden in the Options section of the ListPlot documentation page
you'll find

"ColorFunction requires at least one dataset to be Joined"

Nevertheless, you can achieve what you want in a somewhat clumsy way
by having ListPlot plot nine one-point plots and specifying the color
for each in PlotStyle and FillingStyle (the PointSize directive goes
to BaseStyle):

data = {9, 4, 1, 0, 0, 0, 1, 4, 9};
ListPlot[
{#} & /@ Transpose[{Range[Length[data]], data}],
Filling -> Axis,
PlotStyle -> ((If[# == 0, RGBColor[1, 0, 0], RGBColor[0, 0, 1]] &) /@
data),
FillingStyle -> ((If[# == 0, RGBColor[1, 0, 0],
RGBColor[0, 0, 1]] &) /@ data),
BaseStyle -> PointSize -> 0.02
]

Cheers -- Sjoerd
Post by Serych Jakub
Dear Mathematica users,
first let me thank you for many nice zigzag solutions from my previous post.
Today I have another question. I need to highlight some data points in
ListPlot by changing their color. How to do it? I tried the similar solution
which somebody have sent me time ago for the same problem which I had with
Plot, but it doesn't work with ListPlot.
In the example below I'm trying to make the zero points Red and all other
Blue.
ListPlot[{9, 4, 1, 0, 0, 0, 1, 4, 9}, Filling -> Axis,
PlotStyle -> PointSize[0.02],
ColorFunction -> (If[# == 0, RGBColor[1, 0, 0], RGBColor[0, 0, 1]] &)]
Thanks in advance for any help
Jakub
Loading...