“Matplotlib不支持生成器作为输入。当我尝试使用scatter()打印列表时”

2024-10-08 16:38:58 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码:

import matplotlib.pyplot as plt

x_values = list(range(1,1001))
y_values = (x**2 for x in x_values)
plt.scatter(x_values,y_values,s=10)
#range of axis
plt.axis([0,1100,0,1100000])
plt.show()

每当我试图在python中通过散布方法绘制一个数字列表时,我总是会得到一个错误,即“matplotlib不支持generatore”。有什么解决办法吗


Image of code


Tags: ofinimport密码formatplotlibasshow
1条回答
网友
1楼 · 发布于 2024-10-08 16:38:58

通过做

y_values = (x**2 for x in x_values)

您可以创建一个生成器

将其列为一个列表并提供:

import matplotlib.pyplot as plt

x_values = list(range(1,1001)) 
y_values = [x**2 for x in x_values]   # list - not generator
plt.scatter(x_values,y_values,s=10)

plt.axis([0,1100,0,1100000])
plt.show()

得到

graph

见:

相关问题 更多 >

    热门问题