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
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Customizing grids, ticks, and axes


Lines of grids, ticks, and axes help us to visually locate and measure the data values. Their distribution and style determine whether they make good visual aids for the plot or clutter the figure. We will demonstrate the basic methods here.

Grids

Sometimes it may not be easy to tell the coordinates of any point in the plot. Grid lines extend from axis tick marks and help us estimate the value at a certain position.

Adding grids

Grids can be added by calling pyplot.grid(). By default, grid lines will be added at major tick marks. As in other line features, pyplot.grid() takes in parameters such as linewidth (lw), linestyle (ls), and color (c):

import numpy as np
import matplotlib.pyplot as plt

# Prepare 100 evenly spaced numbers from 0 to 200
evens = np.linspace(0,200,100)

# Plot a square curve
plt.plot(evens,evens**2,label = 'x^2')

# Adding grid lines
plt.grid()

plt.legend()
plt.show()

Here you see the default grid lines extending from axis ticks:

In the...