1

Basically I'm in a situation where I want to lock down the starting point of the graph depending on the first graph that's plotted.

e.g. If i do something like this.

import matplotlib.pyplot as plt
plt.plot([7,8,9,10], [1,4,9,16], 'yo')
plt.plot([1,9,11,12], [1,4,9,16], 'ro')
plt.show()

I would like a way to restrict the x-axis to start from 7 so (1,1) from the second plot will be removed.

Is there a way to do this? I could keep track of it myself but just curious if there's something built in to handle this.

Thanks.

4

3 回答 3

5

Matplotlib 为您提供两种方式:

import matplotlib.pyplot as plt
plt.plot([7,8,9,10], [1,4,9,16], 'yo')
plt.plot([1,9,11,12], [1,4,9,16], 'ro')
plt.xlim(xmin=7)
plt.show()

或更面向对象的方式

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([7,8,9,10], [1,4,9,16], 'yo')
ax.plot([1,9,11,12], [1,4,9,16], 'ro')
ax.set_xlim(xmin=7)
plt.show()

如果您不使用 IPython,我强烈推荐它,因为您可以创建轴对象,然后键入ax.<Tab>并查看所有选项。在这种情况下,自动完成可能是一件很棒的事情。

于 2013-01-18T05:03:47.557 回答
3

简而言之:plt.xlim()

长篇:

import matplotlib.pyplot as plt
x1, y1 = ([7,8,9,10], [1,4,9,16])
plt.plot(x1, y1, 'yo')
plt.xlim(min(x1), max(x1))
plt.plot([1,9,11,12], [1,4,9,16], 'ro')
plt.show()
于 2013-01-18T05:02:56.587 回答
3

您可以在第一个绘图 ( doc )之后关闭自动缩放:

ax = plt.gca()
ax.autoscale(enable=False)

这将锁定所有比例(您也可以分别执行 x 和 y )。

于 2013-01-18T16:21:59.057 回答