Python 带有未知错误的 Matplotlib 散点图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35733223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matplotlib scatter plot with unknown error
提问by Bogdan Janiszewski
I am attempting to create a scatter plot. I have a list of numbers from 0 - 17 as well as an array with 18 values. I can plot the data as a line plot but when I try to plot as a scatter I get an error message I do not understand: 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''
我正在尝试创建散点图。我有一个 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''
What does this error message mean and how can I get the data to plot as a scatter?
此错误消息是什么意思,如何将数据绘制为散点图?
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()
回答by gsmafra
Check the scatter documentation. Third argument is for size of points and should be scalar or array_like. I assume 'r'
is for color so do the following:
检查分散文档。第三个参数是点的大小,应该是标量或类似数组。我假设'r'
是为了颜色,所以请执行以下操作:
plt.scatter(x, y, c='r')