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

时间:2022-05-12 20:12:42

I'm creating a plot with a method similar to the following:

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

import pyplot as plt

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

plt.show()

The range of the x axis seems to get set to the range of the first list of x values that is passed to plt.plot(). Is there a way to have pyplot automatically set the lower limit of the x axis to the lowest value in all of the x_list variables passed to it (plus a bit of leeway), and to have it do the same for the upper limit, using the highest x value passed to plot (plus a bit of leeway)? Thank you.

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

1 个解决方案

#1


13  

Confusingly, your y_list contains the values being plotted along the x-axis. If you want matplotlib to use values from x_list as x-coordinates, then you should call

令人困惑的是,您的y_list包含沿x轴绘制的值。如果您希望matplotlib将x_list中的值用作x坐标,那么您应该调用

plt.plot(x_list, y_list)

Maybe this is the root of your problem. By default matplotlib sets the x and y limits big enough to include all the data plotted.

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

So with this change, matplotlib will now be using x_list as x-coordinates, and will automatically set the limits of the x-axis to be wide enough to display all the x-coordinates specified in x_list_of_lists.

因此,通过此更改,matplotlib现在将使用x_list作为x坐标,并将自动将x轴的限制设置为足够宽以显示x_list_of_lists中指定的所有x坐标。


However, if you wish to adjust the x limits, you could use the plt.xlim function.

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

So, to set the lower limit of the x-axis to the lowest value in all of the x_list variables (and similarly for the upper limit), you'd do this:

因此,要将x轴的下限设置为所有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)

Make sure you place this after all the calls to plt.plot and (of course) before plt.show().

确保在plt.plot和(当然)在plt.show()之前的所有调用之后放置它。

#1


13  

Confusingly, your y_list contains the values being plotted along the x-axis. If you want matplotlib to use values from x_list as x-coordinates, then you should call

令人困惑的是,您的y_list包含沿x轴绘制的值。如果您希望matplotlib将x_list中的值用作x坐标,那么您应该调用

plt.plot(x_list, y_list)

Maybe this is the root of your problem. By default matplotlib sets the x and y limits big enough to include all the data plotted.

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

So with this change, matplotlib will now be using x_list as x-coordinates, and will automatically set the limits of the x-axis to be wide enough to display all the x-coordinates specified in x_list_of_lists.

因此,通过此更改,matplotlib现在将使用x_list作为x坐标,并将自动将x轴的限制设置为足够宽以显示x_list_of_lists中指定的所有x坐标。


However, if you wish to adjust the x limits, you could use the plt.xlim function.

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

So, to set the lower limit of the x-axis to the lowest value in all of the x_list variables (and similarly for the upper limit), you'd do this:

因此,要将x轴的下限设置为所有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)

Make sure you place this after all the calls to plt.plot and (of course) before plt.show().

确保在plt.plot和(当然)在plt.show()之前的所有调用之后放置它。