Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Data Visualization Cookbook (Second Edition)

You're reading from   Python Data Visualization Cookbook (Second Edition) Visualize data using Python's most popular libraries

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher
ISBN-13 9781784396695
Length 302 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (11) Chapters Close

Preface 1. Preparing Your Working Environment FREE CHAPTER 2. Knowing Your Data 3. Drawing Your First Plots and Customizing Them 4. More Plots and Customizations 5. Making 3D Visualizations 6. Plotting Charts with Images and Maps 7. Using the Right Plots to Understand Data 8. More on matplotlib Gems 9. Visualizations on the Clouds with Plot.ly Index

Customizing matplotlib's parameters in code

The library we will use the most throughout this book is matplotlib; it provides the plotting capabilities. Default values for most properties are already set inside the configuration file for matplotlib, called .rc file. This recipe describes how to modify matplotlib properties from our application code.

Getting ready

As we already said, matplotlib configuration is read from a configuration file. This file provides a place to set up permanent default values for certain matplotlib properties, well, for almost everything in matplotlib.

How to do it...

There are two ways to change parameters during code execution: using the dictionary of parameters (rcParams) or calling the matplotlib.rc() command. The former enables us to load an already existing dictionary into rcParams, while the latter enables a call to a function using a tuple of keyword arguments.

If we want to restore the dynamically changed parameters, we can use matplotlib.rcdefaults() call to restore the standard matplotlib settings.

The following two code samples illustrate previously explained behaviors:

  • An example for matplotlib.rcParams:
    import matplotlib as mpl
    mpl.rcParams['lines.linewidth'] = 2
    mpl.rcParams['lines.color'] = 'r'
    
  • An example for the matplotlib.rc() call:
    import matplotlib as mpl
    mpl.rc('lines', linewidth=2, color='r')
    

Both examples are semantically the same. In the second sample, we define that all subsequent plots will have lines with line width of 2 points. The last statement of the previous code defines that the color of every line following this statement will be red, unless we override it by local settings. See the following example:

import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0.0, 1.0, 0.01)

s = np.sin(2 * np.pi * t)
# make line red
plt.rcParams['lines.color'] = 'r'
plt.plot(t,s)

c = np.cos(2 * np.pi * t)
# make line thick
plt.rcParams['lines.linewidth'] = '3'
plt.plot(t,c)

plt.show()

How it works…

First, we import matplotlib.pyplot and NumPy to allow us to draw sine and cosine graphs. Before plotting the first graph, we explicitly set the line color to red using the plt.rcParams['lines.color'] = 'r' command.

Next, we go to the second graph (cosine function) and explicitly set the line width to three points using the plt.rcParams['lines.linewidth'] = '3' command.

If we want to reset specific settings, we should call matplotlib.rcdefaults().

In this recipe, we have seen how to customize the style of a matplotlib chart dynamically changing its configuration parameters. The matplotlib.rcParams object is the interface that we used to modify the parameters. It's global to the matplotlib packages and any change that we apply to it affects all the charts that we draw after.

You have been reading a chapter from
Python Data Visualization Cookbook (Second Edition)
Published in: Nov 2015
Publisher:
ISBN-13: 9781784396695
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime