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


To give further explanation or guide readers to focus on certain remarkable details, annotations can be added to a figure. Matplotlib offers several modules to add text, arrows, and shapes that can be exploited.

Adding text annotations

Annotations can be added easily by specifying the desired locations through some built-in functions in Matplotlib.

Adding text and arrows with axis.annotate

Matplotlib has an axis.annotate function that draws an arrow extending across specified x and y coordinates and adds a text label if a string is input. The target coordinates of the pointed location and text label are assigned by the xy and xytext parameters in tuples, respectively. Here is an example of drawing the basic demand-supply curves we learn in high school economics:

import matplotlib.pyplot as plt
import numpy as np

# create 1000 equally spaced points between -10 and 10
x = np.linspace(0, 10)

# Prepare the data
y1 = x
y2 = 10-x

# Plot the data
fig, ax = plt.subplots()