matplotlib的x标签的分秒格式

2024-10-01 15:43:55 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在下图中显示mm:ss而不是示例编号?在

enter image description here

我试过了:

def formatMS(miliseconds):
    return time.strftime('%M:%S', time.gmtime(miliseconds//1000))

fig, ax = plt.subplots()
plt.plot(segtime, segStrength)
labels = [formatMS(t) for t in segtime]
print labels
ax.set_xticklabels(labels)

但现在显示的是00:00。我如何解决这个问题?在


Tags: 示例labelsreturntimedefpltaxss
1条回答
网友
1楼 · 发布于 2024-10-01 15:43:55

作为设置标签的替代方法,您可以使用记号格式设置工具,如下所示:

import matplotlib
import matplotlib.pyplot as plt
import time


segtime = [1000, 2000, 3000, 3500, 7000]
segStrength = [10000, 30000, 15000, 20000, 22000]    

fig, ax = plt.subplots()
plt.plot(segtime, segStrength)

formatter = matplotlib.ticker.FuncFormatter(lambda ms, x: time.strftime('%M:%S', time.gmtime(ms // 1000)))
ax.xaxis.set_major_formatter(formatter)

plt.show()

这将把一个勾号转换成你需要的格式,给你:

tick conversion

相关问题 更多 >

    热门问题