大熊猫日期未显示在情节上

时间:2018-09-26 23:20:10

标签: python pandas

我的数据帧来自cvs,如下所示:

         Date     Open     High     Low    Close  Adj Close    Volume
0  1996-01-01  4.06250  4.12500  3.8750  3.90625   3.093209   7048800
1  1996-02-01  3.84375  3.96875  3.5000  3.62500   2.870497  12864000
2  1996-03-01  3.50000  4.25000  3.5000  4.12500   3.266428   9526400
3  1996-04-01  4.06250  4.68750  4.0625  4.50000   3.563378   5693600
4  1996-05-01  4.40625  4.65625  4.1250  4.21875   3.340666  30480000

当我尝试绘制它时,图表底部没有日期。这是我正在使用的代码:

df = pd.read_csv('ABC.csv') #read cvs
df2 = df.loc[0:12] #select rows
df2.set_index('Date', inplace=True) #set Date as index
df2['Close'].plot() #plot

enter image description here

2 个答案:

答案 0 :(得分:2)

您的日期是一个字符串,因此matplotlib不会自动绘制标签。首先将其更改为datetime,然后绘制。

import pandas as pd
df['Date'] = pd.to_datetime(df.Date)

您的情节:

df2 = df.loc[0:12]
df2.set_index('Date', inplace=True)
df2['Close'].plot()

enter image description here

答案 1 :(得分:2)

将列日期设置为datetime类型:)

df = pd.read_csv('ABC.csv') #read cvs
df2 = df.loc[0:12] #select rows
df2['Date'] = pd.to_datetime(df2['Date'])
df2.set_index('Date', inplace=True) #set Date as index
df2['Close'].plot() #plot