我有一个包含毫秒的datetime数组。我只使用以下命令来绘制时间:
formatterTime = '%H:%M:%S.%f'
ax0.xaxis.set_major_formatter(formatterTime)但是,日期时间数组有6位毫秒数位,绘制为'11:45:05.100000',而我只需要1毫秒数位,例如'11:45:05.1‘。
我尝试过这样的建议
.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]但这似乎只有在将datetime打印为字符串时才有效。
有没有一种方法可以在不修改原始日期数组的情况下只显示绘图上的第一个毫秒数字?
发布于 2020-06-25 21:26:25
您可以使用FuncFormatter设置自己的格式。请看这个例子(包含一些假数据):
from matplotlib.ticker import FuncFormatter
from matplotlib.dates import num2date
x = pd.date_range("2020-01-01", periods = 10, freq = "131ms")
y = range(10)
fig, ax = plt.subplots()
def foo(a, b):
t = num2date(a)
ms = str(t.microsecond)[:1]
res = f"{t.hour:02}:{t.minute:02}:{t.second:02}.{ms}"
return res
ax.xaxis.set_major_formatter(FuncFormatter(foo))
ax.plot(x, y)结果是:

https://stackoverflow.com/questions/62575054
复制相似问题