【问题标题】:Variable Y-Axis That Adjusts to the Height of the columns可调整到列高度的可变 Y 轴
【发布时间】:2017-09-21 20:00:25
【问题描述】:

我正在寻找当前设置的 y 轴限制的快速代码,如下所示

# Set the Y Axis Limits
ax.set_ylim(0,100)

我希望根据我们的数据点将 100 换成可变/可调整的 y 水平,如下所示:

import pandas as pd
import matplotlib.pyplot as plt

s = pd.Series(
[8, 24, 21, 23, 24],
index = ["Your Plan", "Benchmark", "Region", "Industry", "Size"]
)

我想应该是一个快速修复。

【问题讨论】:

    标签: python matplotlib yaxis


    【解决方案1】:

    您必须通过数据系列的最小值和最大值设置 y 轴限制,这些值是 numpy.max 和 numpy.min 的结果:

    import pandas as pd
    import matplotlib.pyplot as plt
    import numpy as np
    
    s = pd.Series(
    [8, 24, 21, 23, 24],
    index = ["Your Plan", "Benchmark", "Region", "Industry", "Size"]
    )
    
    plt.plot(s.values)
    ax = plt.gca()
    ax.set_ylim(np.min(s),np.max(s))
    plt.show()
    

    【讨论】:

      【解决方案2】:

      如果您删除或不使用ax.set_ylim(0,100) 行,限制将自动根据数据进行调整。

      如果你想固定轴的一端但让另一端自动调整,你也可以使用

      ax.set_ylim(0,None)
      

      【讨论】:

        【解决方案3】:

        如果您添加到代码plt.gca().autoscale_view(),您可以自动缩放您的坐标区。它允许自动调整当前轴限制以适应数据。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-11
          • 2011-04-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-11
          相关资源
          最近更新 更多