Book Image

Matplotlib 2.x By Example

By : Allen Yu, Claire Chung, Aldrin Yim
Book Image

Matplotlib 2.x By Example

By: Allen Yu, Claire Chung, Aldrin Yim

Overview of this book

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.
Table of Contents (15 chapters)
Title Page
About the Authors
About the Reviewer
Customer Feedback


Big data analytics drives innovation in scientific research, digital marketing, policy making, and much more. With the increasing amount of data from sensors, user activities, to APIs and databases, there is a need to visualize data effectively in order to communicate the insights to the target audience.

Matplotlib offers a simple but a powerful plotting library that helps to resolve the complexity in big data visualization, and turns overwhelming data into useful information. The library offers versatile plot types and robust customizations to transform data into persuasive and actionable figures. With the recent introduction of version 2, Matplotlib has further established its pivotal role in Python visualization.

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 with 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 and By extending the power of Matplotlib using toolkits such as GeoPandas, Lifelines, Mplot3d, NumPy, Pandas,, Scikit-learn, SciPy, and Seaborn, you will learn how to visualize geographical data on maps, implement interactive charts, and craft professional scientific visualizations from complex datasets. By the end of this book, you will become well-versed with Matplotlib in your day-to-day work and be able to create advanced data visualizations.

What this book covers

In the first part of this book, you will learn the basics of creating a Matplotlib plot:

  • Chapter 1, Hello Plotting World!, covers the basic constituents of a Matplotlib figure, as well as the latest features of Matplotlib version 2
  • Chapter 2, Figure Aesthetics, explains how to in customize the style of components in a Matplotlib figure
  • Chapter 3, Figure Layout and Annotations, explains how to add annotations and subplots, which allow more comprehensive representation of the data

Once we have a solid foundation of the basics of Matplotlib, in part two of this book, you will learn how to mix and match different techniques to create increasingly complex visualizations:

  • Chapter 4, Visualizing Online Data, teaches you how to design intuitive infographics for effective storytelling through the use of real-world datasets.
  • Chapter 5, Visualizing Multivariate Data, gives you an overview of the plot types that are suitable for visualizing datasets with multiple features or dimensions.
  • Chapter 6, Adding Interactivity and Animating Plots, shows you that Matplotlib is not limited to creating static plots. You will learn how to create interactive charts and animations.

Finally, in part three of this book, you will learn some practical considerations and data analysis routines that are relevant to scientific plotting:

  • Chapter 7, A Practical Guide to Scientific Plotting, explains that data visualization is an art that's closely coupled with statistics. As a data scientist, you will learn how to create visualizations that are not only understandable by yourself, but legible to your target audiences.
  • Chapter 8, Exploratory Data Analytics and Infographics, guides you through more advanced topics in geographical infographics and exploratory data analytics.

What you need for this book

These are the prerequisites for this book:

  • Basic Python knowledge is expected. Interested readers can refer to Learning Python by Fabrizio Romano if they are relatively new to Python programming.
  • A working installation of Python 3.4 or later is required. The default Python distribution can be obtained from Readers may also explore other Python distributions, such as Anaconda (, which provides better package dependency management.
  • A Windows 7+, macOS 10.10+, or Linux-based computer with 4 GB RAM or above is recommended.
  • The code examples are based on Matplotlib 2.x, Seaborn 0.8.0, Pandas 0.20.3, Numpy 1.13.1, SciPy 0.19.1, pycountry 17.5.14, stockstats 0.2.0, BeautifulSoup4 4.6.0, requests 2.18.4, plotly 2.0.14, scikit-learn 0.19.0, GeoPandas 0.2.1, PIL 1.1.6, and lifelines 0.11.1. Brief instructions for installing these packages are included in the chapters, but readers can refer to the official documentation pages for more details.

Who this book is for

This book aims to help anyone interested in data visualization to get insights from big data with Python and Matplotlib 2.x. Well-visualized data aids analysis and communication regardless of the field. This book will guide Python novices to quickly pick up Matplotlib plotting skills through step-by-step tutorials. Data scientists will learn to prepare high-quality figures for publications. News editors and copywriters will learn how to create intuitive infographics to make their message crisply understandable.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Navigate to the Applications folder, and then go into the Utilities folder."

A block of code is set as follows:

evens = []
with open as f:
    for line in f.readlines():

Any command-line input or output is written as follows:

sudo apt update
sudo apt install Python3 build-essential

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Select Markdown from the drop-down list on the toolbar."


Warnings or important notes appear like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply email [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at If you purchased this book elsewhere, you can visit and register to have the files emailed directly to you. You can download the code files by following these steps:

  1. Log in or register to our website using your email address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at We also have other code bundles from our rich catalog of books and videos available at Check them out!

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at [email protected] with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.