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.

3 Answers 3

6

Matplotlib offers you two ways:

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()

or the more object-oriented way

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()

If you don't use IPython, I highly recommend it since you can create the axes object and then type ax.<Tab> and see all of your options. Autocomplete can be a wonderful thing in this case.

Sign up to request clarification or add additional context in comments.

Comments

3

You can turn auto-scaling off after the first plot (doc):

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

which will lock down all of the scales (you can do x and y separately as well).

Comments

3

In short: plt.xlim().

In long:

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.