Pyplot - 自动将x轴范围设置为传递给绘图功能的min,max x值

Lam*_*829 3 python matplotlib

我正在创建一个类似于以下方法的绘图:

import pyplot as plt

for x_list in x_list_of_lists:
   plt.plot(y_list, x_list)

plt.show()
Run Code Online (Sandbox Code Playgroud)

x轴的范围似乎设置为传递给plt.plot()的第一个x值列表的范围.有没有办法让pyplot自动将x轴的下限设置为传递给它的所有x_list变量中的最低值(加上一点余地),并让它为上限做同样的事情,使用传递给情节的最高x值(加上一点余地)?谢谢.

unu*_*tbu 15

令人困惑的是,您y_list包含沿着绘制的值x-axis.如果你想matplotlib使用x_listas中的值x-coordinates,那么你应该调用

plt.plot(x_list, y_list)
Run Code Online (Sandbox Code Playgroud)

也许这是你问题的根源.默认情况下,matplotlib将限制xy限制设置得足够大,以包括绘制的所有数据.

因此,通过此更改,matplotlib现在将使用x_listas x-coordinates,并将自动将其限制设置为x-axis足够宽以显示所有x-coordinates指定的内容x_list_of_lists.


但是,如果要调整x限制,可以使用plt.xlim函数.

因此,要将x-axis所有x_list变量中的下限设置为最低值(并且类似于上限),您可以这样做:

xmin = min([min(x_list) for x_list in x_list_of_lists])-delta
xmax = max([max(x_list) for x_list in x_list_of_lists])+delta
plt.xlim(xmin, xmax)
Run Code Online (Sandbox Code Playgroud)

确保在之前的所有电话plt.plot和(当然)之后放置此电话plt.show().