Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color coding matplotlib markers

I'm trying to color-code the markers of my plot.

The idea is that there are x,y, and z values -- x and y containing coordinates, z containing a number corresponding to each coordinate (i.e., x,y-pair) between 0 and 150.

Now, I need to plot a square for each coordinate, which I accomplish with pyplot.plot(x,y, ls='s'), where I can set the marker edge and face color separately. Usually, all of those square have the same color.

What I am trying is to change the color according to the z-value, for example

z=0-30: black, 30-60: blue, 60-90: green, 90-120: yellow, 120-150: red

I tried binning the data to those bins, but i have no idea how to map that to the colors and plot it accordingly.

Any suggestions? Thanks!

like image 363
frixhax Avatar asked Apr 10 '13 13:04

frixhax


People also ask

How do I change the marker color in matplotlib?

All of the line properties can be controlled by keyword arguments. For example, you can set the color, marker, linestyle, and markercolor with: plot(x, y, color='green', linestyle='dashed', marker='o', markerfacecolor='blue', markersize=12).

How do I specify RGB in matplotlib?

Matplotlib recognizes the following formats to specify a color. RGB or RGBA (red, green, blue, alpha) tuple of float values in a closed interval [0, 1]. Case-insensitive hex RGB or RGBA string. Case-insensitive RGB or RGBA string equivalent hex shorthand of duplicated characters.


1 Answers

Store those X,Y coordinates in separate X,Y's (1 pair for each Z range), then plot them all by multiple plot lines.

Conceptual example:

X1,Y1 = """store your Z0-30 here"""
X2,Y2 = """store your z30-60 here"""
X3,Y3 = """store your z60-z90 here"""
X4,Y4 = """store your z90-120 here"""
X5,Y5 = """store your z120-150 here"""
fig=plt.figure()
pyplot.plot(X1,Y1,color='black')
pyplot.plot(X2,Y2,color='blue')
pyplot.plot(X3,Y3,color='green')
pyplot.plot(X4,Y4,color='yellow')
pyplot.plot(X5,Y5,color='red')
plt.show()

I would suggest using a scatter plot, it sounds a lot more suited to what you describe. Change pyplot.plot to pyplot.scatter in that case. You can use a cmap to colour them by Z axis. You could also assign custom markers to each group as displayed in this example.

like image 171
Bas Jansen Avatar answered Sep 24 '22 07:09

Bas Jansen