我正在使用matplotlib以hh:mm:ss.ms格式绘制数据作为时间的函数,其中ms是毫秒。但是,我看不到情节中的毫秒数。是否可以添加它们?

dates = matplotlib.dates.datestr2num(x_values) # convert string dates to numbers
plt.plot_date(dates, y_values)  # doesn't show milliseconds

最佳答案

这里的问题是,有一个用于格式化刻度的类,而plot_date将该类设置为您不想要的东西:一个自动格式化器,从不绘制毫秒。

为了更改此设置,您需要从matplotlib.dates.AutoDateFormatter更改为自己的格式化程序。 matplotlib.dates.DateFormatter(fmt)datetime.strftime格式字符串创建一个格式化程序。我不确定如何显示毫秒,但是会显示微秒,希望对您有用。毕竟,它只是一个额外的零。试试下面的代码:

dates = matplotlib.dates.datestr2num(x_values) # convert string dates to numbers
plt.plot_date(dates, y_values)  # doesn't show milliseconds by default.

# This changes the formatter.
plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%H:%M:%S.%f"))

# Redraw the plot.
plt.draw()

关于python - 在matplotlib中显示毫秒,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11107748/

10-12 21:45