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

烂漫一生 提交于 2019-12-20 04:19:40

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/44035425/changing-size-of-seaborn-plots-and-matplotlib-library-plots-in-a-common-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!