【问题标题】:How to chart times (mm:ss) in Matplotlib (formatting output values)如何在 Matplotlib 中绘制时间 (mm:ss)(格式化输出值)
【发布时间】:2013-02-04 02:27:36
【问题描述】:

我在 Python Matplotlib 中以 mm:ss.tttt 格式绘制时间线图。

我已经将这些值转换回千分之一秒,我可以创建一个漂亮的绘图。但这意味着 Y 轴显示的值是“832323”,而不是更容易阅读的“1:23.2323”。

有什么方法可以适当地格式化输出值吗?

【问题讨论】:

    标签: python time charts matplotlib plot


    【解决方案1】:

    我在写完这篇文章后不久就自己解决了这个问题。使用 Matplotlibs 的轴,set_major_formatter() 函数。

    我编写了一个快速格式化函数,它会在千分之一秒内获取一个值并将其转换回 mm:ss.tttt。然后将此格式化程序传递给轴定义。

    将“ticker”模块与绘图内容一起导入:

    import matplotlib.pyplot as plt                                                 
    from matplotlib import ticker  
    

    创建自己的值格式化函数:

    def format_10Kth_time(time, pos=None):                                          
        mins     = time // (10000 * 60)                                             
        secs     = (time - (mins * 10000 * 60)) // (10000)                          
        fracsecs = time % 10000                                                     
        return "%d:%02d.%d" % (mins, secs, fracsecs)
    

    然后在我的绘图代码中,我这样做是为了改变 Y 轴格式:

    plt.gca().yaxis.set_major_formatter(ticker.FuncFormatter(format_10Kth_time))
    plt.plot(...) 
    plt.show() 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多