4

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

import pyplot as plt

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

plt.show()

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

4

1 回答 1

18

令人困惑的是,您y_list包含沿x-axis. 如果您希望 matplotlib 使用来自x_listas的值x-coordinates,那么您应该调用

plt.plot(x_list, y_list)

也许这是你问题的根源。默认情况下,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)

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

于 2013-07-28T12:28:04.300 回答