O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Matplotlib for Python Developers

Book Description

Build remarkable publication-quality plots the easy way

  • Create high quality 2D plots by using Matplotlib productively

  • Incremental introduction to Matplotlib, from the ground up to advanced levels

  • Embed Matplotlib in GTK+, Qt, and wxWidgets applications as well as web sites to utilize them in Python applications

  • Deploy Matplotlib in web applications and expose it on the Web using popular web frameworks such as Pylons and Django

  • Get to grips with hands-on code and complete realistic case study examples along with highly informative plot screenshots

In Detail

Providing appealing plots and graphs is an essential part of various fields such as scientific research, data analysis, and so on. Matplotlib, the Python 2D plotting library, is used to produce publication-quality figures in a variety of hardcopy formats and interactive environments across platforms. This book explains creating various plots, histograms, power spectra, bar charts, error charts, scatter-plots and much more using the powerful Matplotlib library to get impressive out-of-the-box results.

This book gives you a comprehensive tour of the key features of the Matplotlib Python 2D plotting library, right from the simplest concepts to the most advanced topics. You will discover how easy it is to produce professional-quality plots when you have this book to hand.

The book introduces the library in steps. First come the basics: introducing what the library is, its important prerequisites (and terminology), installing and configuring Matplotlib, and going through simple plots such as lines, grids, axes, and charts. Then we start with some introductory examples, and move ahead by discussing the various programming styles that Matplotlib allows, and several key features.

Further, the book presents an important section on embedding applications. You will be introduced to three of the best known GUI libraries—GTK+, Qt, and wxWidgets—and presented with the steps to implement to include Matplotlib in an application written using each of them. You will learn through an incremental approach: from a simple example that presents the peculiarities of the GUI library, to more complex ones, using GUI designer tools.

Because the Web permeates all of our activities, a part of the book is dedicated to showing how Matplotlib can be used in a web environment, and another section focuses on using Matplotlib with common Python web frameworks, namely, Pylons and Django. Last, but not least, you will go through real-world examples, where you will see some real situations in which you can use Matplotlib.

Table of Contents

  1. Matplotlib for Python Developers
    1. Matplotlib for Python Developers
    2. Credits
    3. About the Author
    4. About the Reviewers
    5. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Errata
        2. Piracy
        3. Questions
    6. 1. Introduction to Matplotlib
      1. Merits of Matplotlib
      2. Matplotlib web sites and online documentation
      3. Output formats and backends
        1. Output formats
        2. Backends
      4. About dependencies
        1. Build dependencies
      5. Installing Matplotlib
        1. Installing Matplotlib on Linux
        2. Installing Matplotlib on Windows
        3. Installing Matplotlib on Mac OS X
        4. Installing Matplotlib using packaged Python distributions
        5. Installing Matplotlib from source code
        6. Testing our installation
      6. Summary
    7. 2. Getting Started with Matplotlib
      1. First plots with Matplotlib
      2. Multiline plots
        1. A brief introduction to NumPy arrays
      3. Grid, axes, and labels
        1. Adding a grid
        2. Handling axes
        3. Adding labels
      4. Titles and legends
        1. Adding a title
        2. Adding a legend
      5. A complete example
      6. Saving plots to a file
      7. Interactive navigation toolbar
      8. IPython support
        1. Controlling the interactive mode
        2. Suppressing functions output
      9. Configuring Matplotlib
        1. Configuration files
        2. Configuring through the Python code
        3. Selecting backend from code
      10. Summary
    8. 3. Decorate Graphs with Plot Styles and Types
      1. Markers and line styles
        1. Control colors
          1. Specifying styles in multiline plots
        2. Control line styles
        3. Control marker styles
        4. Finer control with keyword arguments
      2. Handling X and Y ticks
      3. Plot types
        1. Histogram charts
        2. Error bar charts
        3. Bar charts
        4. Pie charts
        5. Scatter plots
      4. Polar charts
        1. Navigation Toolbar with polar plots
        2. Control radial and angular grids
      5. Text inside figure, annotations, and arrows
        1. Text inside figure
        2. Annotations
        3. Arrows
      6. Summary
    9. 4. Advanced Matplotlib
      1. Object-oriented versus MATLAB styles
        1. A brief introduction to Matplotlib objects
        2. Our first (simple) example of OO Matplotlib
      2. Subplots
        1. Multiple figures
        2. Additional Y (or X) axes
        3. Logarithmic axes
        4. Share axes
      3. Plotting dates
        1. Date formatting
        2. Axes formatting with axes tick locators and formatters
        3. Custom formatters and locators
      4. Text properties, fonts, and LaTeX
        1. Fonts
        2. Using LaTeX formatting
          1. Mathtext
          2. External TeX renderer
      5. Contour plots and image plotting
        1. Contour plots
        2. Image plotting
      6. Summary
    10. 5. Embedding Matplotlib in GTK+
      1. A brief introduction to GTK+
        1. Introduction to GTK+ signal system
      2. Embedding a Matplotlib figure in a GTK+ window
        1. Including a navigation toolbar
      3. Real-time plots update
      4. Embedding Matplotlib in a Glade application
        1. Designing the GUI using Glade
          1. Code to use Glade GUI
      5. Summary
    11. 6. Embedding Matplotlib in Qt 4
      1. Brief introduction to Qt 4 and PyQt4
      2. Embedding a Matplotlib figure in a Qt window
        1. Including a navigation toolbar
      3. Real-time update of a Matplotlib graph
      4. Embedding Matplotlib in a GUI made with Qt Designer
        1. Designing the GUI using Qt Designer
        2. Code to use the Qt Designer GUI
        3. Introduction to signals and slots
        4. Returning to the example
      5. Summary
    12. 7. Embedding Matplotlib in wxWidgets
      1. Brief introduction to wxWidgets and wxPython
      2. Embedding a Matplotlib figure in a wxFrame
        1. Including a navigation toolbar
      3. Real-time plots update
      4. Embedding Matplotlib in a GUI made with wxGlade
      5. Summary
    13. 8. Matplotlib for the Web
      1. Matplotlib and CGI
        1. What is CGI
        2. Configuring Apache for CGI execution
        3. Simple CGI example
        4. Matplotlib in a CGI script
        5. Passing parameters to a CGI script
      2. Matplotlib and mod_python
        1. What is mod_python
        2. Apache configuration for mod_python
        3. Matplotlib in a mod_python example
        4. Matplotlib and mod_python's Python Server Pages
      3. Web Frameworks and MVC
      4. Matplotlib and Django
        1. What is Django
        2. Matplotlib in a Django application
      5. Matplotlib and Pylons
        1. What is Pylons
        2. Matplotlib in a Pylons application
      6. Summary
    14. 9. Matplotlib in the Real World
      1. Plotting data from a database
      2. Plotting data from the Web
      3. Plotting data by parsing an Apache log file
      4. Plotting data from a CSV file
      5. Plotting extrapolated data using curve fitting
      6. Tools using Matplotlib
        1. NetworkX
        2. Mpmath
      7. Plotting geographical data
        1. First example
        2. Using satellite background
        3. Plot data over a map
        4. Plotting shapefiles with Basemap
      8. Summary