Plotting a Pandas Pivot Table in Python

Plotting data that is organized into pivot table has a slightly different syntax than plotting a columns in a dataframe.

Setting up Your Pivot Table

unpivoted = df.groupby(['year', 'month'])['quantity'].mean().reset_index()

pivoted = unpivoted.pivot(
    columns='year',
    index='month',
    values='quantity')

Using Seaborn to Plot your Pivot Table

sns.set()  # allows you to use Seaborn styles
sns.set_style("whitegrid")
sns.set_context("poster", font_scale = 1)
f, ax = plt.subplots(figsize=(20,10))

pivoted.plot(ax=ax)

plt.show()

Posted

in

by

Tags: