pancho
pancho

Reputation: 211

matplotlib use rgba color for text

I would like to add a text with rgba color but matplotlib rejects it, this is my minimal working example

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.text(0.5,0.5,'wololo', color=np.array([[1,1,1,1]]))
plt.show()

it produces the error

TypeError: unhashble type: 'numpy.narray'

Somehow, it works with string defined colors (see the following example), but I would like it to work with any RGBA format

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.text(0.5,0.5,'wololo', color='r')
plt.show()

How could I achieve this?

Upvotes: 0

Views: 457

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339220

The problem is that the array you provide is a 2D array. But RGBA colors should be a 1D array or list. You may use

color=np.array([1,1,1,1])            # Use 1D array

or

color=np.array([[1,1,1,1]]).flat   # flatten 2D array

Upvotes: 2

Related Questions