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 2.x By Example

Book Description

Unlock deeper insights into visualization in form of 2D and 3D graphs using Matplotlib 2.x

About This Book

  • Create and customize live graphs, by adding style, color, font to make appealing graphs.
  • A complete guide with insightful use cases and examples to perform data visualizations with Matplotlib's extensive toolkits.
  • Create timestamp data visualizations on 2D and 3D graphs in form of plots, histogram, bar charts, scatterplots and more.

Who This Book Is For

This book is for anyone interested in data visualization, to get insights from big data with Python and Matplotlib 2.x. With this book you will be able to extend your knowledge and learn how to use python code in order to visualize your data with Matplotlib. Basic knowledge of Python is expected.

What You Will Learn

  • Familiarize with the latest features in Matplotlib 2.x
  • Create data visualizations on 2D and 3D charts in the form of bar charts, bubble charts, heat maps, histograms, scatter plots, stacked area charts, swarm plots and many more.
  • Make clear and appealing figures for scientific publications.
  • Create interactive charts and animation.
  • Extend the functionalities of Matplotlib with third-party packages, such as Basemap, GeoPandas, Mplot3d, Pandas, Scikit-learn, and Seaborn.
  • Design intuitive infographics for effective storytelling.

In Detail

Big data analytics are driving innovations in scientific research, digital marketing, policy-making and much more. Matplotlib offers simple but powerful plotting interface, versatile plot types and robust customization.

Matplotlib 2.x By Example illustrates the methods and applications of various plot types through real world examples.

It begins by giving readers the basic know-how on how to create and customize plots by Matplotlib. It further covers how to plot different types of economic data in the form of 2D and 3D graphs, which give insights from a deluge of data from public repositories, such as Quandl Finance. You will learn to visualize geographical data on maps and implement interactive charts.

By the end of this book, you will become well versed with Matplotlib in your day-to-day work to perform advanced data visualization. This book will guide you to prepare high quality figures for manuscripts and presentations. You will learn to create intuitive info-graphics and reshaping your message crisply understandable.

Style and approach

Step by step comprehensive guide filled with real world examples.

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 code file.

