简体   繁体   中英

changing size of seaborn plots and matplotlib library plots in a common way

from pylab import rcParams
rcParams['figure.figsize'] = (10, 10)

This works fine for histogram plots but not for factor plots. sns.factorplot (.....) still shows the default size.

sns.factorplot('Pclass','Survived',hue='person',data = titanic_df,size = 6,aspect =1)

I have to specify size,aspect everytime.

Please suggest something that works for both of them globally.

It's not possible to change the figure size of a factorplot via rcParams.

The figure size is hardcoded inside the FacetGrid class as

figsize = (ncol * size * aspect, nrow * size)

A new figure is then created using this figsize .

This makes it impossible to change the figure size by other means than the argument in the function call to factorplot . It makes it also impossible to first create a figure with other parameters and plot the factorplot to this figure. However, for a workaround in case of a factorplot with a single axes see @MartinEvans' answer .

The author of seaborn argues here that this is because a factorplot would need to have full control over the figure.

While one may question whether this needs to be the case, there is nothing you can do about it, other than (a) adding a feature request at the GitHub site and/or write your own wrapper - which wouldn't be too difficult, given that seaborn as well as matplotlib are open source.

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=(10, 10))

df = pd.DataFrame({
    'product' : ['A', 'A', 'A', 'B', 'B', 'C', 'C'], 
    'close' : [1, 1, 0, 1, 1, 1, 0],
    'counts' : [3, 3, 3, 2, 2, 2, 2]})

sns.factorplot(y='counts', x='product', hue='close', data=df, kind='bar', palette='muted', ax=ax)
plt.close(2)    # close empty figure
plt.show()

When using an Axis grids type plot, seaborn will automatically create another figure. A workaround is to close the empty second figure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM