Message Boards Message Boards

0
|
752 Views
|
2 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Assigning colors to individual points in ListPlot

Posted 4 months ago

I have a list of random data points which I would like to plot using ListPlot. How can I assign a specific color to each of the individual data points where the assigned color depends upon some property of the particular data point? As a simple example, how can I have a positive valued data point plot Green and a negative valued data point plot Red? Basically, I want to assign an individual plot style to each data point which depends upon the value of the data point itself.

Thank you very much for your thoughtful advise.
It is greatly appreciated.

POSTED BY: Kerry Litvin
2 Replies
Posted 4 months ago

One way, using Style and Which:

pts = Range[-5, 5];
color[n_] := Which[n < 0, Red, n > 0, Green, True, Black]

ListPlot[Table[Style[pts[[i]], color[pts[[i]]]], {i, Length[pts]}]]
POSTED BY: Hans Milton
Posted 4 months ago

Use Select to extract each category of points and then overlay plots of the two categories using Show. You can do that with any number of properties, selections and colors.

yourlist={{-1,3},{2,4},{-2,2},{3,1}};
Show[
  ListPlot[Select[yourlist,#[[1]]<0&],PlotStyle->Red],
  ListPlot[Select[yourlist,#[[1]]>0&],PlotStyle->Green],
  AxesOrigin->{0,0},PlotRange->All
]
POSTED BY: Bill Nelson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract