【问题标题】:Plotting time in x axis using matplotlib使用 matplotlib 在 x 轴上绘制时间
【发布时间】:2019-04-17 02:41:38
【问题描述】:

我有以下数据集:

pressuredfjan1[['Unit','Time1']].head()
Out[53]: 
       Unit               Time1
0  321.3599 1970-01-01 00:00:40
1  321.3599 1970-01-01 00:04:40
2  321.6651 1970-01-01 00:06:40
3  320.7496 1970-01-01 00:10:40
4  322.2755 1970-01-01 00:14:40

我正在尝试以 HH:MM:SS 格式绘制 UnitTime1。 我试过以下代码:

dates = dates.date2num(list(pressuredfjan1['Time1']))
fig = plt.figure(figsize=(20,10))
ax1 = fig.add_subplot(111)
ax1.plot(dates,pressuredfjan1['Unit'])
plt.show()

这给了我情节,但我没有在 x 轴上得到 HH:MM:SS 格式。相反,我得到 3.1、3.2、3.4...Le14 等。有人可以帮我吗?谢谢。

【问题讨论】:

    标签: python datetime matplotlib timedelta


    【解决方案1】:

    你可以使用:

    import matplotlib as mpl
    
    pressuredfjan1['dates'] = pd.to_datetime(pressuredfjan1['Time1']).dt.strftime('%H:%M:%S')
    fig = plt.figure(figsize=(20,10))
    ax1 = fig.add_subplot(111)
    majorFormatter = mpl.dates.DateFormatter('%H:%M:%S')
    ax1.xaxis.set_major_formatter(majorFormatter)
    plt.plot_date(pressuredfjan1['dates'], pressuredfjan1['Unit'])
    plt.show()
    

    【讨论】:

    • 非常感谢! :)
    猜你喜欢
    • 1970-01-01
    • 2022-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2021-09-27
    • 2012-03-26
    相关资源
    最近更新 更多