python - 在matplotlib中截断y轴
问题描述
我的 matplotlib 条形图的 y 轴介于 0 和 1 之间。但是,图表中的所有值都介于 0.8 和 1 之间,所以我想从 0.7 开始 y 轴。我一直无法找到执行此操作的 pandas 方法,因此将不胜感激。
提前致谢
解决方案
新答案
@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()
.
推荐阅读
- c# - How to make a unique column whose uniqueness depends on another column (enum values)? (ASP .NET Core)
- php - Is there a way to disable copy function in embedded pdf html
- java - HtmlUnit and cookies
- mysql - Prestashop: 'Link to database cannot be established'
- sql - 从记录员工表中获取一个打卡时间和一个打卡时间
- excel - 使用 NumberFormat VBA Excel 添加文本
- javascript - 使用 useState 处理多个单选按钮组
- javascript - 从日期字符串数组中查找最大和最小日期
- java - Spring MVC 找不到带有 pageContext.request.contextPath 的文件
- asp.net - 方法 Edit(MongoDB.Bson.ObjectId) 的不可空类型“MongoDB.Bson.ObjectId”的参数“id”的空条目