在matplotlib中显示毫秒数

Bob*_*Bob 11 python matplotlib

我正在使用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
Run Code Online (Sandbox Code Playgroud)

cge*_*cge 14


这里的问题是有一个格式化刻度的类,而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()
Run Code Online (Sandbox Code Playgroud)