Book Image

Matplotlib 3.0 Cookbook

By : Srinivasa Rao Poladi, Nikhil Borkar
Book Image

Matplotlib 3.0 Cookbook

By: Srinivasa Rao Poladi, Nikhil Borkar

Overview of this book

Matplotlib provides a large library of customizable plots, along with a comprehensive set of backends. Matplotlib 3.0 Cookbook is your hands-on guide to exploring the world of Matplotlib, and covers the most effective plotting packages for Python 3.7. With the help of this cookbook, you'll be able to tackle any problem you might come across while designing attractive, insightful data visualizations. With the help of over 150 recipes, you'll learn how to develop plots related to business intelligence, data science, and engineering disciplines with highly detailed visualizations. Once you've familiarized yourself with the fundamentals, you'll move on to developing professional dashboards with a wide variety of graphs and sophisticated grid layouts in 2D and 3D. You'll annotate and add rich text to the plots, enabling the creation of a business storyline. In addition to this, you'll learn how to save figures and animations in various formats for downstream deployment, followed by extending the functionality offered by various internal and third-party toolkits, such as axisartist, axes_grid, Cartopy, and Seaborn. By the end of this book, you'll be able to create high-quality customized plots and deploy them on the web and on supported GUI applications such as Tkinter, Qt 5, and wxPython by implementing real-world use cases and examples.
Table of Contents (17 chapters)

Changing and resetting default environment variables

Matplotlib uses the matplotlibrc file to store default values for various environment and figure parameters used across matplotlib functionality. Hence, this file is very long. These default values are customizable to apply for all the plots within a session.

You can use the print(matplotlib.rcParams) command to get all the default parameter settings from this file.

The matplotlib.rcParams command is used to change these default values to any other supported values, one parameter at a time. The matplotlib.rc command is used to set default values for multiple parameters within a specific group, for example, lines, font, text, and so on. Finally, the matplotlib.rcdefaults() command is used to restore default parameters.

The matplotlib.rcsetup() command is used internally by Matplotlib to validate that the parameters being changed are acceptable values.

Getting ready

The following code block provides the path to the file containing all configuration the parameters:

# Get the location of matplotlibrc file
import matplotlib
matplotlib.matplotlib_fname()

You should see the directory path like the one that follows. The exact directory path depends on your installation:

'C:\\Anaconda3\\envs\\keras35\\lib\\site-packages\\matplotlib\\mpl-
data\\matplotlibrc'

How to do it...

The following block of code along with comments helps you to understand the process of changing and resetting default environment variables:

  1. Import the matplotlib.pyplot package with the plt synonym:
import matplotlib.pyplot as plt
  1. Load x and y variables from same test.csv file that we used in the preceding recipe:
x, y = np.loadtxt ('test.csv', unpack = True, usecols = (0,1), 
delimiter = ',')
  1. Change the default values for multiple parameters within the group 'lines':
matplotlib.rc('lines', linewidth=4, linestyle='-', marker='*')
  1. Change the default values for parameters individually:
matplotlib.rcParams['lines.markersize'] = 20
matplotlib.rcParams['font.size'] = '15.0'
  1. Plot the graph:
plt.plot(x,y)
  1. Display the graph:
plt.show()

The following is the output that will be obtained:

How it works...

The matplotlib.rc and matplotlib.rcParams commands overwrite the default values for specified parameters as arguments in these commands. These new values will be used by the pyplot tool while plotting the graph.

It should be noted that these values will be active for all plots in the session. If you want different settings for each plot in the same session, then you should use the attributes available with the plot command.

There's more...

You can reset all the parameters to their default values, using the rsdefaults() command, as shown in the following block:

# To restore all default parameters
matplotlib.rcdefaults()
plt.plot(x,y)
plt.show()

The graph will look as follows:

.