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

Leverage the power of Matplotlib to visualize and understand your data more effectively

About This Book
  • Perform effective data visualization with Matplotlib and get actionable insights from your data
  • Design attractive graphs, charts, and 2D plots, and deploy them to the web
  • Get the most out of Matplotlib in this practical guide with updated code and examples
Who This Book Is For

This book is essentially for anyone who wants to create intuitive data visualizations using the Matplotlib library. If you're a data scientist or analyst and wish to create attractive visualizations using Python, you'll find this book useful. Some knowledge of Python programming is all you need to get started.

What You Will Learn
  • Create 2D and 3D static plots such as bar charts, heat maps, and scatter plots
  • Get acquainted with GTK+3, Qt5, and wxWidgets to understand the UI backend of Matplotlib
  • Develop advanced static plots with third-party packages such as Pandas, GeoPandas, and Seaborn
  • Create interactive plots with real-time updates
  • Develop web-based, Matplotlib-powered graph visualizations with third-party packages such as Django
  • Write data visualization code that is readily expandable on the cloud platform
In Detail

Python is a general-purpose programming language increasingly being used for data analysis and visualization. Matplotlib is a popular data visualization package in Python used to design effective plots and graphs. This is a practical, hands-on resource to help you visualize data with Python using the Matplotlib library.

Matplotlib for Python Developers, Second Edition shows you how to create attractive graphs, charts, and plots using Matplotlib. You will also get a quick introduction to third-party packages, Seaborn, Pandas, Basemap, and Geopandas, and learn how to use them with Matplotlib. After that, you'll embed and customize your plots in third-party tools such as GTK+3, Qt 5, and wxWidgets. You'll also be able to tweak the look and feel of your visualization with the help of practical examples provided in this book. Further on, you'll explore Matplotlib 2.1.x on the web, from a cloud-based platform using third-party packages such as Django. Finally, you will integrate interactive, real-time visualization techniques into your current workflow with the help of practical real-world examples.

By the end of this book, you'll be thoroughly comfortable with using the popular Python data visualization library Matplotlib 2.1.x and leveraging its power to build attractive, insightful, and powerful visualizations.

Style and approach

