[英]How to set 'auto' for upper limit, but keep a fixed lower limit with matplotlib.pyplot
I want to set the upper limit of the y-axis to 'auto', but I want to keep the lower limit of the y-axis to always be zero.我想将 y 轴的上限设置为“自动”,但我想保持 y 轴的下限始终为零。 I tried 'auto' and 'autorange', but those don't seem to work.
我试过“自动”和“自动范围”,但这些似乎不起作用。 Thank you in advance.
先感谢您。
Here is my code:这是我的代码:
import matplotlib.pyplot as plt
def plot(results_plt,title,filename):
############################
# Plot results
# mirror result table such that each parameter forms an own data array
plt.cla()
#print results_plt
XY_results = []
XY_results = zip( *results_plt)
plt.plot(XY_results[0], XY_results[2], marker = ".")
plt.title('%s' % (title) )
plt.xlabel('Input Voltage [V]')
plt.ylabel('Input Current [mA]')
plt.grid(True)
plt.xlim(3.0, 4.2) #***I want to keep these values fixed"
plt.ylim([0, 80]) #****CHANGE**** I want to change '80' to auto, but still keep 0 as the lower limit
plt.savefig(path+filename+'.png')
You can pass just left
or right
to set_xlim
:您可以将
left
或right
传递给set_xlim
:
plt.gca().set_xlim(left=0)
For the y axis, use bottom
or top
:对于 y 轴,使用
bottom
或top
:
plt.gca().set_ylim(bottom=0)
只需为以下限制之一设置xlim
:
plt.xlim(left=0)
As aforementioned and according to the matplotlib documentation, the x-limits of a given axis ax
can be set using the set_xlim
method of the matplotlib.axes.Axes
class.如前所述,根据 matplotlib 文档,可以使用
matplotlib.axes.Axes
类的set_xlim
方法设置给定轴ax
的 x 限制。
For instance,例如,
>>> ax.set_xlim(left_limit, right_limit)
>>> ax.set_xlim((left_limit, right_limit))
>>> ax.set_xlim(left=left_limit, right=right_limit)
One limit may be left unchanged (eg the left limit):一个限制可以保持不变(例如左限制):
>>> ax.set_xlim((None, right_limit))
>>> ax.set_xlim(None, right_limit)
>>> ax.set_xlim(left=None, right=right_limit)
>>> ax.set_xlim(right=right_limit)
To set the x-limits of the current axis, the matplotlib.pyplot
module contains the xlim
function that just wraps matplotlib.pyplot.gca
and matplotlib.axes.Axes.set_xlim
.要设置当前轴的 x 限制,
matplotlib.pyplot
模块包含xlim
函数,该函数仅包装matplotlib.pyplot.gca
和matplotlib.axes.Axes.set_xlim
。
def xlim(*args, **kwargs):
ax = gca()
if not args and not kwargs:
return ax.get_xlim()
ret = ax.set_xlim(*args, **kwargs)
return ret
Similarly, for the y-limits, use matplotlib.axes.Axes.set_ylim
or matplotlib.pyplot.ylim
.同样,对于 y 限制,请使用
matplotlib.axes.Axes.set_ylim
或matplotlib.pyplot.ylim
。 The keyword arguments are top
and bottom
.关键字参数是
top
和bottom
。
Just add a point on @silvio 's: if you use axis to plot like figure, ax1 = plt.subplots(1,2,1)
.只需在@silvio 上添加一个点:如果您使用轴来绘制
figure, ax1 = plt.subplots(1,2,1)
。 Then ax1.set_xlim(xmin = 0)
also works!然后
ax1.set_xlim(xmin = 0)
也有效!
You can also do:你也可以这样做:
ax.set_xlim((None,upper_limit))
ax.set_xlim((lower_limit,None))
That is helpful if you want to use set(), which allows you to set several parameters at once:如果您想使用 set(),这会很有帮助,它允许您一次设置多个参数:
ax.set(xlim=(None, 3e9), title='my_title', xlabel='my_x_label', ylabel='my_ylabel')
The set_xlim
and set_ylim
permit None
values to achieve this. set_xlim
和set_ylim
允许None
值来实现这一点。 However, you must use the functions AFTER you have plotted the data.但是,您必须在绘制数据后使用这些函数。 If you don't do this, it will use the default 0 for left/bottom and 1 for top/right.
如果您不这样做,它将使用默认的 0 表示左/下和 1 表示上/右。 It does not recalculate the "automatic" limit each time you plot new data once you have set the limits.
一旦您设置了限制,它不会在每次绘制新数据时重新计算“自动”限制。
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([0, 1, 4, 5], [3, 5, 6, 9])
ax.set_xlim(left=2, right=None)
ax.set_ylim(bottom=None, top=7)
plt.show()
(Ie, in the above example, if you would do ax.plot(...)
at the end, it won't give the desired effect.) (即,在上面的示例中,如果您在最后执行
ax.plot(...)
,则不会产生预期的效果。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.