下面是我正在try 绘制的函数
def f(x):
return 50 * (1 / (1 + ((50/5)-1) * e ** -max(0, 0.1 * x)))
try 使用matplotlib绘制时出现以下错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
个
我相信这是因为我的功能中的-max(0, 0.1 * x)
部分.
以下是我的matplotlib代码的完整版本:
import matplotlib.pyplot as plt
import numpy as np
from math import e
# 100 linearly spaced numbers between -10 and 100
x = np.linspace(-10,100,200)
def f(x):
return 50 * (1 / (1 + ((50/5)-1) * e ** -max(0, 0.1 * x)))
# setting the axes at the centre
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# plot the function
plt.plot(x,f(x), 'r')
# show the plot
plt.show()
研究this错误使我发现,虽然max()
函数通常计算数字,但由于它传递的x是np.linspace()
而不是数字,因此它无法计算它.
如何在不进行内部更改的情况下绘制f(x)
函数(因为这会 destruct 我已有的一些单元测试)