Using Core Matplotlib Syntax

Contents

Using Core Matplotlib Syntax#

There are a few different options for integrating core matplotlib syntax and workflows with data in a Pandas DataFrame and Pandas plotting functions.

The most straightforward workflow is to assign the Pandas plotting function output to a Matplotlib axes variable (i.e. ax).

import pandas as pd, matplotlib.pyplot as plt # import statement
df = pd.read_csv('https://raw.githubusercontent.com/kwaldenphd/elements-of-computing/main/book/data/ch10/air_quality_no2.csv', index_col=0, parse_dates=True) # load data
fig, ax = plt.subplots() # figure with axes
ax = df['station_paris'].plot(xlabel = 'Paris Station', ylabel = 'No2 Level', title = 'Air Quality Level Readings') # axes object from pandas plotting function
plt.show() # show output

We can use that workflow to set axis labels and the plot title using core Matplotlib syntax.

fig, ax = plt.subplots () # figure with axes
ax = df['station_paris'].plot() # axes object from pandas plotting function
ax.set_xlabel('Paris Station') # set x axis label
ax.set_ylabel('No2 Reading') # set y axis label
ax.set_title('Air Quality Readings') # set title
plt.show() # plot show

Style Sheets#

We can also combine matplotlib’s style sheets with Pandas plotting syntax.

plt.style.use('ggplot') # set style
fig, ax = plt.subplots() # figure with axes
ax = df['station_paris'].plot(xlabel = 'Paris Station', ylabel = 'No2 Level', title = 'Air Quality Level Readings') # axes object from pandas plotting function
plt.show() # show output