General Matplotlib Tips

Before we dive into the details of creating visualizations with Matplotlib, here are a few useful tips about using this package:

1. Importing Matplotlib

Just as we used the np shorthand for NumPy, and pd for Pandas, plt is the standard shorthand for Matplotlib:

import matplotlib.pyplot as plt

Note that Matplotlib is a huge library, so we are only importing the pyplot part of it. This is useful to save memory and speedup code. Otherwise we’ll be importing gigabytes of libraries even when we are just interested in using it to perform some trivial tasks.

2. Setting Styles directive can be used to choose different prettyfying styles for our figures. There a number of pre-defined styles provided by Matplotlib. For example, there’s a pre-defined style called ggplot, which tries to copy the look and feel of ggplot, a popular plotting package for R. Below are some examples, both code and visual outputs, of the available styles; you can go through the official reference sheet for a complete overview.

# Examples of available styles'classic')'ggplot')'seaborn-whitegrid')['dark_background', 'presentation'])

Get hands-on with 1200+ tech skills courses.