如何在 matplot 的 x 轴上绘制 dtype = datetime64[ns] 的数据帧列?

How to draw a dataframe column of dtype = datetime64[ns] on x- axis of a matplot?

我想在matplotlib的同一张图上画两个时间序列。 我有一个 pandas 数据框 as shown here 其中 Date 列为 dtype= datetime64[ns] 作为 x 轴,Close、Open 列为 type=np.float64 作为 y 轴

我正在尝试代码

import matplotlib as plt
plt.xticks( df['Index'].values)
plt.plot(df['Close'])
plt.plot(df['Open'])
plt.show()

但是显示错误。哪里需要改进??

要仅绘制 df 的某些列(在此示例中为 'col1' 和 'col2'),您可以这样做:

import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame([[10,11, 3,'2018-04-02 20:30'], [15, 20, 5, '2018-04-02 20:31'], [20, 25, 6, '2018-04-02 20:40'], [10, 12, 8, '2018-04-02 20:45']], columns = ['col1', 'col2', 'col3' ,'Dates'])
print (df)

   col1  col2  col3             Dates
0    10    11     3  2018-04-02 20:30
1    15    20     5  2018-04-02 20:31
2    20    25     6  2018-04-02 20:40
3    10    12     8  2018-04-02 20:45

df['Dates'] = pd.to_datetime(df['Dates'], format='%Y-%m-%d %H:%M')
df.set_index(['Dates'],inplace=True)
df.loc[:,['col1','col2']].plot()
plt.show()

在你的情况下这应该有效:

df.loc[:,['Open','Close']].plot()