Step by step approach to learning the best of Matplotlib 2.1.x

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Matplotlib for Python Developers Second Edition
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Introduction to Matplotlib
    1. What is Matplotlib?
      1. Merits of Matplotlib
        1. Easy to use
        2. Diverse plot types
        3. Hackable to the core (only when you want)
        4. Open source and community support
      2. What's new in Matplotlib 2.x?
        1. Improved functionality and performance
          1. Improved color conversion API and RGBA support
          2. Improved image support
          3. Faster text rendering
          4. Change in the default animation codec
        2. Changes in default styles
      3. Matplotlib website and online documentation
      4. Output formats and backends 
        1. Static output formats
          1. Raster images
          2. Vector images
    2. Setting up Matplotlib
      1. Installing Python
        1. Python installation for Windows 
        2. Python installation for macOS
        3. Python installation for Linux
      2. Installing Matplotlib
        1. About the dependencies
        2. Installing the pip Python package manager
        3. Installing Matplotlib with pip
      3. Setting up Jupyter Notebook
        1. Starting a Jupyter Notebook session
        2. Running Jupyter Notebook on a remote server
        3. Editing and running code
        4. Manipulating notebook kernel and cells
        5. Embed your Matplotlib plots
        6. Documenting in Markdown
        7. Save your hard work!
    3. Summary
  8. Getting Started with Matplotlib
    1. Loading data
      1. List
      2. NumPy array
      3. pandas DataFrame
    2. Our first plots with Matplotlib
      1. Importing the pyplot
      2. Line plot
      3. Scatter plot
      4. Overlaying multiple data series in a plot
        1. Multiline plots
        2. Scatter plot to show clusters
        3. Adding a trendline over a scatter plot
    3. Adjusting axes, grids, labels, titles, and legends
      1. Adjusting axis limits
      2. Adding axis labels
      3. Adding a grid
      4. Titles and legends
        1. Adding a title
        2. Adding a legend
    4. A complete example
    5. Saving plots to a file
      1. Setting the output format
      2. Setting the figure resolution
      3. Jupyter support
        1. Interactive navigation toolbar
    6. Configuring Matplotlib
      1. Configuring within Python code
        1. Reverting to default settings
      2. Global setting via configuration rc file
        1. Finding the rc configuration file
        2. Editing the rc configuration file
    7. Summary
  9. Decorating Graphs with Plot Styles and Types
    1. Controlling the colors
      1. Default color cycle
      2. Single-lettered abbreviations for basic colors
      3. Standard HTML color names
      4. RGB or RGBA color code
      5. Hexadecimal color code
      6. Depth of grayscale
      7. Colormaps
      8. Creating custom colormaps
    2. Line and marker styles
      1. Marker styles
        1. Choosing the shape of markers
        2. Using custom characters as markers
        3. Adjusting marker sizes and colors
        4. Fine-tuning marker styles with keyword arguments
      2. Line styles
        1. Color
        2. Line thickness
        3. Dash patterns
          1. Designing a custom dash style
        4. Cap styles
      3. Spines
    3. More native Matplotlib plot types
      1. Choosing the right plot
      2. Histogram
      3. Bar plot
        1. Setting bar plot properties
        2. Drawing bar plots with error bars using multivariate data
      4. Mean-and-error plots
      5. Pie chart
      6. Polar chart
        1. Controlling radial and angular grids
    4. Text and annotations
      1. Adding text annotations
      2. Font
      3. Mathematical notations
        1. Mathtext
        2. LaTeX support
          1. External text renderer
      4. Arrows
    5. Using style sheets
      1. Applying a style sheet
      2. Creating own style sheet
      3. Resetting to default styles
    6. Aesthetics and readability considerations in styling
      1. Suitable font styles
      2. Effective use of colors
      3. Keeping it simple
    7. Summary
  10. Advanced Matplotlib
    1. Drawing Subplots
      1. Initiating a figure with plt.figure()
      2. Initiating subplots as axes with plt.subplot()
      3. Adding subplots with plt.figure.add_subplot()
      4. Initiating an array of subplots with plt.subplots()
        1. Shared axes
      5. Setting the margin with plt.tight_layout()
      6. Aligning subplots of different dimensions with plt.subplot2grid()
      7. Drawing inset plots with fig.add_axes()
      8. Adjusting subplot dimensions post hoc with plt.subplots_adjust
    2. Adjusting axes and ticks
      1. Customizing tick spacing with locators
        1. Removing ticks with NullLocator
        2. Locating ticks in multiples with MultipleLocator
        3. Locators to display date and time
      2. Customizing tick formats with formatters
      3. Using a non-linear axis scale
    3. More on Pandas-Matplotlib integration
      1. Showing distribution with the KDE plot
      2. Showing the density of bivariate data with hexbin plots
    4. Expanding plot types with Seaborn 
      1. Visualizing multivariate data with a heatmap
      2. Showing hierarchy in multivariate data with clustermap
    5. Image plotting
    6. Financial plotting
    7. 3D plots with Axes3D
    8. Geographical plotting
      1. Basemap
      2. GeoPandas
    9. Summary
  11. Embedding Matplotlib in GTK+3
    1. Installing and setting up GTK+3
      1. A brief introduction to GTK+3
      2. Introduction to the GTK+3 signal system
      3. Installing Glade
      4. Designing the GUI using Glade
    2. Summary
  12. Embedding Matplotlib in Qt 5
    1. A brief introduction to Qt 5 and PyQt 5
    2. Differences between Qt 4 and PyQt 4
      1. Introducing QT Creator / QT Designer
    3. Summary
  13. Embedding Matplotlib in wxWidgets Using wxPython
    1. A brief introduction to wxWidgets and wxPython
      1. Embedding Matplotlib in a GUI from wxGlade
    2. Summary
  14. Integrating Matplotlib with Web Applications
    1. Installing Docker
      1. Docker for Windows users
      2. Docker for Mac users
    2. More about Django
      1. Django development in Docker containers
      2. Starting a new Django site
      3. Installation of Django dependencies
      4. Django environment setup
      5. Running the development server
    3. Showing Bitcoin prices using Django and Matplotlib
      1. Creating a Django app
      2. Creating a simple Django view
      3. Creating a Bitcoin candlestick view
      4. Integrating more pricing indicators
      5. Integrating the image into a Django template
    4. Summary
  15. Matplotlib in the Real World
    1. Typical API data formats
      1. CSV
      2. JSON
    2. Importing and visualizing data from a JSON API
      1. Using Seaborn to simplify visualization tasks
    3. Scraping information from websites
    4. Matplotlib graphical backends
      1. Non-interactive backends
      2. Interactive backends
    5. Creating animated plot
    6. Summary
  16. Integrating Data Visualization into the Workflow
    1. Getting started
    2. Visualizing sample images from the dataset
      1. Importing the UCI ML handwritten digits dataset
      2. Plotting sample images
        1. Extracting one sample each of digits 0-9
        2. Examining the randomness of the dataset
        3. Plotting the 10 digits in subplots
    3. Exploring the data nature by the t-SNE method
      1. Understanding t-Distributed stochastic neighbor embedding 
        1. Importing the t-SNE method from scikit-learn
      2. Drawing a t-SNE plot for our data
    4. Creating a CNN to recognize digits
    5. Evaluating prediction results with visualizations
      1. Examining the prediction performance for each digit 
      2. Extracting falsely predicted images
    6. Summary