索引超出范围// Python,数据框,图

时间:2019-04-03 21:47:00

标签: python plot

我想用数据框中的最大值绘制点。

import pandas as pd
import matplotlib.pyplot as plt

dane = pd.read_table('C:\\xxx.txt', names=('rok', 'kroliki', 'lisy', 'marchewki'))
df = pd.DataFrame(dane)

data = df[1:]
data=data.astype(float)

x = int(data['kroliki'].max())
y = int(data['lisy'].max())
z = int(data['marchewki'].max())

p= data['rok'].where(data['kroliki'] == x)
q = data['rok'].where(data['lisy'] == y)
r = data['rok'].where(data['marchewki'] == z)

p1 = int(p[p.notnull()])
q1 = int(q[q.notnull()])
r1 = int(r[r.notnull()])
point = pd.DataFrame({'x':[p1],'y':[q1],'z':[r1]})
point.plot((p1,x),(q1,y),(r1,z))

我有这样的错误:

IndexError: index 1993 is out of bounds for axis 0 with size 4

可能有人知道此代码有什么问题吗? 谢谢

1 个答案:

答案 0 :(得分:0)

我认为,当您使用Pandas进行绘制时,它将在自身中查找索引,而不是值。

因此,就您而言,当您这样做:

point.plot(p1,x)

熊猫将在x方向上(即在所有列中)寻找索引 1993 。换句话说,您应该有1993栏。

我试图重现您的问题,如下所示:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,100,size=(10, 4)), columns=('rok', 'kroliki', 'lisy', 'marchewki'))

data = df[1:]
data=data.astype(float)

x = int(data['kroliki'].max())
y = int(data['lisy'].max())
z = int(data['marchewki'].max())

p = data['rok'].where(data['kroliki'] == x)
q = data['rok'].where(data['lisy'] == y)
r = data['rok'].where(data['marchewki'] == z)

p1 = int(p[p.notnull()])
q1 = int(q[q.notnull()])
r1 = int(r[r.notnull()])

point = pd.DataFrame({'x':[p1],'y':[q1],'z':[r1]})
point.plot((p1,x),(q1,y),(r1,z))

我收到以下错误:

>>> AttributeError: 'tuple' object has no attribute 'lower'

当我分别运行每个点时:

>>>> IndexError: index 85 is out of bounds for axis 0 with size 3

要解决它:

import matplotlib.pyplot as plt

plt.plot((point.x, point.y, point.z), (x,y,z),'ko')

我得到以下结果:

enter image description here

希望有帮助。