software:matplotlib:matplotlib

This is an old revision of the document!


Matplotlib

Matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell (ala MATLAB®* or Mathematica®†), web application servers, and six graphical user interface toolkits.

Matplotlib, pylab and pyplot are all related. Matplotlib is the whole package; pylab is a module in matplotlib that gets installed alongside matplotlib; and matplotlib.pyplot is a module in matplotlib. There is a pyplot tutorial and numerous other sites that use matplotlib for their plot examples such as SciPy Cookbook for Matplotlib.

All interactive jobs should be run on a compute node by setting your workgroup and using qlogin. You need to be running X-Windows and you should make sure you are in the directory where your input and output files will be stored. In the example below, we are using the traine account in workgroup it-css to create the plot123.png file using Python packages Scipy and Matplotlib.

[(it_css:traine)@mills traine]$ qlogin
Your job 136505 ("QLOGIN") has been submitted
waiting for interactive job to be scheduled ...
Your interactive job 136505 has been successfully scheduled.
Establishing /opt/shared/GridEngine/local/qlogin_ssh session to host n016 ...
Last login: Mon Dec 10 15:05:04 2012 from mills.mills.hpc.udel.edu
[anita@n016 traine]$ vpkg_require scipy matplotlib
Adding dependency `python/2.7.2` to your environment
Adding dependency `python-nose/1.1.2-2.7` to your environment
Adding dependency `mkl/10.3.8-64bit` to your environment
Adding dependency `numpy/1.6.1-2.7` to your environment
Adding package `scipy/0.10.0-2.7` to your environment
Adding dependency `x11/RHEL6.1` to your environment
Adding dependency `pycairo/1.8.6-2.7.2` to your environment
Adding dependency `pygtk/2.16.0-2.7.2` to your environment
Adding package `matplotlib/1.1.0-2.7.2` to your environment
[anita@n016 traine]$ python
Python 2.7.2 (default, Dec 27 2011, 15:05:10)
[GCC 4.4.5 20110214 (Red Hat 4.4.5-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygtk
>>> import matplotlib
>>> from pylab import *
>>> plot([1,2,3])
[<matplotlib.lines.Line2D object at 0x242ce10>]
>>> show()
>>> quit()
[traine@n016 traine]$ ls *.png
plot123.png

After typing show() in the example above, a Figure window should appear displaying the plot. Now click on the Save the figure icon at the bottom of the Figure window. A Save the Figure window appears with the default file name image.png for the file format type Portable Network Graphics (png). In this example, we changed image to plot123 and the plot file plot123.png was saved in the current working directory. If you want a different file format, use the pull-down menu at the bottom of the Save the Figure window and this will automatically change the file name extension. When you close the Figure window it returns you back to Python.

If you need to create many plots, then you will want to use a non-interactive method by specifying the file format with matplotlib.use() and saving each plot using savefig(). In the example below we are only generating one plot by specifying AGG to use Portable Network Graphics (png) file format and savefig('plot-cs.png') to save this one plot instead of using show().

plot-ex.py
import matplotlib
matplotlib.use("AGG")

from pylab import *

X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)

plot(X,C)
plot(X,S)

# show()
savefig('plot-cs.png')

Now we can use this python plot example file and run it using a basic serial job script.

serial-plot.qs
# Setup the environment; add vpkg_require commands after this
# line:
vpkg_require scipy matplotlib

# Now append all of your shell commands necessary to run your program
# after this line:

python plot-ex.py

Now we can submit our batch run by using

qsub serial-plot.qs

This will create the plot file plot-cs.png in the current working directory where you submitted the job.

[(it_css:traine)@mills matplotlib]$ ls
plot-cs.png  plot-ex.py  serial-plot.qs  serial.qs.o138451

If you want to review the plots created, then you can use Imagemagick on the compute nodes via qlogin as long as you are running X-Windows. For example, using account traine and workgroup it_css, we display the plot-cs.png plot file.

[(it_css:traine)@mills matplotlib]$ qlogin
Your job 150600 ("QLOGIN") has been submitted
waiting for interactive job to be scheduled ...
Your interactive job 150600 has been successfully scheduled.
Establishing /opt/shared/GridEngine/local/qlogin_ssh session to host n016 ...
Last login: Wed Dec 19 15:24:03 2012 from mills.mills.hpc.udel.edu
[traine@n016 matplotlib]$ vpkg_require imagemagick
Adding dependency `x11/RHEL6.1` to your environment
Adding dependency `fftw/3.3-gcc-openmp` to your environment
Adding dependency `open64/4.5` to your environment
Adding dependency `ffmpeg/20120319-open64` to your environment
Adding package `imagemagick/6.7.6-3` to your environment
[traine@n016 matplotlib]$ display plot-cs.png

With some minor modifications from Text rendering with LaTeX - Matplotlib, we can create a plot called latex_demo.png with labels rendered by LaTeX.

latex_demo.py
import numpy as np
import matplotlib
matplotlib.use("AGG")
import matplotlib.pyplot as plt


# Example data
t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(4 * np.pi * t) + 2

plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.plot(t, s)

plt.xlabel(r'\textbf{time} (s)')
plt.ylabel(r'\textit{voltage} (mV)',fontsize=16)
plt.title(r"\TeX\ is Number "
          r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
          fontsize=16, color='gray')
# Make room for the ridiculously large title.
plt.subplots_adjust(top=0.8)

plt.savefig('latex_demo')

Using a basic serial job script like serial-plot-latex.qs below which loads the appropriate packages, then use qsub serial-plot-latex.qs will create the plot file latex_demo.png in the current working directory where you submitted the job.

serial-plot-latex.qs
# Setup the environment; add vpkg_require commands after this
# line:
vpkg_require numpy matplotlib
vpkg_require texlive

# Now append all of your shell commands necessary to run your program
# after this line:

python latex_demo.py
  • software/matplotlib/matplotlib.1524758040.txt.gz
  • Last modified: 2018-04-26 11:54
  • by sraskar