Truncating y axis in matplotlib

时间:2019-03-19 15:16:25

标签: python pandas matplotlib

The y axis of my matplotlib bar chart is between 0 and 1. However, all values for in the chart are between 0.8 and 1 so I would like to start the y axis at 0.7. I haven't been able to find the pandas method of doing this so any help would be appreciated.

Thanks in advance

1 个答案:

答案 0 :(得分:2)

新答案

@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()(参考:The Lifecycle of a Plot)开头,则无需运行ax = plt.gca()。 / p>