Graphics with Python: Difference between revisions

From AstroEdWiki
Jump to navigation Jump to search
No edit summary
Line 3: Line 3:
== Installation of matplotlib ==
== Installation of matplotlib ==


The current version 1.2 may be included in some Linux distributions.  Version 1.1 has most of the features you will need now, and it is in Ubuntu and OpenSuse packages that can be added to your core Python system after you also install Numpy.  For example, under Ubuntu you would use
The current version 1.2 may be included in some Linux distributions.  Version 1.1 has most of the features you will need now, and it is in Ubuntu and OpenSuse packages that can be added to your core Python system after you also install numpy.  For example, under Ubuntu you would use


  sudo apt-get install python-matplotlib
  sudo apt-get install python-matplotlib


to get the most recent version available for your system.
to get the most recent version available for your system and resolve missing components.


For Windows and MacOS users, if you installed the [http://www.enthought.com/products/epd.php Enthought version of Python] you will have it "out of the box".  For others, look at the [http://matplotlib.org/users/installing.html matplotlib installation website] for directions on how to install it.  You will need numpy too, and it also comes in the Enthought collection.
For Windows and MacOS users, if you installed the [http://www.enthought.com/products/epd.php Enthought version of Python] you will have it "out of the box".  For others, look at the [http://matplotlib.org/users/installing.html matplotlib installation website] for directions on how to install it.  You will need numpy too, and it also comes in the Enthought collection.
Line 21: Line 21:
  print np.__version__
  print np.__version__
  print plt.__version__
  print plt.__version__


== Learning the basics of 2D data and function plotting ==
== Learning the basics of 2D data and function plotting ==

Revision as of 20:48, 18 February 2013

The Python matplotlib module provides tools for interactive 2-D and 3-D graphics, and for saving plots in file formats you can easily display on the web or in other programs, print, and incorporate in documents.

Installation of matplotlib

The current version 1.2 may be included in some Linux distributions. Version 1.1 has most of the features you will need now, and it is in Ubuntu and OpenSuse packages that can be added to your core Python system after you also install numpy. For example, under Ubuntu you would use

sudo apt-get install python-matplotlib

to get the most recent version available for your system and resolve missing components.

For Windows and MacOS users, if you installed the Enthought version of Python you will have it "out of the box". For others, look at the matplotlib installation website for directions on how to install it. You will need numpy too, and it also comes in the Enthought collection.


Once you have it installed, programs that use this library will have to import it with lines such as

import numpy as np
import matplotlib as plt

to make the functions available. With these, numpy functions will start with np. and mathplotlib functions will have plt. in front of the function name, which shortens the code you would write. You can check that your computer has numpy and matplotlib by trying these commands in interactive Python or Idle. The version numbers will be available too with

print np.__version__
print plt.__version__

Learning the basics of 2D data and function plotting

The matplotlib user's offers a tutorial with many examples, some of which we will look at here. There is also a helpful [ quick start guide written in three parts by an astrophysics grad student.