python - Pandas 日期未显示在绘图上

标签 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

最佳答案

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

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

关于python - Pandas 日期未显示在绘图上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52527270/

相关文章:

python - 从冗长的字典生成时,Pandas DataFrame.from_dict() 性能不佳

python - 如何将 Pandas 数据框显示为数据表?

python - Groupby Pandas DataFrame 列表中的项目

python - 我如何排除特定类型的 pymysql.err.InternalErrors,而不仅仅是所有类型?

python - 应用 TensorFlow Transform 来转换/缩放生产中的特征

python - 使用 Python Quickfix OrderMassStatusRequest

python - 如何使用新值更新列的特定 DataFrame 切片?

python - Pandas 将用户代理列解析为多列

python - Selenium Python - 等待元素加载并单击它

python - 应用函数然后过滤 DataFrame