Python 使用matplotlib打印df时如何在图表上获取x轴标签

Python 使用matplotlib打印df时如何在图表上获取x轴标签,python,matplotlib,charts,Python,Matplotlib,Charts,我在一个数据帧中有详细的数据。我试图在y轴列C上绘制索引列日期。如何将5个日期显示为x轴标签 目前,我试着这样画: df['C'].plot(figsize=(16,6)) 但是,我看不到任何x轴标签 df=pd.DataFrame([["2020-07-01 00:00:00",2],["2020-07-01 00:01:00",3]],columns=['date','C']) 以下是解决方案: 阅读数据帧: 将date的dtype更改为da

我在一个数据帧中有详细的数据。我试图在y轴列
C
上绘制索引列日期。如何将5个日期显示为x轴标签

目前,我试着这样画:

df['C'].plot(figsize=(16,6))
但是,我看不到任何x轴标签

df=pd.DataFrame([["2020-07-01 00:00:00",2],["2020-07-01 00:01:00",3]],columns=['date','C'])
以下是解决方案:

  • 阅读
    数据帧
  • date
    dtype
    更改为
    datetime64[ns]
  • 日期
    作为
    x
    绘制:
输出:

您可以使用
mdates.DateFormatter
更改滴答声的频率,如回答中所述:

df=pd.DataFrame([["2020-07-01 00:00:00",2],["2020-07-01 00:01:00",3]],columns=['date','C'])
df.date = pd.to_datetime(df.date)
df.plot(x='date')
## borrowed from https://stackoverflow.com/questions/14946371/editing-the-date-formatting-of-x-axis-tick-labels-in-matplotlib
import matplotlib.dates as mdates
myFmt = mdates.DateFormatter('%d') ## change '%d' depending on your requirement
ax.xaxis.set_major_formatter(myFmt)