python - matplotlib的x标签的分秒格式

标签 python matplotlib

如何在下图中显示 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。我该如何解决这个问题?

最佳答案

作为设置标签的替代方法,您可以使用如下的刻度格式化程序:

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

关于python - matplotlib的x标签的分秒格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40395227/

相关文章:

python : How to plot 3d graphs using Python?

python - 在列表中的另一个值之后插入一个值

python处理无尽的XML

python - 如何安排python脚本在给定时间退出

python - Matplotlib 饼图标签与值不匹配

python - 如何为 Jupyter 中的每个单元格设置 matplotlib 的 DPI

python - 绘制双曲面

python - 如何知道 CPython 可执行文件是否是 python 中的调试版本?

python - 如何获取使用 zip() 从列表构建的 pandas-Dataframe 中的第二个 header

python - 如何在 Alpine 上安装 matplotlib