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

后端 未结 2 486
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-24 04:13
from pylab import rcParams
rcParams[\'figure.figsize\'] = (10, 10)

This works fine for histogram plots but not for factor plots. sns.factorplot (.....

相关标签:
2条回答
  • 2021-01-24 04:16

    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.

    0 讨论(0)
  • 2021-01-24 04:27

    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.

    0 讨论(0)
提交回复
热议问题