我可以在matplotlib中使用散布函数而不指定x轴吗?

2024-10-01 07:29:12 发布

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

我试图在单列数据帧上使用matplotlib中的散点视图,如下所示:

uva1pd.plot(kind='scatter', y='RESULT')

这是数据帧:

      RESULT
0    2009.13
1    1999.19
2    2014.34
3    1987.51
4    1987.51
..       ...
475  1999.35
476  1987.51
477  1993.19
478  1993.19
479  1982.62

但是,我得到以下错误:

An error was encountered:
scatter requires an x and y column

有没有办法只使用matplotlib中数据帧索引的默认行数


Tags: 数据an视图plotmatplotlib错误errorresult
2条回答

您只需定义一个长度等于y列的伪x变量-

y = np.random.randint(0,20,size=(10,))
x = np.arange(0,len(y)) #dummy x
plt.scatter(x,y)

enter image description here

这里有一种方法,你不需要定义一个新的变量

import matplotlib.pyplot as plt
import pandas as pd

d = pd.DataFrame({'data':[5,89,7,1,56,8]})

plt.scatter(d.index, d['data'])
plt.show()

相关问题 更多 >