【问题标题】:Truncating y axis in matplotlib [duplicate]在matplotlib中截断y轴[重复]
【发布时间】:2019-03-19 15:16:24
【问题描述】:

我的 matplotlib 条形图的 y 轴介于 0 和 1 之间。但是,图表中的所有值都介于 0.8 和 1 之间,所以我想从 0.7 开始 y 轴。我一直无法找到执行此操作的 pandas 方法,因此将不胜感激。

提前致谢

【问题讨论】:

  • 如果使用DataFrame.plot()方法,添加参数ylim=(0.7, 1)
  • 是的,非常感谢。我不知道 lim 存在

标签: python pandas matplotlib


【解决方案1】:

新答案

@ChrisA 在 cmets 中提供了最佳答案:如果使用DataFrame.plot() 方法,请添加参数ylim=(0.7, 1)

旧答案

如果您使用的是通常的 import matplotlib.pyplot as plt,则可以快速解决以下问题:在您的 pandas plot 命令下添加以下行:

# gca = "get current axis"
ax = plt.gca()

# ax.get_ylim() returns a tuple of (lower ylim, upper ylim)
ax.set_ylim(0.7, ax.get_ylim()[1])

如果您使用的面向对象的 matplotlib API 通常以 fig, ax = plt.subplots() 之类的开头(参考:The Lifecycle of a Plot),那么您不需要运行 ax = plt.gca()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多