Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib, set color of lines along a grayscale using numbers from 0. (white) to 1. (black)

I've been trying to follow the instructions here to set the color of many lines along a grayscale using floats from 0. (white) to 1. (black). The function line.set_color() accepts the floats, but the error below appears when I do plt.show():

ValueError: to_rgba: Invalid rgba arg "1.0"
to_rgb: Invalid rgb arg "1.0"
cannot convert argument to rgb sequence

In this answer it is explained how to do that using plt.cm.RdYlBu(i). Is there any equivalent for grayscale?

like image 943
Saullo G. P. Castro Avatar asked Feb 17 '23 04:02

Saullo G. P. Castro


1 Answers

For historical reasons, matplotlib expects "floats" to be interpreted as grayscale values to be passed in as strings, which is the reason for your error.

For example:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(range(10), color="0.5")
plt.show()

enter image description here

Also, for a grayscale colormap, just use plt.cm.gray or plt.cm.gray_r (reversed). There's a full list of colormaps here: http://matplotlib.org/examples/pylab_examples/show_colormaps.html

like image 160
Joe Kington Avatar answered Feb 18 '23 18:02

Joe Kington