I want to plot a piece wise function using matplotlib
:
import numpy as np
import matplotlib.pyplot as plt
def pwf(x):
return 0 if x < 0 else 1
x = np.linspace(-1, 1, 100)
plt.plot(x, pwf(x))
and I get the following error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
However, on using (x < 0).any()
I simply get True
and on (x < 0).all()
I get False
, neither of which is suitable for my case.
I found the answer here: Python Error: Using matplotlib: Truth value of an array with more than one element is ambiguous. Use a.any() or a.all(), but the question and answer are so bloated with unnecessary code that I decided to post a concise solution out of it.