theotheraussie
theotheraussie

Reputation: 495

python - plot twin y axis not working using dates

I'm using python 3.7

I would like to be able to plot the below data on 2 separate axes, I know the standard way to do this is to use plt.twinx(), but this doesn't seem to work. I think it has something to do with the dates as the x-axis, but I'm not sure how to resolve it.

When I plot the data on separate plots, they work fine, but when plotting the twin axis, each field gets squashed next to their respective axis (shown below).

My question is: How do i plot data on 2 y-axis correctly?

Here is sample code that produces the issue.

df_so = pd.DataFrame( {'date': ['2020/07/09', '2020/07/16', '2020/07/26', '2020/08/01', '2020/08/08', 
                            '2020/08/17', '2020/08/22', '2020/08/29', '2020/09/05', '2020/09/12', 
                            '2020/09/19', '2020/09/26', '2020/10/04', '2020/10/10', '2020/10/15', 
                            '2020/10/20', '2020/11/01', '2020/11/06', '2020/11/13', '2020/11/21', 
                            '2020/11/27', '2020/12/03', '2020/12/12', '2020/12/17', '2020/12/24', 
                            '2021/01/09', '2021/01/16', '2021/01/22', '2021/01/28', '2021/02/04', 
                            '2021/02/13', '2021/02/20', '2021/02/26', '2021/03/06', '2021/03/12', 
                            '2021/03/20', '2021/03/28', '2021/04/02', '2021/04/11', '2021/04/15'], 
                   'count': [23, 32, 17, 18, 27, 18, 32, 17, 18, 26, 38, 40, 19, 12, 20, 53, 11, 29, 
                             35, 20, 56, 15, 21, 11, 4, 6, 6, 8, 34, 8, 39, 31, 39, 8, 29, 38, 19, 
                             9, 14, 11], 
                   'CPI rate': [-0.3, -0.3, -0.3, -0.3, -0.3, -0.3, -0.3, -0.3, -0.3, -0.3, -0.3, 
                                -0.3, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 
                                0.7, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 
                                1.1, 1.1, 1.1], 
                   'inflation': [0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 
                                 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 
                                 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 
                                 0.1, 0.1, 0.1, 0.1]})

df_so['date'] = pd.to_datetime(df_so['date'])

fig, ax = plt.subplots(figsize=(16,10));
sns.barplot(data=df_so, x='date', y='count')
plt.xticks(rotation=90);

ax2 = plt.twinx()
sns.lineplot(data=df_so, x='date', y='CPI rate', ax=ax2)

axis_labels = df_so['date'].dt.strftime('%Y-%m-%d').sort_values().unique()
ax.set_xticklabels(labels=axis_labels, rotation=90, ha='right');
plt.show();

Here is the output i am getting: enter image description here

This is the expected output (without the 2nd CPI rate line) enter image description here

Upvotes: 0

Views: 115

Answers (1)

Redox
Redox

Reputation: 9987

As the second plot (lineplot) is set after the initial barplot is built, you need to slightly modify your code. Change the line -->

sns.lineplot(data=df_so, x='date', y='CPI rate', ax=ax2)

to...

sns.lineplot(data=df_so['CPI rate'], ax=ax2)

...and you will get the plots

enter image description here ..

Upvotes: 1

Related Questions