Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 565
Jacques MALAPRADE Avatar asked Aug 01 '15 09:08

Jacques MALAPRADE


People also ask

How do you plot a subplot from a data frame?

You can manually create the subplots with matplotlib, and then plot the dataframes on a specific subplot using the ax keyword. For example for 4 subplots (2x2): import matplotlib. pyplot as plt fig, axes = plt.

How do I plot specific columns in pandas?

Pandas has a tight integration with Matplotlib. You can plot data directly from your DataFrame using the plot() method. To plot multiple data columns in single frame we simply have to pass the list of columns to the y argument of the plot function.


1 Answers

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

like image 156
Jianxun Li Avatar answered Oct 17 '22 03:10

Jianxun Li