0

I would like to plot the max function, but python's default max function can't take arrays, and I guess that's what matplotlib does under the hood.

So what is the correct syntax?

import matplotlib.pyplot as plt
import matplotlib.pylab as pylab

t = pylab.arange(-6, 6)
s = max(0,t)
plt.plot(t, s)
plt.show()

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
3
  • are you trying to plot t and the max value of t? Commented Jun 13, 2014 at 14:18
  • 1
    That's not what the error is... Please give the actual code. Commented Jun 13, 2014 at 14:19
  • @Veedrac woops sorry! I pasted the wrong code Commented Jun 13, 2014 at 14:37

1 Answer 1

1

np.max takes the maximum value of an array (or along a specific axis). What you want is np.maximum, that takes the greater value of two options:

In [3]: np.maximum(0, [-1,2,3])
Out[3]: array([0, 2, 3])

On the other hand, your error does not correspond to the code posted. np.max(0, t) raises a TypeError: only length-1 arrays can be converted to Python scalars because the second argument is the axis, and a scalar number has no dimensions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.