【问题标题】:matplotlib intelligent axis labels for timedelta用于 timedelta 的 matplotlib 智能轴标签
【发布时间】:2013-03-06 05:56:21
【问题描述】:

我在 matplotlib 中绘制了一个简单的 X 和 Y 值数据集。我的数据中的自变量是持续时间/时间增量(例如 60 秒、2 小时、24 小时、10 天),在我的输入数据中始终表示为整数秒数。我的问题是,matplotlib 有没有办法以人类可读的形式智能地设置持续时间轴标签?

例如,在规模较小的一端,最好将 30 秒简单地显示为“30 秒”。在规模较大的一端,显示“10 天”而不是 864000 秒会更好。介于两者之间的某个地方,最好以“分钟”和“小时”为单位读取标签。 matplotlib 是否有任何自动方式来推断在跨越几个数量级的持续时间内近似人类可读的东西?

理想情况下,我使用的任何方法都应该推广到跨越不同持续时间尺度的数据集,而不是针对一个输入数据集单独定制的图。

【问题讨论】:

    标签: matplotlib


    【解决方案1】:

    你能举个例子吗?这是你想要的吗:

    import datetime                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                   
    import numpy as np                                                                                                                                                                                                                                                             
    import pylab as plt                                                                                                                                                                                                                                                            
    import matplotlib                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                   
    fig = plt.figure()                                                                                                                                                                                                                                                             
    ax = fig.add_subplot(111)                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                   
    x = np.linspace(0, 300)  # 5 minutes                                                                                                                                                                                                                                                      
    y = np.random.random(len(x))                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                   
    ax.plot(x, y)                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                   
    def timeTicks(x, pos):                                                                                                                                                                                                                                                         
        d = datetime.timedelta(seconds=x)                                                                                                                                                                                                                                          
        return str(d)                                                                                                                                                                                                                                                              
    formatter = matplotlib.ticker.FuncFormatter(timeTicks)                                                                                                                                                                                                                         
    ax.xaxis.set_major_formatter(formatter)                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                   
    plt.show()
    

    它使用pythons timedelta。 864000 秒以上将导致“10 天,10:00:00”。您当然可以将更高级的格式填充到上面的 timeTicks() 函数中。

    【讨论】:

    • 另外,如果您想修改 xticks 以每分钟显示一次:ax.xaxis.set_major_locator(plt.MultipleLocator(1*60))
    猜你喜欢
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多