首页 > 解决方案 > 在matplotlib中截断y轴

问题描述

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

提前致谢

标签: pythonpandasmatplotlib

解决方案


新答案

@ChrisA 在评论中提供了最佳答案:如果使用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()(参考:绘图的生命周期)开头,那么您不需要运行ax = plt.gca().


推荐阅读