如何更改 matplotlib X 轴时间戳格式

有时你会在 Matplotlib 中得到奇怪的时间戳格式选择,如 22 19:40(表示某月 22 日 19:40)。

Matplotlib plot with confusing default timestamp format on x-axis

这很容易修复:

matplotlib_timestamp_format.py
import matplotlib.dates as mdates

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))

这将生成此图像,例如:

Matplotlib plot with custom HH:MM timestamp format on x-axis


Check out similar posts by category: Python