带有未知错误的 Matplotlib 散点图

问题描述 投票:0回答:2

我正在尝试创建散点图。我有一个 0 - 17 的数字列表以及一个包含 18 个值的数组。我可以将数据绘制为线图,但是当我尝试绘制为散点图时,我收到一条我不理解的错误消息:

TypeError: ufunc 'sqrt' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

此错误消息是什么意思,我怎样才能将数据绘制成散点图?

import numpy as np
import matplotlib.pyplot as plt

y = [7316.0, 7453.25, 7518.25, 7711.5, 7448.0, 7210.25, 7416.75, 6960.75, 
     7397.75, 6397.5, 5522.75, 5139.0, 5034.75, 4264.75, 5106.0, 3489.5, 
     4712.0, 4770.0]
x = np.arange(0,18,1)

plt.rcParams['legend.loc'] = 'best'
plt.figure(1)
plt.xlim(0, 20)
plt.ylim(0, 10000)
plt.scatter(x, y, 'r')
plt.show()
python python-3.x numpy matplotlib scatter-plot
2个回答
97
投票

检查 scatter 文档。第三个参数是点的大小,应该是标量或 array_like。我假设

'r'
是颜色,所以请执行以下操作:

plt.scatter(x, y, c='r')

0
投票

plot()
的第三个位置参数是
fmt=
,它采用
'[marker][line][color]'
格式的字符串,这样每条线、颜色和标记都可以一次格式化。如果省略线,它就会变成散点图。例如,以下两个绘制相同的图形:

plt.plot(x, y, 'ro');

plt.scatter(x, y, color='r');
© www.soinside.com 2019 - 2024. All rights reserved.