Skip to main content
 首页 » 编程设计

python之在 python matplotlib.dates 中绘制毫秒图

2025年05月04日13freeliver54

我有使用 datetime.datetime.strptime 格式化的时间标记数据,其中包括毫秒。数据的范围从几分钟到几小时不等。当我使用 pyplot 绘图并放大时,最小刻度线似乎是 1 秒。似乎 matplotlib 的 TickHelper RRuleLocator 只有一个 SecondLocator。有没有办法在放大时包含毫秒分辨率?

receivedTime = datetime.datetime.strptime(str(data[1]), "%H:%M:%S.%f") 
 
fig=plt.figure() 
ax=fig.add_axes([0.1,0.1,.8,.8]) 
fig.autofmt_xdate() 
 
ax.plot(times, prices, color='blue', lw=1, drawstyle='steps-post', label = prices) 
plt.show() 

请您参考如下方法:

Matplotlib 使用类似 Matlab 的数字(自 1970 年以来的秒数)作为日期。如果您有毫秒,则必须将日期转换为“数字”:

import matplotlib.dates as mdates 
ntimes = mdates.num2epoch(times / 1000.0) 

并绘制 ntimes 而不是 times。