Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to color points base on other values in matplotlib?

I have some data that looks like this: (4,120,507.0),(6, 37, 7012.0),etc

The first two is the coordinates, another is the values. I've drawn a scatter plot based on these coordinates.

So how can I color these points based on the values? the bigger the values is ,the darker the color? Thanks a lot

like image 919
dlwlrma Avatar asked Dec 20 '25 22:12

dlwlrma


1 Answers

In a scatter plot, the c argument to plt.scatter(x,y,c=z) can be set to determine the color depending on the value of z. The color is set according to a colormap, which can be set with the cmap argument.

import matplotlib.pyplot as plt

a = [(4,120,507.0),(5, 80, 5415.0),(6, 37, 7012.0),(7, 96, 2173.0),(8,57,3777.0)]
x,y,z = zip(*a)

plt.scatter(x,y, c=z, s=100, cmap="YlOrBr", edgecolor="k")
plt.colorbar(label="values")

plt.show()

enter image description here

Also refer to the documentation and look at the matplotlib examples.

like image 162
ImportanceOfBeingErnest Avatar answered Dec 22 '25 10:12

ImportanceOfBeingErnest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!