37

I am trying to plot some data from FITS files and I wanted to know if anyone knows how to focus on certain regions of a plot's axis? Here is some example code:

import pyfits
from matplotlib import pyplot as plt
from matplotlib import pylab
from pylab import *
#Assuming I have my data in the current directory
a = pyfits.getdata('fits1.fits')
x = a['data1'] # Lets assume data1 is the column: [0, 1, 1.3, 1.5, 2, 4, 8]
y = a['data2'] # And data2 is the column: [0, 0.5, 1, 1.5, 2, 2.5, 3]
plt.plot(x,y)

How could I only plot the region from [1.3 to 4] in the x-axis?

Hooked
  • 84,485
  • 43
  • 192
  • 261
Dax Feliz
  • 12,220
  • 8
  • 30
  • 33

2 Answers2

55

Use the plt.axis() function with your limits.

plt.axis([x_min, x_max, y_min, y_max])

where x_min, x_max, y_min, and y_max are the coordinate limits for both axes.

MaxPowers
  • 5,235
  • 2
  • 44
  • 69
  • @nye17 Using `xlim` / `ylim` or `axis` doesnt matter, since `axis` calls `xlim` / `ylim` to perform the operation. Visit the definition of `axis` in the [source code](https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes.py). `axis` is just a little more convenient since you can manipulate all four limits with one command. – MaxPowers Jul 10 '12 at 14:31
  • @MaxPowers You are right, I somehow mistook it as `add_axis`, my bad! – nye17 Jul 10 '12 at 18:03
25

This question has nothing to do with how you manipulate pyfits, but simply a matter of adding

plt.xlim(1.3, 4.0)

to your code before plt.show()

nye17
  • 12,857
  • 11
  • 58
  • 68