Python 在 matplotlib 中设置 y 轴限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3777861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setting y-axis limit in matplotlib
提问by Curious2learn
I need help with setting the limits of y-axis on matplotlib. Here is the code that I tried, unsuccessfully.
我需要在 matplotlib 上设置 y 轴限制的帮助。这是我尝试过的代码,但没有成功。
import matplotlib.pyplot as plt
plt.figure(1, figsize = (8.5,11))
plt.suptitle('plot title')
ax = []
aPlot = plt.subplot(321, axisbg = 'w', title = "Year 1")
ax.append(aPlot)
plt.plot(paramValues,plotDataPrice[0], color = '#340B8C',
marker = 'o', ms = 5, mfc = '#EB1717')
plt.xticks(paramValues)
plt.ylabel('Average Price')
plt.xlabel('Mark-up')
plt.grid(True)
plt.ylim((25,250))
With the data I have for this plot, I get y-axis limits of 20 and 200. However, I want the limits 20 and 250.
有了这个图的数据,我得到了 20 和 200 的 y 轴限制。但是,我想要限制 20 和 250。
回答by Eric O Lebigot
This should work. Your code works for me, like for Tamás and Manoj Govindan. It looks like you could try to update Matplotlib. If you can't update Matplotlib (for instance if you have insufficient administrative rights), maybe using a different backend with matplotlib.use()could help.
这应该有效。您的代码适用于我,例如 Tamás 和 Manoj Govindan。看起来您可以尝试更新 Matplotlib。如果您无法更新 Matplotlib(例如,如果您的管理权限不足),则使用不同的后端matplotlib.use()可能会有所帮助。
回答by thetarro
Your code works also for me. However, another workaround can be to get the plot's axis and then change only the y-values:
您的代码也适用于我。但是,另一种解决方法是获取绘图的轴,然后仅更改 y 值:
x1,x2,y1,y2 = plt.axis()
plt.axis((x1,x2,25,250))
x1,x2,y1,y2 = plt.axis()
plt.axis((x1,x2,25,250))
回答by Hima
Try this . Works for subplots too .
尝试这个 。也适用于子图。
axes = plt.gca()
axes.set_xlim([xmin,xmax])
axes.set_ylim([ymin,ymax])
回答by Steven C. Howell
To add to @Hima's answer, if you want to modify a current x or y limit you could use the following.
要添加到@Hima 的答案中,如果您想修改当前的 x 或 y 限制,您可以使用以下内容。
import numpy as np # you probably alredy do this so no extra overhead
fig, axes = plt.subplot()
axes.plot(data[:,0], data[:,1])
xlim = axes.get_xlim()
# example of how to zoomout by a factor of 0.1
factor = 0.1
new_xlim = (xlim[0] + xlim[1])/2 + np.array((-0.5, 0.5)) * (xlim[1] - xlim[0]) * (1 + factor)
axes.set_xlim(new_xlim)
I find this particularly useful when I want to zoom out or zoom in just a little from the default plot settings.
当我想从默认绘图设置缩小或放大一点时,我发现这特别有用。
回答by Birat Bose
One thing you can do is to set your axis range by yourself by using matplotlib.pyplot.axis.
您可以做的一件事是使用 matplotlib.pyplot.axis 自己设置轴范围。
matplotlib.pyplot.axis
matplotlib.pyplot.axis
from matplotlib import pyplot as plt
plt.axis([0, 10, 0, 20])
0,10 is for x axis range. 0,20 is for y axis range.
0,10 用于 x 轴范围。0,20 用于 y 轴范围。
or you can also use matplotlib.pyplot.xlim or matplotlib.pyplot.ylim
或者你也可以使用 matplotlib.pyplot.xlim 或 matplotlib.pyplot.ylim
matplotlib.pyplot.ylim
matplotlib.pyplot.ylim
plt.ylim(-2, 2)
plt.xlim(0,10)
回答by Rainald62
If an axes (generated by code below the code shown in the question) is sharingthe range with the first axes, make sure that you set the range afterthe last plot of that axes.
如果轴(由问题中显示的代码下方的代码生成)与第一个轴共享范围,请确保在该轴的最后一个绘图之后设置范围。
回答by Antti A
This worked at least in matplotlib version 2.2.2:
这至少在 matplotlib 版本 2.2.2 中有效:
plt.axis([None, None, 0, 100])
Probably this is a nice way to set up for example xmin and ymax only, etc.
可能这是设置例如仅 xmin 和 ymax 等的好方法。
回答by Reza Keshavarz
You can instantiate an object from matplotlib.pyplot.axesand call the set_ylim()on it. It would be something like this:
您可以从中实例化一个对象matplotlib.pyplot.axes并调用set_ylim()它。它会是这样的:
import matplotlib.pyplot as plt
axes = plt.axes()
axes.set_ylim([0, 1])
回答by loved.by.Jesus
Just for fine tuning. If you want to set only one of the boundaries of the axis and let the other boundary unchanged, you can choose one or more of the following statements
只是为了微调。如果只想设置轴的一个边界,而让另一个边界不变,可以选择以下一个或多个语句
plt.xlim(right=xmax) #xmax is your value
plt.xlim(left=xmin) #xmin is your value
plt.ylim(top=ymax) #ymax is your value
plt.ylim(bottom=ymin) #ymin is your value

