Cur*_*arn 366 python matplotlib
我需要帮助设置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))
根据我对该图的数据,我得到20和200的y轴限制.但是,我想要限制20和250.
Him*_*ima 530
试试这个 .适用于次要情节.
axes = plt.gca()
axes.set_xlim([xmin,xmax])
axes.set_ylim([ymin,ymax])
小智 116
您的代码也适用于我.但是,另一种解决方法是获取绘图的轴,然后仅更改y值:
x1,x2,y1,y2 = plt.axis()
plt.axis((x1,x2,25,250))
Bir*_*ose 54
您可以做的一件事是使用matplotlib.pyplot.axis自行设置轴范围.
matplotlib.pyplot.axis
from matplotlib import pyplot as plt
plt.axis([0, 10, 0, 20])
0,10表示x轴范围.0,20表示y轴范围.
或者您也可以使用matplotlib.pyplot.xlim或matplotlib.pyplot.ylim
matplotlib.pyplot.ylim
plt.ylim(-2, 2)
plt.xlim(0,10)
lov*_*sus 22
只是为了微调。如果只想设置轴的一个边界,而让另一个边界不变,可以选择以下一个或多个语句
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
Rez*_*arz 13
您可以从中实例化对象matplotlib.pyplot.axes并对其进行调用set_ylim()。就像这样:
import matplotlib.pyplot as plt
axes = plt.axes()
axes.set_ylim([0, 1])
Ste*_*ell 12
要添加@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)
当我想从默认的绘图设置中缩小或放大一点时,我觉得这特别有用.
这至少在matplotlib 2.2.2版中有效:
plt.axis([None, None, 0, 100])
大概这是设置例如xmin和ymax等的好方法。
这应该工作.你的代码适合我,比如Tamás和Manoj Govindan.看起来您可以尝试更新Matplotlib.如果您无法更新Matplotlib(例如,如果您没有足够的管理权限),可能使用不同的后端matplotlib.use()可能有所帮助.
| 归档时间: | 
 | 
| 查看次数: | 792565 次 | 
| 最近记录: |