Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Matplotlib -> to_rgba: Invalid rgba arg

I'm trying to plot contours and have previously used a RGB tuple to specify the color (only one for all contours) - however, now I get a ValueError from to_rgba:

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

Here is an example:

import numpy as np
import matplotlib.pyplot as plt
grid = np.random.random((10,10))
contours = np.linspace(0, 1, 10)

Now this works!

plt.contour(grid, levels = contours, colors = 'r')
plt.show()

But this does NOT work!

plt.contour(grid, levels = contours, colors = (1,0,0))
plt.show()

Am I doing something wrong or is this bug (/new feature) in Matplotlib? Thanks.

like image 371
mrvj Avatar asked Jan 14 '15 19:01

mrvj


1 Answers

As pointed out in the comments, plt.contour() expects a sequence of colors. If you want to specify an RGB tuple, make it the first element of such a sequence.

plt.contour(grid, levels = contours, colors = ((1,0,0),) )

or

plt.contour(grid, levels = contours, colors = [(1,0,0),] )
like image 146
Alex Björling Avatar answered Oct 18 '22 18:10

Alex Björling