【发布时间】:2019-04-19 06:30:52
【问题描述】:
我尝试运行下面的代码,
import math
import matplotlib.pyplot as plt
from functools import partial
def difference_quotient(f,x,h):
return(f(x+h)-f(x))/h
def square(x):
return x*x
def derivative(x):
return 2*x
derivative_estimate = partial(difference_quotient,square,h=0.0001)
x = range(-10,10)
y = range(-10,10)
plt.title("actual vs estimation")
plt.plot(x,map(derivative,x),'rx',label="actual")
plt.plot(x,map(derivative_estimate,x),'b+',label="estimate")
plt.show()
print(len(list(map(derivative,x))))
但它在下面显示错误
Traceback(最近一次调用最后一次):文件“C:\Program Files\Python37\lib\site-packages\matplotlib\units.py",第 168 行,在 获取转换器 如果不是 np.all(xravel.mask): AttributeError: 'numpy.ndarray' object has no attribute 'mask'
在处理上述异常的过程中,又发生了一个异常:
Traceback(最近一次调用最后一次):文件 “C:\Users\asus\Documents\Sublime\dataScience\gradient.py”,第 20 行,在 plt.plot(x,map(derivative,x),'rx',label="actual") 文件 "C:\Program Files\Python37\lib\site-packages\matplotlib\pyplot.py", 第 2811 行,在情节中 is not None else {}), **kwargs) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib__init__.py”,第 1810 行, 在内部 返回 func(ax, *args, **kwargs) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib\axes_axes.py”,第 1611 行, 在情节 对于 self._get_lines(*args, **kwargs) 中的行:文件“C:\Program Files\Python37\lib\site-packages\matplotlib\axes_base.py”,第 393 行, 在 _grab_next_args 来自 self._plot_args(this, kwargs) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib\axes_base.py”,第 370 行, 在_plot_args x, y = self._xy_from_xy(x, y) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib\axes_base.py”,第 205 行, 在 _xy_from_xy by = self.axes.yaxis.update_units(y) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib\axis.py”,第 1467 行,在 更新单位 转换器 = munits.registry.get_converter(data) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib\units.py”,第 181 行,在 获取转换器 转换器 = self.get_converter(next_item) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib\units.py”,第 187 行,在 获取转换器 thisx = safe_first_element(x) 文件“C:\Program Files\Python37\lib\site-packages\matplotlib\cbook__init__.py”,行 第1635章 raise RuntimeError("matplotlib 不支持生成器" RuntimeError: matplotlib 不支持生成器作为输入 [0.7s完成]
我的嫌疑人在这条线上,
plt.plot(x,map(derivative,x),'rx',label="actual")
plt.plot(x,map(derivative_estimate,x),'b+',label="estimate")
当我尝试使用范围 (-10,10) 的 y 更改 map(derivative,x) 和 map(derivative_estimate,x) 时,它可以工作。
当我使用上面的地图功能时,我应该怎么做才能让代码显示绘图?
【问题讨论】:
-
请放回原始回溯。使用 >
python /n traceback text
标签: python python-3.x matplotlib python-3.7