Pandas dataframe plot(): x轴日期标签显示但不显示数据
Pandas dataframe plot(): x-axis date labels display but not data
我正在尝试从 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) 未出现。
如果我省略 ax.set_xlim( datemin, datemax)
,会显示时间序列数据,但 x 轴的格式不再正确(从 1970 年开始,运行s 到 1971 年)。
这是修改后的代码示例:
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')
# format the ticks
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()
- 数据集位于 Assets: Total Assets: Total Assets (Less Eliminations from Consolidation): Wednesday Level (WALCL)
- 使用
parse_dates
和 .read_csv
验证 DATE
列是否为日期时间格式。
设置DATE
为索引
import pandas as pd
import numpy as np
# verify the DATE column is in a datetime format and set it as the index
dfData = pd.read_csv('WALCL.csv', skiprows=0, parse_dates=['DATE'], index_col='DATE')
# plot the data
ax = 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 file
dfData = pd.read_csv('WALCL.csv', skiprows=0, parse_dates=['DATE'])
# plot data
ax = dfData.plot('DATE', 'WALCL', figsize=(20, 8))
我正在尝试从 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) 未出现。
如果我省略 ax.set_xlim( datemin, datemax)
,会显示时间序列数据,但 x 轴的格式不再正确(从 1970 年开始,运行s 到 1971 年)。
这是修改后的代码示例:
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')
# format the ticks
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()
- 数据集位于 Assets: Total Assets: Total Assets (Less Eliminations from Consolidation): Wednesday Level (WALCL)
- 使用
parse_dates
和.read_csv
验证DATE
列是否为日期时间格式。
设置DATE
为索引
import pandas as pd
import numpy as np
# verify the DATE column is in a datetime format and set it as the index
dfData = pd.read_csv('WALCL.csv', skiprows=0, parse_dates=['DATE'], index_col='DATE')
# plot the data
ax = 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 file
dfData = pd.read_csv('WALCL.csv', skiprows=0, parse_dates=['DATE'])
# plot data
ax = dfData.plot('DATE', 'WALCL', figsize=(20, 8))