使用matplotlib绘制最大函数

时间:2014-06-13 14:16:00

标签: python matplotlib

我想绘制最大函数,但是python的默认最大函数不能接受数组,我猜这就是matplotlib在幕后所做的事情。

那么正确的语法是什么?

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()

1 个答案:

答案 0 :(得分:1)

np.max获取数组的最大值(或沿特定轴)。你想要的是np.maximum,它取两个选项的更大价值:

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

另一方面,您的错误与发布的代码不对应。 np.max(0, t)引发TypeError: only length-1 arrays can be converted to Python scalars因为第二个参数是轴,而标量数字没有维度。