I am having the following code to display a plot. I would like to conditionally format the values based on a threshold of 0.05.
from matplotlib.colors import to_rgba
# Generate x-axis
x = np.linspace(0, len(data_formula), len(data_formula))
colors = np.where(data_formula <= 0.05, "blue", "green")
plt.plot(x, data_formula, c=colors)
# Add labels and title
plt.ylabel('Volume')
plt.xlabel('time')
plt.title('Energy')
# Display the plot
plt.show()
Unfortunately I do receive the error: array(['blue', 'blue', 'blue', ..., 'blue', 'blue', 'blue'], dtype='<U5') is not a valid value for color
, which indicates that I have passed the wrong value to the color parameter. I have tried it with lists, etc. But it doesn't seem to work. What went wrong? Does the argument color
just not accept any datastructure or is the format wrong?
For reference: data_formula
is defined as follow:
def energy(x):
return (x**2)
data_formula = np.apply_along_axis(energy, axis=0, arr=data_normalized)
It is of datatype: numpy.ndarray
.