RichAd
RichAd

Reputation: 29

Plot secondary axis on multiple subplots in python

I am trying to plot on the secondary axis of all subplots of a bar chart, but I was only successful showing the secondary plot on one of the subplots (see image below). I tried:

df[['loan_amnt','int_rate']].plot(kind='bar',subplots=True,layout=(1,2), figsize=(15,5))
df['dti'].plot(secondary_y=True, marker='d', style='g:');

and got see below:

enter image description here What can I add to this code to ensure that the secondary plot is displayed on both subplots.

Upvotes: 0

Views: 5231

Answers (1)

RichAd
RichAd

Reputation: 29

I was able to solve this using the code below:

fig = plt.figure(figsize=(15,5))

cx0 = fig.add_subplot(121)
cx1 = cx0.twinx()
cx2 = plt.subplot(122)
cx3 = cx2.twinx()

rate_amnt_byGrade['loan_amnt'].plot(kind='bar', ax=cx0)
rate_amnt_byGrade['dti'].plot(ax=cx1, secondary_y=True)
rate_amnt_byGrade['int_rate'].plot(kind='bar', ax=cx2)
rate_amnt_byGrade['dti'].plot(ax=cx3, secondary_y=True)

Upvotes: 1

Related Questions