Book Image

Mastering matplotlib

By : Duncan M. McGreggor, Duncan M McGreggor
Book Image

Mastering matplotlib

By: Duncan M. McGreggor, Duncan M McGreggor

Overview of this book

Table of Contents (16 chapters)
Mastering matplotlib
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

In just over a decade, matplotlib has grown to offer the Python scientific computing community a world-class plotting and visualization library. When combined with related projects, such as Jupyter, NumPy, SciPy, and SymPy, matplotlib competes head-to-head with commercial software, which is far more established in the industry. Furthermore, the growth experienced by this open source software project is reflected again and again by individuals around the world, who make their way through the thorny wilds that face the newcomer and who develop into strong intermediate users with the potential to be very productive.

In essence, Mastering matplotlib is a very practical book. Yet every chapter was written considering this learning process, as well as a larger view of the same. It is not just the raw knowledge that defines how far developers progress in their goal. It is also the ability of motivated individuals to apply meta-levels of analysis to the problem and the obstacles that must be surmounted. Implicit in the examples that are provided in each chapter are multiple levels of analysis, which are integral to the mastery of the subject matter. These levels of analysis involve the processes of defining the problem, anticipating potential solutions, evaluating approaches without losing focus, and enriching your experience with a wider range of useful projects.

Finding resources that facilitate developers in their journey towards advanced knowledge and beyond can be difficult. This is not due to the lack of materials. Rather, it is because of the complex interaction of learning styles, continually improving codebases with strong legacies, and the very flexible nature of the Python programming language itself. The matplotlib developers who aspire to attain an advanced level, must tackle all of this and more. This book aims to be a guide for those in search of such mastery.

What this book covers

Chapter 1, Getting Up to Speed, covers some history and background of matplotlib, goes over some of the latest features of the library, provides a refresher on Python 3 and IPython Notebooks, and whets the reader's appetite with some advanced plotting examples.

Chapter 2, The matplotlib Architecture, reviews the original design goals of matplotlib and then proceeds to discuss its current architecture in detail, providing visualizations of the conceptual structure and relationships between the Python modules.

Chapter 3, matplotlib APIs and Integrations, walks the reader through the matplotlib APIs, adapting a single example accordingly, examines how third-party libraries are integrated with matplotlib, and gives migration advice to the advanced users of the deprecated pylab API.

Chapter 4, Event Handling and Interactive Plots, provides a review of the event-based systems, covers event loops in matplotlib and IPython, goes over a selection of matplotlib events, and shows how to take advantage of these to create interactive plots.

Chapter 5, High-level Plotting and Data Analysis, combines the interrelated topics, providing a historical background of plotting, a discussion on the grammar of graphics, and an overview of high-level plotting libraries. This is then put to use in a detailed analysis of weather-related data that spans 120 years.

Chapter 6, Customization and Configuration, covers the custom styles in matplotlib and the use of grid specs to create a dashboard effect with the combined plots. The lesser-known configuration options are also discussed with an eye to optimization.

Chapter 7, Deploying matplotlib in Cloud Environments, explores a use case for matplotlib in a remote deployment, which is followed by a detailed programmatic batch-job example using Docker and Amazon AWS.

Chapter 8, matplotlib and Big Data, provides detailed examples of working with large local data sets, as well as distributed ones, covering options such as numpy.memmap, HDF5, and Hadoop. Plots with millions of points will also be demonstrated.

Chapter 9, Clustering for matplotlib, introduces parallel programming and clusters that are designed for use with matplotlib, demonstrating how to distribute the parts of a problem and then assemble the results for analysis in matplotlib.

What you need for this book

For this book, you will need Python 3.4.2 or a later version of this as is available with Ubuntu 15.04 and Mac OS X 10.10. This book was written using Python 3.4.2 on Mac OS X.

You will also need graphviz, HDF5, and their respective development libraries installed. Obtaining the code for each chapter depends upon the Git binary being present on your system. The other software packages that are used in this book will be automatically downloaded and installed for you in a virtual environment when you clone and set up the code for each chapter. Some of the chapters explore the use of matplotlib in Cloud environments. This is demonstrated by using Amazon AWS. As such, an AWS account will be needed for the users who wish to go through all the steps for these chapters.

If you are new to Python 3, the first chapter provides a brief overview of the same. It will provide you with the level of comfort that is needed when dealing with the examples in the book.

Who this book is for

If you are a scientist, programmer, software engineer, or a student who has working knowledge of matplotlib and now want to extend your usage of matplotlib to plot complex graphs and charts and handle large datasets, then this book is for you.

Conventions

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: "The axes and projections directories form a crucial part of the artist layer."

A block of code is set as follows:

#! /usr/bin/env python3.4
import matplotlib.pyplot as plt

def main () -> None:
  plt.plot([1,2,3,4])
  plt.ylabel('some numbers')
  plt.savefig('simple-line.png')

if __name__ == '__main__':
  main()

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

$ git clone https://github.com/masteringmatplotlib/architecture.git
$ cd architecture
$ make

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: " For instance, when the Zoom-to-Rectangle button is clicked, the mode will be set to zoom rect "

Note

Warnings or important notes appear in a box like this.

Tip

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 e-mail , 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 www.packtpub.com/authors.

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

Each chapter in Mastering matplotlib provides instructions on obtaining the example code and notebook from Github. A master list has been provided at https://github.com/masteringmatplotlib/notebooks. You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. 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.

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 https://www.packtpub.com/sites/default/files/downloads/7542OS_ColoredImages.pdf.

Errata

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 http://www.packtpub.com/submit-errata, 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 https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

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 with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

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