
Mastering Matplotlib 2.x
Effective Data Visualization techniques with Python
- 214 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
About this book
Understand and build beautiful and advanced plots with Matplotlib and Python
Key Features
- Practical guide with hands-on examples to design interactive plots
- Advanced techniques to constructing complex plots
- Explore 3D plotting and visualization using Jupyter Notebook
Book Description
In this book, you'll get hands-on with customizing your data plots with the help of Matplotlib. You'll start with customizing plots, making a handful of special-purpose plots, and building 3D plots. You'll explore non-trivial layouts, Pylab customization, and more about tile configuration. You'll be able to add text, put lines in plots, and also handle polygons, shapes, and annotations. Non-Cartesian and vector plots are exciting to construct, and you'll explore them further in this book. You'll delve into niche plots and visualize ordinal and tabular data. In this book, you'll be exploring 3D plotting, one of the best features when it comes to 3D data visualization, along with Jupyter Notebook, widgets, and creating movies for enhanced data representation. Geospatial plotting will also be explored. Finally, you'll learn how to create interactive plots with the help of Jupyter.
Learn expert techniques for effective data visualization using Matplotlib 3 and Python with our latest offering -- Matplotlib 3.0 Cookbook
What you will learn
- Deal with non-trivial and unusual plots
- Understanding Basemap methods
- Customize and represent data in 3D
- Construct Non-Cartesian and vector plots
- Design interactive plots using Jupyter Notebook
- Make movies for enhanced data representation
Who this book is for
This book is aimed at individuals who want to explore data visualization techniques. A basic knowledge of Matplotlib and Python is required.
Tools to learn more effectively

Saving Books

Keyword Search

Annotating Text

Listen to it instead
Information
Drawing on Plots
- How to put lines in place
- How to add text on plots
- How to play with polygons and shapes
- How to add different kinds of annotations
Putting lines in place
Adding horizontal and vertical lines
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
# Set up figure size and DPI for screen demo
plt.rcParams['figure.figsize'] = (6,4)
plt.rcParams['figure.dpi'] = 150
- We will create the simple sine plot that we saw in Chapter 1, Heavy Customization, as follows:
# Adding a horizontal and vertical line
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))

- Now, to add an annotation, say, a line that splits the region between stuff above and below 0.5, add a horizontal line using axhline(0.5), as shown here. ax stands for the x axis and gives a value in the y co-ordinate for the horizontal line:
# Adding a horizontal and vertical line
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.axhline(0.5)

- To color this horizontal line red, insert the following code:
# Adding a horizontal and vertical line
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.axhline(0.5, color='r')

- To add a vertical line right at the first maximum, input pi/2 and color this red, along with a dashed line:
# Adding a horizontal and vertical line
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.axhline(0.5, color='r')
plt.axvline(np.pi/2., color='r', linestyle='--')

Adding spans that cover whole regions
# Adding a horizontal and vertical span
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.axhspan(-0.5,0.5)

# Adding a horizontal and vertical span
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.axhspan(-0.5,0.5, alpha=0.5)

# Adding a horizontal and vertical span
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.axvspan(-0.5,0.5, color='k', alpha=0.5)

# Adding a horizontal and vertical span
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.axhspan(-0.5,0.5, alpha=0.5)

Table of contents
- Title Page
- Copyright and Credits
- About Packt
- Contributors
- Preface
- Heavy Customization
- Drawing on Plots
- Special Purpose Plots
- 3D and Geospatial Plots
- Interactive Plotting
- Other Books You May Enjoy
Frequently asked questions
- Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
- Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app