Message Boards Message Boards

How to color each point with a different color in ListPlot?? Please help

Posted 10 years ago

Hello colleagues... I'm afraid that this is an easy question, but I found no solution yet

Here it goes: I have a list of points, and a list of colors (both of the same length), say:

 aList = {{1, 1}, {2, 3}, {4, 5}, {6, 7}};
 colorList = {Black, Red, Blue, Yellow};

I want to plot the points using ListPlot, but taking care that each point should have a different color, according to the colorList. So, the point {1,1} should be colored black, the point {2,3} should be red, and so on.... In the actual problem I'm solving, the length of the list is about 30.000, so I need an efficient way to construct the plots...

Thank you very much!!! :-)

Daniel

POSTED BY: Daniel Branco
2 Replies
Posted 10 years ago

Thank you so much David!!!! Pretty neat, thank you :-)

POSTED BY: Daniel Branco
Posted 10 years ago

You can make the list of points a list of lists, each having a single point. ListPlot treats it as multiple plots, with each directive in a PlotStyle applying to a one-point list. The PlotStyle directives would be used cyclically for larger lists.

aList = {{1, 1}, {2, 3}, {4, 5}, {6, 7}};

colorList = {Black, Red, Blue, Yellow};

bList = {#} & /@ aList;

ListPlot[bList, PlotStyle -> colorList]

enter image description here

Or you can just construct your own list of points. With a color directive coming before each point in the list. You can use usual options to get the range, axes, labels as you want.

Graphics[Prepend[Riffle[colorList, Point /@ aList], PointSize[.02]], 
 Frame -> True]

enter image description here

POSTED BY: David Keith
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