cdonts
cdonts

Reputation: 9609

Show specific range in graphic

I'm just starting with matplotlib. For example, having the following code:

import pylab

x = [1, 2, 3, 4, 5, 6, 7]
y = [2, 3, 4, 5, 6, 7, 8]

pylab.plot(x, y)
pylab.axhline(0, color="black")
pylab.axvline(0, color="black")

pylab.show()

enter image description here

Shows from 0 to 8 (Y) and 0 to 7 (X). Is there anyway to specify the range of the values showed in the axes? For example, from -5 to 3 and from -5 to 3. It doesn't matter if the function line is out of that range.

Upvotes: 6

Views: 16573

Answers (1)

Lurendrejer
Lurendrejer

Reputation: 692

You can use pylab.{x|y}lim(min, max):

import pylab

x = [1, 2, 3, 4, 5, 6, 7]
y = [2, 3, 4, 5, 6, 7, 8]

pylab.plot(x, y)
pylab.axhline(0, color="black")
pylab.axvline(0, color="black")

# Here we go:
pylab.xlim(-5, 3)
pylab.ylim(-5, 3)

pylab.show()

Edit: I agree with @tcaswell. You should use the pyplot package instead:

import numpy as np
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 6, 7]
y = [2, 3, 4, 5, 6, 7, 8]

plt.plot(x, y)
plt.axhline(0, color="black")
plt.axvline(0, color="black")

plt.xlim(-5, 3)
plt.ylim(-5, 3)

plt.show()

Upvotes: 8

Related Questions