Jona
Jona

Reputation: 21

different ylabel for each subplot in pandas.DataFrame.plot()

I have a data frame df with two columns: df['A'] and df['B']. I am plotting my dataframe with df.plot(subplots=True). This produces a figure with two subplots, one for A and one for B. Is there a way to use the ylabel keyword to assign different ylabel values to the two subplots? I tried with df.plot(subplots=True,ylabel=['A','B']) and hoped that the first element of the list was assigned to the first subplot and the second element to the second subplot. However, the labels of both subplots are set to '[A,B]'.

Upvotes: 2

Views: 1055

Answers (1)

Bill
Bill

Reputation: 11603

You can use the axes handles returned by df.plot:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"A": [1, 2, 3], "B": [2, 4, 1]})

axes = df.plot(subplots=True)
axes[0].set_ylabel("label1")
axes[1].set_ylabel("label2")
plt.show()

Upvotes: 1

Related Questions