简体   繁体   English

仅在 Matplotlib 中绘制时间(而不是 DateTime)

[英]Plot Time Only in Matplotlib (Instead of DateTime)

My Dataframe is in this format我的数据框采用这种格式

   Time    AvgDiff
07:00:00   7.750782
07:01:00   9.306567
07:02:00  13.225209
07:03:00  18.346431
07:04:00  10.049761
07:05:00  18.567049
07:06:00  -4.903014
07:07:00 -10.66217

I did the following to create a synthetic date, the date is not important as my AvgDiff is the average of all dates for the particular time value我做了以下操作来创建一个合成日期,日期并不重要,因为我的 AvgDiff 是特定时间值的所有日期的平均值

my_day = datetime.date(2018, 1, 15)
x_dt = [ datetime.datetime.combine(my_day, t) for t in df['Time'] ]
x = x_dt
y1 = df['AvgDiff']


plt.bar(x,y1, label='AvgDiff BPS', width=0.001) #bar to plot
plt.legend(loc='best')
plt.title('AvgDiff BPS')
plt.ylabel('BPS')
plt.xlabel('Time')
plt.gcf().autofmt_xdate()
plt.show()

This shows这显示

在此处输入图片说明

It shows both the Date and Hours behind (in the X-Axis), However I need it to show just the Hours and Minutes.它显示后面的日期和小时数(在 X 轴中),但是我需要它只显示小时数和分钟数。

I tried我试过了

FMT = '%H:%M:%S'
df['Time'] = df['Time'].map(lambda x: datetime.strptime(str(x), FMT))
df['Time'] = df['Time'].map(lambda x: x.replace(day=date, month=month, year=year))
plt.gcf().autofmt_xdate()
plt.show()

But it produces the exact same chart, why is that so?但它产生完全相同的图表,为什么会这样?

Ok I managed to solve this as per below好的,我设法按照下面的方法解决了这个问题

import matplotlib.dates as mdates
import matplotlib.pyplot as plt
xformatter = mdates.DateFormatter('%H:%M')
plt.gcf().axes[0].xaxis.set_major_formatter(xformatter)

This produces Hour:Minute这产生小时:分钟

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM