在matplotlib中设置y轴限制。

时间:2022-07-13 20:18:37

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之间。

6 个解决方案

#1


342  

Try this . Works for subplots too .

试试这个。也适用于次要情节。

axes = plt.gca()
axes.set_xlim([xmin,xmax])
axes.set_ylim([ymin,ymax])

#2


93  

Your code works also for me. However, another workaround can be to get the plot's axis and then change only the y-values:

你的代码也适用于我。但是,另一个解决方案是获取plot的轴,然后只更改y值:

x1,x2,y1,y2 = plt.axis()
plt.axis((x1,x2,25,250))

x1,x2,y1,y2 = plt.axis()plt.axis((x1,x2,25250))

#3


6  

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.

这应该工作。你的代码对我很有用,比如Tamas和Manoj Govindan。看起来您可以尝试更新Matplotlib。如果您不能更新Matplotlib(例如,如果您没有足够的管理权限),则可能使用不同的后端与Matplotlib .use()可以提供帮助。

#4


6  

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.

当我想要缩小或缩小一些默认的情节设置时,我觉得这特别有用。

#5


4  

One thing you can do is to set your axis range by yourself by using matplotlib.pyplot.axis.

你可以做的一件事是用matplotlib.pyplot.axis设置你自己的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)

#6


0  

If an axes (generated by code below the code shown in the question) is sharing the range with the first axes, make sure that you set the range after the last plot of that axes.

如果一个轴(由问题所示代码下面的代码生成)是与第一个轴共享的范围,请确保在该轴的最后一块区域之后设置范围。

#1


342  

Try this . Works for subplots too .

试试这个。也适用于次要情节。

axes = plt.gca()
axes.set_xlim([xmin,xmax])
axes.set_ylim([ymin,ymax])

#2


93  

Your code works also for me. However, another workaround can be to get the plot's axis and then change only the y-values:

你的代码也适用于我。但是,另一个解决方案是获取plot的轴,然后只更改y值:

x1,x2,y1,y2 = plt.axis()
plt.axis((x1,x2,25,250))

x1,x2,y1,y2 = plt.axis()plt.axis((x1,x2,25250))

#3


6  

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.

这应该工作。你的代码对我很有用,比如Tamas和Manoj Govindan。看起来您可以尝试更新Matplotlib。如果您不能更新Matplotlib(例如,如果您没有足够的管理权限),则可能使用不同的后端与Matplotlib .use()可以提供帮助。

#4


6  

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.

当我想要缩小或缩小一些默认的情节设置时,我觉得这特别有用。

#5


4  

One thing you can do is to set your axis range by yourself by using matplotlib.pyplot.axis.

你可以做的一件事是用matplotlib.pyplot.axis设置你自己的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)

#6


0  

If an axes (generated by code below the code shown in the question) is sharing the range with the first axes, make sure that you set the range after the last plot of that axes.

如果一个轴(由问题所示代码下面的代码生成)是与第一个轴共享的范围,请确保在该轴的最后一块区域之后设置范围。