How to plot a ylabel per subplot using pandas DataFrame plot function

Jacques MALAPRADE picture Jacques MALAPRADE · Aug 1, 2015 · Viewed 7.2k times · Source

By default pandas.DataFrame.plot() using the subplots option doesn't seem to make it easy to plot a ylabel per subplot. I am trying to plot a pandas dataframe having a subplot per column in the dataframe. Code so far that doesn't work:

fig = plt.figure(figsize=(10,10))
ax = plt.gca()
df.plot(y=vars, ax=ax, subplots=True, layout=(3,1), sharex=True, legend=False,)
ax.set_ylabel = ['y','x', 'z']

But this doesn't plot any labels at all.

Answer

Jianxun Li picture Jianxun Li · Aug 1, 2015

You can set y label on each ax separately.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# data
df = pd.DataFrame(np.random.randn(100,3), columns=list('ABC'))

# plot
axes = df.plot(figsize=(10, 10), subplots=True, sharex=True, legend=False)
axes[0].set_ylabel('yA')
axes[1].set_ylabel('yB')
axes[2].set_ylabel('yC')

enter image description here