0

I'm trying to assign a color to points in a matplotlib scatter plot based on a value that is not related to the x or y value. Each point can be, for example, one of 3 values, so I want three different colors for the 3 possible values. So, there are 3 arrays: x_arr, y_arr and val_arr where each element of val_arr can be 1, 2 or 3. Seems like most of the examples I've seen have the color based on the position in the array, and not based on an independent value.

In order to get a high contrast between the 3 kinds of points for better visibility where there are many points, I've done something like this:

 pt_color = plt.cm.Dark2(np.linspace(0, 1, 3))

What I cannot figure out is how to assign the right color for each value element. Conceptually, I would like to do something like this,

gr.scatter(x_arr, y_arr, c=val_arr, cmap=pt_color)

where each element of val_arr is mapped to a color using pt_color, but the above does not work (I get a TypeError because pt_color is unhashable type).

Any pointers are appreciated.

ChrisF
  • 47
  • 3
  • 11
  • Why not split your val_arr into three differents arrays then you can easily give differents colors to each set of value... You have to hide the legend though because you don't want to see that there is 3 groups sets. – Richard Sep 17 '15 at 01:23
  • @Richard Interesting idea. It could get kind of hairy though because although I said there were only 3 kinds of points, that was only to simplify the problem. There could be, say 32 different types. Thanks for the suggestion. – ChrisF Sep 17 '15 at 13:55

1 Answers1

1
import matplotlib.pyplot as plt
from numpy.random import random, randint

X = random(10)
Y = random(10)
val_arr = randint(low=0, high=3, size=10)
plt.scatter(X, Y, c=val_arr,
            s=40)
plt.colorbar()
plt.show()

spaces the val_arr values out over the default colormap, and the colorbar explains what the val_arr colors mean.

enter image description here

cphlewis
  • 15,759
  • 4
  • 46
  • 55
  • 2
    setting `makeredegcolor='none'` helps with the appearance of small scatter markers with light colors. – tacaswell Sep 17 '15 at 03:40
  • @cphlewis Thank you. That looks pretty good. I have a lot of points right next to each other. Since the first 2 colors in the default map (red and blue, looking at your image) are both dark, it can be difficult to distinguish. I had assumed the default was some kind of gradual map, where 2 adjacent numbers (e.g., 1 and 2) would have colors that are almost the same, but I see that's not true. Anyway, that's what led me to try the non-default cmap. – ChrisF Sep 17 '15 at 13:34
  • @tcaswell Thanks. That kwarg didn't work for me. Nor did `makeedgecolors` (assuming you had a typo). Also, is this different from `edgecolors='None'`, which does work for me? – ChrisF Sep 17 '15 at 13:41
  • Related question...I'm not sure how to modify my legend for this graph. In my previous code I had something like: `leg_colors = plt.cm.Dark2(np.linspace(0, 1, 3)) recs = [] for i in range(3) recs.append(mpatches.Rectangle(0,0),1,1,fc=leg_colors[i])` How does one specify to cycle through the default colors instead of using Dark2 as in the above case for the legend rectangles? In other words, how would you add a legend to the example you gave? – ChrisF Sep 17 '15 at 15:37
  • Check out `markeredgecolor`. (Though I should say I think keeping the margin helps with low-quality representations, so it depends on how people will be looking at this.) Don't use a legend to explain colors alone; that's what a colorbar is for (added). – cphlewis Sep 17 '15 at 20:18
  • I could try a colorbar; it looks easier. But that kind of implies the value range is continuous. In my case the values are discrete whole numbers which is why I opted for a legend initially. – ChrisF Sep 17 '15 at 21:41
  • Shapes -- markerstyles -- might be a better ?cognitive? match, then, as they're even more obviously categorical. You could do both at once, e.g. with pandas as in [this example](http://stackoverflow.com/questions/27711078/scatter-plots-in-pandas-pyplot-how-to-plot-by-category-with-different-markers?lq=1) – cphlewis Sep 18 '15 at 03:22