【发布时间】:2019-11-01 01:10:05
【问题描述】:
我目前正在尝试绘制数据,其中x 变量是年份,y 变量是费城费城人队在一个赛季中获胜的次数。我尝试了多种方法从我的数据集中绘制这两个变量,但是没有任何效果。以下是我尝试的最后一个选项。
我文件的第一列是年份,第三列是获胜次数(也就是第 0 列和第 2 列)。
我尝试将 x 和 y 设置为列,下面是我最近尝试过的。
import csv
import numpy
import random
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize']=(10,6)
phillies_data =
pd.read_csv('/Users/hannahbeegle/Desktop/Teams/PHILLIEScsv.csv',
header = None)
phillies_data.plot(x='Year',y='W')
plt.xlabel('Year')
plt.ylabel('Wins')
plt.title('Amount of Wins in Phillies History (1871-2018)')
plt.xlim(1870, 2020)
plt.ylim(0, 170)
plt.show()
错误信息:
Traceback (most recent call last):
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexes/base.py”,第 2657 行,在 get_loc 返回 self._engine.get_loc(key) 文件“pandas/_libs/index.pyx”,第 108 行,在 pandas._libs.index.IndexEngine.get_loc 文件“pandas/_libs/index.pyx”,第 129 行,在 pandas._libs.index.IndexEngine.get_loc 文件“pandas/_libs/index_class_helper.pxi”,第 91 行,在 pandas._libs.index.Int64Engine._check_type KeyError: '年份'
在处理上述异常的过程中,又发生了一个异常:
Traceback(最近一次调用最后一次): 文件“/Users/hannahbeegle/Desktop/Text Files/TeamDataBase.py”,第 121 行,在 phillies_data.plot(x="年", y="W") 调用中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/plotting/_core.py”,第 2942 行 sort_columns=sort_columns, **kwds) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/plotting/_core.py”,第 1973 行,在 plot_frame **kwds) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/plotting/_core.py”,第 1763 行,在 _plot elif 不是实例(数据 [x],ABCSeries): getitem 中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame.py”,第 2927 行 索引器 = self.columns.get_loc(key) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexes/base.py”,第 2659 行,在 get_loc 返回 self._engine.get_loc(self._maybe_cast_indexer(key)) 文件“pandas/_libs/index.pyx”,第 108 行,在 pandas._libs.index.IndexEngine.get_loc 文件“pandas/_libs/index.pyx”,第 129 行,在 pandas._libs.index.IndexEngine.get_loc 文件“pandas/_libs/index_class_helper.pxi”,第 91 行,在 pandas._libs.index.Int64Engine._check_type KeyError: '年份'
【问题讨论】:
-
参数有误。
x和y参数在您的数据框中采用 列的名称,而不是列本身。df.plot(x="column1", y="column3") -
尝试使用 .iloc untead of .loc 吗?
-
@ImportanceOfBeingErnest 我按照你说的做了,得到了我在上面编辑的这个错误
-
@ImportanceOfBeingErnest file:///Users/hannahbeegle/Desktop/Screen%20Shot%202019-06-18%20at%2012.30.27%20PM.png
标签: python pandas matplotlib plot