使用matplotlib将y范围更改为从0开始 [英] Change y range to start from 0 with matplotlib

查看:1907
本文介绍了使用matplotlib将y范围更改为从0开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib绘制数据.这是执行类似操作的代码:

I am using matplotlib to plot data. Here's a code that does something similar:

import matplotlib.pyplot as plt
f, ax = plt.subplots(1)
xdata = [1, 4, 8]
ydata = [10, 20, 30]
ax.plot(xdata, ydata)
plt.show(f)

这在图中显示了一条y轴从10到30的线.虽然我对x范围感到满意,但我想将y范围更改为从0开始并调整ymax以显示所有内容

This shows a line in a graph with the y axis that goes from 10 to 30. While I am satisfied with the x range, I would like to change the y range to start from 0 and adjust on the ymax to show everything.

我当前的解决方案是:

ax.set_ylim(0, max(ydata))

但是我想知道是否有一种方法可以说:自动缩放但从0开始.

However I am wondering if there is a way to just say: autoscale but starts from 0.

推荐答案

必须在绘图后之后设置范围.

The range must be set after the plot.

import matplotlib.pyplot as plt
f, ax = plt.subplots(1)
xdata = [1, 4, 8]
ydata = [10, 20, 30]
ax.plot(xdata, ydata)
ax.set_ylim(ymin=0)
plt.show(f)

如果在绘制前更改了ymin,将导致范围为[0,1].

If ymin is changed before plotting, this will result in a range of [0, 1].

ymin参数已由bottom替换:

the ymin argument has been replaced by bottom:

ax.set_ylim(bottom=0)

这篇关于使用matplotlib将y范围更改为从0开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