1

Unable to change fig size, see example:

df = pd.DataFrame({'subset_product':['A','A','A','B','B','C','C'],
                   'subset_close':[1,1,0,1,1,1,0]})

prod_counts = df.groupby('subset_product').size().rename('prod_counts')
df['prod_count'] = df['subset_product'].map(prod_counts)
g = sns.factorplot(y='prod_count',x='subset_product',hue='subset_close',data=df,kind='bar',palette='muted',legend=False,ci=None)
plt.rcParams["figure.figsize"] = [20,10]
plt.legend(loc='best')

Despite changing the figsize, it always gives me the same plot size and an empty plot

enter image description here

jxn
  • 7,685
  • 28
  • 90
  • 172

1 Answers1

5

The figure size can be modified by first creating a figure and axis and passing this as a parameter to the seaborn plot:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns       


fig, ax = plt.subplots(figsize=(20, 10))

df = pd.DataFrame({'subset_product':['A','A','A','B','B','C','C'],
                   'subset_close':[1,1,0,1,1,1,0]})

prod_counts = df.groupby('subset_product').size().rename('prod_counts')
df['prod_count'] = df['subset_product'].map(prod_counts)
g = sns.factorplot(y='prod_count', x='subset_product', hue='subset_close', data=df, kind='bar', palette='muted', legend=False, ci=None, ax=ax)
plt.close(2)    # close empty figure
plt.show()

When using an Axis grids type plot, seaborn will automatically create a figure. A workaround could be to close the second figure.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • This seems to be a hacky but valid solution, since apparently the `ax` argument is somehow transfered to the underlying axes. I guess it will fail once the `factorplot`'s `col` or `row` argument are used?! In any case, may I suggest that you add this solution to the duplicate question, such that people can find all possible solutions in one place? – ImportanceOfBeingErnest Mar 05 '18 at 21:02