matplotlib

From Infogalactic: the planetary knowledge core
Jump to: navigation, search
matplotlib
300px
300px
Circular diffraction pattern using matplotlib
Original author(s) John D. Hunter
Developer(s) Michael Droettboom, et al.
Stable release 1.5.0 (30 October 2015; 8 years ago (2015-10-30)) [±]
Written in Python
Operating system Cross-platform
Type Plotting
License matplotlib license
Website matplotlib.org

matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like wxPython, Qt, or GTK+. There is also a procedural "pylab" interface based on a state machine (like OpenGL), designed to closely resemble that of MATLAB. SciPy makes use of matplotlib.

matplotlib was originally written by John D. Hunter, has an active development community,[1] and is distributed under a BSD-style license. Michael Droettboom was nominated as matplotlib's lead developer shortly before John Hunter's death in 2012.[2]

As of 30 October 2015, matplotlib 1.5.x supports Python versions 2.7 through 3.5. Matplotlib 1.2 is the first version of matplotlib to support Python 3.x. Matplotlib 1.4 is the last version of matplotlib to support Python 2.6.[3]

Comparison with MATLAB

The pylab interface makes matplotlib easy to learn for experienced MATLAB users, making it a viable alternative to MATLAB as a teaching tool for numerical mathematics and signal processing.

Some of the advantages of the combination of Python, NumPy, and matplotlib over MATLAB include:

  • Based on Python, a full-featured modern object-oriented programming language suitable for large-scale software development
  • Free, open source, no license servers
  • Native SVG support

Typically pylab is imported to bring NumPy and matplotlib into a single global namespace for the most MATLAB like syntax, however a more explicit import style, which names both matplotlib and NumPy, is the preferred coding style.[4]

Comparison with Gnuplot

Both Gnuplot and Matplotlib are mature open source projects. They both can produce enormous types of different plots. While it is hard to specify a type of figure that one can do and the other can not, they still have different advantages and disadvantages:

Advantages Disadvantages
Matplotlib
  • Default plot styles with built-in code
  • Deep integration with Python
  • Matlab style programming interface (this is an advantage for some, but a disadvantage for others).
  • Heavily relied on other packages, such as Numpy.
  • Only works for Python: hard/impossible to be used in languages other than Python. (But can be used from Julia via PyPlot package)
Gnuplot
  • Cross-language solution: can be used as a plot engine in applications (e.g. GNU Octave, Maxima (software), JavaGnuplotHybrid) written in different languages through pipes or files.
  • Standalone program: no external dependencies.
  • Very fast when processing large datasets.
  • Easier to manipulate plot details
  • Old default plot styles: need a little bit of small tweaks to produce an attractive figure.
  • A smaller number (compared to Matplotlib) of active members in development.

Examples

Line plot

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> a = np.linspace(0,10,100)
>>> b = np.exp(-a)
>>> plt.plot(a,b)
>>> plt.show()

Histogram

>>> from numpy.random import normal,rand
>>> x = normal(size=200)
>>> plt.hist(x,bins=30)
>>> plt.show()

Scatter plot

>>> a = rand(100)
>>> b = rand(100)
>>> plt.scatter(a,b)
>>> plt.show()

3D plot

>>> from matplotlib import cm
>>> from mpl_toolkits.mplot3d import Axes3D
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> fig = plt.figure()
>>> ax = fig.gca(projection='3d')
>>> X = np.arange(-5, 5, 0.25)
>>> Y = np.arange(-5, 5, 0.25)
>>> X, Y = np.meshgrid(X, Y)
>>> R = np.sqrt(X**2 + Y**2)
>>> Z = np.sin(R)
>>> surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm)
>>> plt.show()

More examples

Toolkits

Several toolkits are available which extend matplotlib functionality. Some are separate downloads, others ship with the matplotlib source code but have external dependencies.[5]

  • Basemap: map plotting with various map projections, coastlines, and political boundaries[6]
  • Cartopy: a mapping library featuring object-oriented map projection definitions, and arbitrary point, line, polygon and image transformation capabilities.[7] (matplotlib v1.2 and above)
  • Excel tools: utilities for exchanging data with Microsoft Excel
  • GTK tools: interface to the GTK+ library
  • Qt interface
  • Mplot3d: 3-D plots
  • Natgrid: interface to the natgrid library for gridding irregularly spaced data.

Related projects

References

  1. Lua error in package.lua at line 80: module 'strict' not found.
  2. Lua error in package.lua at line 80: module 'strict' not found.
  3. Lua error in package.lua at line 80: module 'strict' not found.
  4. Lua error in package.lua at line 80: module 'strict' not found.
  5. Lua error in package.lua at line 80: module 'strict' not found.
  6. Lua error in package.lua at line 80: module 'strict' not found.
  7. Lua error in package.lua at line 80: module 'strict' not found.
  8. Lua error in package.lua at line 80: module 'strict' not found.
  9. Lua error in package.lua at line 80: module 'strict' not found.
  10. Lua error in package.lua at line 80: module 'strict' not found.
  11. Lua error in package.lua at line 80: module 'strict' not found.
  12. Lua error in package.lua at line 80: module 'strict' not found.
  13. Lua error in package.lua at line 80: module 'strict' not found.

External links