seaborn barplot() output figure is overwritten

1.7k views Asked by At

I am using following lines of code to plot a couple of seaborn bar graphs in jupyter notebooks

sns.set(style="darkgrid")
rcParams['figure.figsize'] = (12, 8)
bar_plot = sns.barplot(x='Country',y='Average Rate',data=pddf1, palette="muted", x_order=pddf1["Country"].tolist())
abc = bar_plot.set_xticklabels(pddf1["Country"],rotation=90)

sns.set(style="darkgrid")
rcParams['figure.figsize'] = (12, 4)
bar_plot = sns.barplot(x='Country',y='% Jobs Completed',data=pddf2,     palette="muted", x_order=pddf2["Country"].tolist())
abc = bar_plot.set_xticklabels(pddf2["Country"],rotation=90)

where pddf variables are panda dataframes constructed from the lists.

If I comment out one set of statements, the other graph is plotted correctly. However, if both of them are run together, both the graphs are drawn at the same axes. In other words, the first one is overwritten by the second one. I am sure because I see the longer bars from the first graph shown in the final figure.

Any idea, how I can draw them one after the other? What am I doing wrong?

Since seaborn is developed on top of matplotlib, so I searched it as well. In matplotlib, you draw by changing figure numbers. Not sure if it can be achieved using rcParams in seaborn.

2

There are 2 answers

0
user3165768 On BEST ANSWER

Thanks @iayork for subplot(). I just want to point out a couple of things that might help others

  1. I have infact 3 figures to plot & will use them on separate rows or they become too small to view

  2. I have "country name" as x-labels. Some country names are long like "United Arab Emirates", hence to avoid overlap I use the rotation angle of 90. When I am plotting the graphs on separate rows using f, (ax1, ax2, ax3) = plt.subplots(3,1, figsize=(15,6)), I get overlap of x-labels with the figure below. But if I use a separate subplot() statement for each figure, there is no overlap.Hence final code looks like this

    f, (ax1) = plt.subplots(1,figsize=(15,6))
    f, (ax2) = plt.subplots(1,figsize=(15,6))
    f, (ax3) = plt.subplots(1,figsize=(15,6))
    
    sns.set(style="darkgrid")
    
    sns.barplot(x='Country',y='Average Rate',data=pddf1, palette="muted", x_order=pddf1["Country"].tolist(), ax=ax1)
    ax1.set_xticklabels(pddf1["Country"],rotation=90)
    
    sns.barplot(x='Country',y='Jobs Completed',data=pddf2, palette="muted", x_order=pddf2["Country"].tolist(), ax=ax2)
    ax2.set_xticklabels(pddf2["Country"],rotation=90)
    
    sns.barplot(x='Country',y='User Rating',data=pddf3, palette="muted", x_order=pddf3["Country"].tolist(), ax=ax3)
    ax3.set_xticklabels(pddf3["Country"],rotation=90)
    
0
iayork On

Have you tried subplots?

sns.set(style="darkgrid")   # Only need to call this once 
fig, (ax1,ax2) = plt.subplots(1,2, figsize=(12,8))  # plots on same row
sns.barplot(x='Country',y='Average Rate',data=pddf1, palette="muted", x_order=pddf1["Country"].tolist(), ax=ax1)
ax1.set_xticklabels(pddf1["Country"],rotation=90)

sns.barplot(x='Country',y='% Jobs Completed',data=pddf2,     palette="muted", x_order=pddf2["Country"].tolist(), ax=ax2)
abc = bar_plot.set_xticklabels(pddf2["Country"],rotation=90)

This yields two figures that are the same size; there are other options like gridspec that allow more customization of positions and sizes.