How to change figuresize using seaborn factorplot
Asked Answered
E

7

64
%pylab inline

import pandas as pd
import numpy as np
import matplotlib as mpl
import seaborn as sns

typessns = pd.DataFrame.from_csv('C:/data/testesns.csv', index_col=False, sep=';')

mpl.rc("figure", figsize=(45, 10))
sns.factorplot("MONTH", "VALUE", hue="REGION", data=typessns, kind="box", palette="OrRd");

enter image description here

I always get a small size figure, no matter what size I 've specified in figsize... How to fix it?

Eoin answered 2/10, 2014 at 15:0 Comment(0)
P
118

Note added in 2019: In modern seaborn versions the size argument has been renamed to height.

To be a little more concrete:

%matplotlib inline

import seaborn as sns

exercise = sns.load_dataset("exercise")

# Defaults are size=5, aspect=1
sns.factorplot("kind", "pulse", "diet", exercise, kind="point", size=2, aspect=1)
sns.factorplot("kind", "pulse", "diet", exercise, kind="point", size=4, aspect=1)
sns.factorplot("kind", "pulse", "diet", exercise, kind="point", size=4, aspect=2)

You want to pass in the arguments 'size' or 'aspect' to the sns.factorplot() when constructing your plot.

Size will change the height, while maintaining the aspect ratio (so it will also also get wider if only size is changed.)

Aspect will change the width while keeping the height constant.

The above code should be able to be run locally in an ipython notebook.

Plot sizes are reduced in these examples to show the effects, and because the plots from the above code were fairly large when saved as png's. This also shows that size/aspect includes the legend in the margin.

size=2, aspect=1

size=2, aspect=1

size=4, aspect=1

size=4, aspect=1

size=4, aspect=2

size=4, aspect=2

Also, all other useful parameters/arguments and defaults for this plotting function can be viewed with once the 'sns' module is loaded:

help(sns.factorplot)
Pock answered 27/2, 2015 at 12:24 Comment(1)
this is the only solution that works for me. Tried all the others, even when there are no errors, the figure still displays as default size and aspect (small and square).Roundfaced
C
24

mpl.rc is stored in a global dictionary (see http://matplotlib.org/users/customizing.html). So, if you only want to change the size of one figure (locally), it will do the trick:

plt.figure(figsize=(45,10))
sns.factorplot(...)

It worked for me using matplotlib-1.4.3 and seaborn-0.5.1

Cayuse answered 10/4, 2015 at 7:38 Comment(3)
Did work for me until I respected same order than you, plt.figure must indeed go first. ThanksUpcoming
In which case, you also need import matplotlib.pyplot as pltIndian
@Indian I also found it better to stick with 'height' param instead of importing pyplot for most cases!Rhatany
C
5

The size of the figure is controlled by the size and aspect arguments to factorplot. They correspond to the size of each facet ("size" really means "height" and then size * aspect gives the width), so if you are aiming for a particularl size for the whole figure you'll need to work backwards from there.

Costello answered 3/10, 2014 at 15:12 Comment(1)
My first day of using matplotlib and Seaborn, so apologies for the stupid question, but what is mpl.rc("figure", figsize=(45, 10)) good for, then, if not controlling the size of the figure? I also see the same mpl.rc code used here: github.com/mwaskom/seaborn/issues/112 — and it doesn't change the size in my plots.Lyceum
I
5
import seaborn as sns

sns.set(rc={'figure.figsize':(12.7,8.6)})

plt.figure(figsize=(45,10))

Output

Inherited answered 28/4, 2019 at 12:50 Comment(0)
H
3
  1. Do not use %pylab inline, it is deprecated, use %matplotlib inline
  2. The question is not specific to IPython.
  3. use seaborn .set_style function, pass it your rc as second parameter or kwarg.: http://web.stanford.edu/~mwaskom/software/seaborn/generated/seaborn.set_style.html
Hasid answered 2/10, 2014 at 15:42 Comment(0)
M
3

If you just want to scale the figure use the below code:

import matplotlib.pyplot as plt
plt.figure(figsize=(8, 6))
sns.factorplot("MONTH", "VALUE", hue="REGION", data=typessns, kind="box", palette="OrRd"); // OR any plot code
Mosra answered 27/2, 2016 at 13:18 Comment(0)
J
3

Note as of July 2018:

seaborn.__version__ == 0.9.0

Two main changes which affect the above answers

  1. The factorplot function has been renamed to catplot()

  2. The size parameter has been renamed to height for multi plot grid functions and those that use them.

https://seaborn.pydata.org/whatsnew.html

Meaning the answer provided by @Fernando Hernandez should be adjusted as per below:

%matplotlib inline

import seaborn as sns

exercise = sns.load_dataset("exercise")

# Defaults are hieght=5, aspect=1
sns.catplot("kind", "pulse", "diet", exercise, kind="point", height=4, aspect=2)

Judicatory answered 1/5, 2019 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.