Table of Contents

  1. 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. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Hello Plotting World!
    1. Hello Matplotlib!
      1. What is Matplotlib?
      2. What's new in Matplotlib 2.0?
        1. Changes to the default style
          1. Color cycle
          2. Colormap
          3. Scatter plot
          4. Legend
          5.  Line style
          6. Patch edges and color
          7. Fonts
        2. Improved functionality or performance
          1. Improved color conversion API and RGBA support
          2. Improved image support
          3. Faster text rendering
          4. Change in the default animation codec
        3. Changes in settings
          1. New configuration parameters (rcParams)
          2. Style parameter blacklist
          3. Change in Axes property keywords
    2. Setting up the plotting environment
      1. Setting up Python
        1. Windows
          1. Using Python
        2. macOS
        3. Linux
      2. Installing the Matplotlib dependencies
        1. Installing the pip Python package manager
        2. Installing Matplotlib with pip
      3. Setting up Jupyter notebook
        1.  Why Jupyter notebook?
        2. Installing Jupyter notebook
      4. Using Jupyter notebook
        1. Starting a Jupyter notebook session
        2. Editing and running code
        3. Jotting down notes in Markdown mode
      5. Viewing Matplotlib plots
      6. Saving the notebook project
      7. All set to go!
    3. Plotting our first graph
      1. Loading data for plotting
        1. Data structures
          1. List
          2. Numpy array
          3. pandas dataframe
        2. Loading data from files
          1. The basic Python way
          2. The Numpy way
          3. The pandas way
      2. Importing the Matplotlib pyplot module
      3. Plotting a curve
      4. Viewing the figure
      5. Saving the figure
        1. Setting the output format
          1. PNG (Portable Network Graphics)
          2. PDF (Portable Document Format)
          3. SVG (Scalable Vector Graphics)
          4. Post (Postscript)
        2. Adjusting the resolution
    4. Summary
  3. Figure Aesthetics
    1. Basic structure of a Matplotlib figure
      1. Glossary of objects in a Matplotlib figure
    2. Setting colors in Matplotlib
      1. Single letters for basic built-in colors
      2. Names of standard HTML colors
      3. RGB or RGBA color code
      4. Hexadecimal color code
      5. Depth of grayscale
      6. Using specific colors in the color cycle
      7. Aesthetic and readability considerations
    3. Adjusting text formats
      1. Font
        1. Font appearance
          1. Font size
          2. Font style
          3. Font weight
          4. Font family
        2. Checking available fonts in system
      2. LaTeX support
    4. Customizing lines and markers
      1. Lines
        1. Choosing dash patterns
          1. Setting capstyle of dashes
          2. Advanced example
      2. Markers
        1. Choosing markers
        2. Adjusting marker sizes
    5. Customizing grids, ticks, and axes
      1. Grids
        1. Adding grids
      2. Ticks
        1. Adjusting tick spacing
          1. Removing ticks
          2. Drawing ticks in multiples
          3. Automatic tick settings
          4. Setting ticks by the number of data points
          5. Set scaling of ticks by mathematical functions
          6. Locating ticks by datetime
        2. Customizing tick formats
          1. Removing tick labels
          2. Fixing labels
          3. Setting labels with strings
          4. Setting labels with user-defined functions
          5. Formatting axes by numerical values
        3. Setting label sizes
        4. Trying out the ticker locator and formatter
        5. Rotating tick labels
      3. Axes
        1. Nonlinear axis
        2. Logarithmic scale
        3. Changing the base of the log scale
          1. Advanced example
        4. Symmetrical logarithmic scale
        5. Logit scale
    6. Using style sheets
      1. Applying a style sheet
      2. Resetting to default styles
      3. Customizing a style sheet
    7. Title and legend
      1. Adding a title to your figure
      2. Adding a legend
    8. Test your skills
    9. Summary
  4. Figure Layout and Annotations
    1. Adjusting the layout
      1. Adjusting the size of the figure
      2. Adjusting spines
      3. Adding subplots
        1. Adding subplots using pyplot.subplot
        2. Using pyplot.subplots() to specify handles
        3. Sharing axes between subplots
      4. Adjusting margins
        1. Setting dimensions when adding subplot axes with figure.add_axes
        2. Modifying subplot axes dimensions via pyplot.subplots_adjust
        3. Aligning subplots with pyplot.tight_layout
        4. Auto-aligning figure elements with pyplot.tight_layout
      5. Stacking subplots of different dimensions with subplot2grid
      6. Drawing inset plots
        1. Drawing a basic inset plot
        2. Using inset_axes
    2. Annotations
      1. Adding text annotations
        1. Adding text and arrows with axis.annotate
        2. Adding a textbox with axis.text
        3. Adding arrows
        4. Labeling data values on a bar chart
      2. Adding graphical annotations
        1. Adding shapes
      3. Adding image annotations
    3. Summary
  5. Visualizing Online Data
    1. Typical API data formats
      1. CSV
      2. JSON
      3. XML
    2. Introducing pandas
      1. Importing online population data in the CSV format
      2. Importing online financial data in the JSON format
    3. Visualizing the trend of data
      1. Area chart and stacked area chart
    4. Introducing Seaborn
    5. Visualizing univariate distribution
      1. Bar chart in Seaborn
      2. Histogram and distribution fitting in Seaborn
    6. Visualizing a bivariate distribution
      1. Scatter plot in Seaborn
    7. Visualizing categorical data
      1. Categorical scatter plot
      2. Strip plot and swarm plot
      3. Box plot and violin plot
    8. Controlling Seaborn figure aesthetics
      1. Preset themes
      2. Removing spines from the figure
      3. Changing the size of the figure
      4. Fine-tuning the style of the figure
      5. More about colors
      6. Color scheme and color palettes
    9. Summary
  6. Visualizing Multivariate Data
    1. Getting End-of-Day (EOD) stock data from Quandl
      1. Grouping the companies by industry
      2. Converting the date to a supported format
      3. Getting the percentage change of the closing price
    2. Two-dimensional faceted plots
      1. Factor plot in Seaborn
      2. Faceted grid in Seaborn
      3. Pair plot in Seaborn
    3. Other two-dimensional multivariate plots
      1. Heatmap in Seaborn
      2. Candlestick plot in matplotlib.finance
        1. Visualizing various stock market indicators
      3. Building a comprehensive stock chart
    4. Three-dimensional (3D) plots
      1. 3D scatter plot
      2. 3D bar chart
      3. Caveats of Matplotlib 3D
    5. Summary
  7. Adding Interactivity and Animating Plots
    1. Scraping information from websites
    2. Non-interactive backends
    3. Interactive backends
      1. Tkinter-based backend 
      2. Interactive backend for Jupyter Notebook 
      3. Plot.ly-based backend
    4. Creating animated plots
      1. Installation of FFmpeg
      2. Creating animations
    5. Summary
  8. A Practical Guide to Scientific Plotting
    1. General rules of effective visualization
      1. Planning your figure
        1. Do we need the plot?
        2. Choosing the right plot
        3. Targeting your audience
      2. Crafting your graph
        1. The science of visual perception
          1. The Gestalt principles of visual perception
        2. Getting organized
          1. Ordering plots and data series logically
          2. Grouping 
        3. Giving emphasis and avoiding clutter
          1. Color and hue
          2. Size and weight
          3. Spacing
          4. Typography
          5. Use minimal marker shapes
        4. Styling plots for slideshows, posters, and journal articles
          1. Display time
          2. Space allowed
          3. Distance from the audience
          4. Adaptations
          5. Summary of styling plots for slideshows, posters, and journal articles
    2. Visualizing statistical data more intuitively
      1. Stacked bar chart and layered histogram
      2. Replacing bar charts with mean-and-error plots
      3. Indicating statistical significance
      4. Methods for dimensions reduction
        1. Principal Component Analysis (PCA)
      5. t-distributed Stochastic Neighbor Embedding (t-SNE)
    3. Summary
  9. Exploratory Data Analytics and Infographics
    1. Visualizing population health information
      1. Map-based visualization for geographical data
      2. Combining geographical and population health data
    2. Survival data analysis on cancer
    3. Summary