edit · print · PDF

Please note that all the SIEpedia's articles address specific issues or questions raised by IAC users, so they do not attempt to be rigorous or exhaustive, and may or may not be useful or applicable in different or more general contexts.

Python for astronomers

Python is becoming one the most used interpreted languages for data analysis, competing directly with well-stablish commercial solutions as Matlab or IDL. Apart from its simple syntax and very smooth learning curve, the main advantage of Python is that you can use it virtually for everything, having modules for database interaction, web development, parallel computing and a long etcetera. Of course, there mature scientific and mathematical libraries.

Beeing Python so extensive, its easy to get lost with all the possible modules and its documentation, because there are many options to do the same. Differing from monolithic-packages as IDL or Matlab, where you have one plotting library, one GUI library, a standard IDE (integrated Development Environment) and so on, all with a common documentation, in Python, as with any general use programming language, you have to choose the modules you need and install them, refering to the documentation of each module separately.

In this guide you will find which are the most common packages and documentation for astronomy and where to obtain them.

The basics

Obviously first you need Python itself. If you are using GNU/Linux or a Mac, its already installed by default on this systems. If you have Windows you can install the official package or WinPython (recommended) or Python(x,y) project which apart from Python includes the most common scientific packages and tools.

Scientific packages

The main scientific package is Scipy, which includes ODE solvers, linear algebra libraries, interpolation, optimization, etc. Scipy requires Numpy, a powerful arrays library.

  • Scipy - The main scientific library
  • Numpy - Arrays manipulation library
  • Matplotlib - 2D (and basic 3D) plotting library
  • IPython - An advanced interactive console

From GNU/Linux, you can try to obtain this packages using the package installer of your distribution (apt-get in Ubuntu/Debian, yum in Fedora or YAST in SUSE). The Python(x,y) package (Windows only) includes all this.

Other scientific modules


  • NLopt - Non-Linear optimization library
  • lmfit - It is a high-level interface to Scipy's optimize.leastsq and others.
  • OpenOPT - Numerical optimization framework

Statistics and other

  • Statsmodels - Data analysis and statistics
  • Pandas - Python Data Analysis Library
  • Sympy - Symbolic Mathematics Library

Astronomy packages

There are several modules to do the same thing, below are the most popular. Also take a look to Astropython, a knowledge base for research in astronomy using Python.

  • Astropy - A community effort to develop a single core package for Astronomy
  • Pyfits - FITS images and tables manipulation (already included in astropy)
  • PyRAF - A command language for IRAF written in Python. It also enable to call IRAF tasks from Python. Requires IRAF.
  • APLpy - Astronomical Plotting Library with matplotlib
  • Kapteyn Package - A very complete and well documented collection of libraries from the Kapteyn Astronomical Institute. It includes coordinate and WCS manipulation, tables and plotting utilities.
  • pywcsgrid2 - Astronomical plotting with matplotlib.
  • SAMPy - A SAMP implementation for Python.
  • pyregion - A ds9 region files parser.
  • CosmoPy - A cosmology package.
  • idlsave - An IDL's "save" file format reader (to numpy types). Do not requires IDL.
  • A Python Connection to DS9 via XPA
  • cosmics.py - L.A.Cosmic algorithm implementation in Python (for cosmic ray detection)
  • Chantipy - A Python interface to the CHIANTI atomic database for astrophysical spectroscopy
  • python-montage - A Python wrapper for montage
  • SunPy: An effort to create an open-source software library for solar physics
  • AsPyLib: A python library for FITS image processing, including photometry and astrometry.
  • pySpecKit: A splot-like spectroscopic analysis toolkit for astronomy
  • PEP - Python and C library for source extraction and photometry - Python and C library for source extraction and photometry
  • Optical simulation toolkit poppy and Simulated PDF for JWT webbpsf
  • pyAOS - Monte-Carlo Adaptive Optics Simulation toolkit
  • AOtools - A useful set of tools for Adaptive Optics in Python
  • SpecViz - 1D astronomical spectral visualization and analysis suite
  • SkyField - Positions for the stars, planets, and satellites in orbit around the Earth.

Other useful packages

  • Sympy - Impressive symbolic calculus module
  • Mayavi - A powerful 3D data visualizer (Matplotlib cannot do only make simple 3D plots)
  • f2py - Fortran to Python interface creator
  • imageio - Python library for reading and writing image data. Supports a lot of image and video formats.
  • AstroML: A Python module for machine learning and data mining


If you don't know anything about Python, just begin with official tutorial, its brief and clear. Then, the main reference for astronomers is the Perry Greenfield and Robert Jedrzejewski's Using Python for interactive data analysis in astronomy tutorial, which covers the Python's basics and all the important astronomical packages with many examples (many thanks Perry and Robert!). Remember that the reference website for scientific packages and documentation is Scipy.org.

Here are documentation of the main scientific packages:

Courses and tutorials

Video tutorials and demonstrations

edit · print · PDF
Page last modified on July 05, 2023, at 01:32 PM