首页 > 解决方案 > Y 轴格式从秒到 HH:mm

问题描述

我有一个熊猫数组,其中包含一列秒。现在我只想从这些秒数中以 hh:mm 格式格式化 y 轴 - 而不是仅从该列中格式化秒数。

在此处输入图像描述

标签: pythonpandasmatplotlib

解决方案


Try this code:

import matplotlib.pyplot as plt
from matplotlib import dates

# fake data
arr_data = range(0, 4)
arr_secs = [52000, 53000, 54000, 55000]

# format seconds to date with this format '%H:%M:%S'
arr__secs_formatted = list(map(datetime.datetime.strptime, map(lambda s: time.strftime('%H:%M:%S', time.gmtime(s)), arr_secs), len(arr_secs)*['%H:%M:%S']))

fig, ax = plt.subplots()
ax.plot(arr_data, arr_secs_formatted, 'ro-')

# set your date formatter whit this format '%H:%M'
formatter = dates.DateFormatter('%H:%M')
ax.yaxis.set_major_formatter(formatter)

plt.show()

enter image description here


推荐阅读