Pandas dataframeplot():显示x轴日期标签但不显示数据

我正在尝试将 pandas 数据框中的数据绘制为时间(年)的函数。数据摘要如下所示:


           DATE    WALCL

0    2010-08-18  2313662

1    2010-08-25  2301015

2    2010-09-01  2301996

3    2010-09-08  2305802

4    2010-09-15  2296079

517  2020-07-15  6958604

518  2020-07-22  6964755

519  2020-07-29  6949032

520  2020-08-05  6945237

521  2020-08-12  6957277

我尝试使用以下代码绘制数据:


import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import matplotlib.dates as mdates


years = mdates.YearLocator()   # every year

months = mdates.MonthLocator()  # every month

years_fmt = mdates.DateFormatter('%Y')


dfData = pd.read_csv(sPathIn+sFname, skiprows = 0)


ax = dfData.plot()

ax.xaxis.set_major_locator(years)

ax.xaxis.set_major_formatter(years_fmt)

ax.xaxis.set_minor_locator(months)

datemin = np.datetime64(dfData['DATE'][0], 'Y')

datemax = np.datetime64(dfData['DATE'].iloc[-1], 'Y') + np.timedelta64(1, 'Y')

ax.set_xlim( datemin, datemax)     

plt.show()

当我运行此代码时,绘图轴显示正确,但时间序列数据 (WALCL) 未出现。

http://img3.mukewang.com/649aa9340001a2ed03770261.jpg

如果我省略ax.set_xlim( datemin, datemax),则会显示时间序列数据,但 x 轴的格式不再正确(从 1970 年开始一直运行到 1971 年)。

http://img3.mukewang.com/649aa9400001561003730257.jpg

这是修改后的代码示例:


import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import matplotlib.dates as mdates


years = mdates.YearLocator()   # every year

months = mdates.MonthLocator()  # every month

years_fmt = mdates.DateFormatter('%Y')


sPathIn = "C:\\Users\\reg\\projects\\notes\\Political_Economy\\S&P+Fed-Assets\\"

sFname = "WALCL.csv"

这是回溯:


Traceback (most recent call last):


  File "C:\Users\reg\projects\Notes\Political_Economy\S&P+Fed-Assets\Python\s&p-fed-assets-v0.2.3.py", line 25, in <module>

    dfData.set_index('DATE', inplace=True)


  File "C:\Users\reg\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4545, in set_index

    raise KeyError(f"None of {missing} are in the columns")


KeyError: "None of ['DATE'] are in the columns"

    # load data

    dfData = pd.read_csv(sPathIn+sFname, skiprows = 0, parse_dates=['DATE'], index_col='DATE')

    

    #set up plot fxn

    dfData.set_index('DATE', inplace=True)

    ax = dfData.plot('DATE', 'WALCL')

   

翻过高山走不出你
浏览 296回答 1
1回答

叮当猫咪

数据集位于资产:总资产:总资产(减去合并冲销):周三水平 (WALCL)使用with验证该DATE列是否采用日期时间格式。parse_dates.read_csv设置DATE为索引import pandas as pdimport numpy as np# verify the DATE column is in a datetime format and set it as the indexdfData = pd.read_csv('WALCL.csv', skiprows=0, parse_dates=['DATE'], index_col='DATE')# plot the dataax = dfData.plot(figsize=(20, 8))datemin = np.datetime64(dfData.index.min(), 'Y')datemax = np.datetime64(dfData.index.max(), 'Y') + np.timedelta64(1, 'Y')ax.set_xlim(datemin, datemax)保留DATE为专栏import pandas as pd# read filedfData = pd.read_csv('WALCL.csv', skiprows=0, parse_dates=['DATE'])# plot dataax = dfData.plot('DATE', 'WALCL', figsize=(20, 8))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python