# How to Change the Figure Size for a Seaborn Plot?

Rate this post

Seaborn is a comprehensive data visualization library used for the plotting of statistical graphs in Python. It provides fine-looking default styles and color schemes for making more attractive statistical plots. Seaborn is built on the top portion of the matplotlib library and is also integrated closely with data structures from pandas.

How to change the figure size for a Seaborn plot?

## Method 1: Changing the Size of Axes-Level Plots

In this method, figure size is altered by creating a Seaborn scatter plot with non-identical values for height and width. For example, a Seaborn plot with a width of 8 and a height of 4. Here is the code for this Seaborn plot.

```import pandas as pd
import seaborn as sns

#create data
df = pd.DataFrame({"var1": [25, 12, 15, 14, 19, 23, 25, 29],
"var2": [5, 7, 7, 9, 12, 9, 9, 4],
"var3": [11, 8, 10, 6, 6, 5, 9, 12]})

#define figure size
sns.set(rc={"figure.figsize":(8, 4)}) #width=8, height=4

#display scatterplot
sns.scatterplot(data=df, x="var1", y="var2")
```

## Method 2: Changing the size of Figure-Level Plots

Height and width of figure level plots like `sns.catplot`, `sns.jointplot`, `sns.implot` have to be specified within the chart itself. Here is the code to design a Seaborn Implot with a height and width of 5 and 1.5, respectively.

```import pandas as pd
import seaborn as sns

#create data
df = pd.DataFrame({"var1": [25, 12, 15, 14, 19, 23, 25, 29],
"var2": [5, 7, 7, 9, 12, 9, 9, 4],
"var3": [11, 8, 10, 6, 6, 5, 9, 12]})

#create lmplot
sns.lmplot(data=df, x="var1", y="var2",
height=5, aspect=1.5) #height=5, width=1.5 times larger than height
```

## Difference Between Figure-Level and Axes-Level Functions

Axes-level functions lie below the figure-level functions in the overall hierarchy. Such as `sns.displot()` is a figure level function, and it covers four axes-level functions `histplot`, `kdeplot`, `ecdfplot`, and `rugplot`. Every module in Seaborn has one figure-level function that can create any possible plot of the underlying axes level functions. Even though `displot()` can create four types of plots, there are still pros and cons to using more specific axes-level function or figure level function.

## Object-level Plots

Figure level function `sns.displot` is used to create histogram. The object type used during that was `seaborn.axisgrid.FacetGrid`. This is the whole outcome of displaying the Seaborn Facet Grid object that interferes with the Matplotlib API. A plot can also be created through the object directly. Plot created from an object directly also opens up more options for customization. Here is the code and object-level plot from that code.

```fig_obj = sns.FacetGrid(pen, col='species', margin_titles=True, height=6)
fig_obj.map(plt.scatter, "bill_depth_mm", "body_mass_g", s=30)
fig_obj.set(xlim=(10,23),ylim=(2000,6500))
fig_obj.set_axis_labels("Bill Depth (mm)", "Body Mass (g)", fontsize=14)```

The use of Matplotlib makes a lot easier to achieve such kind of plots. Further adjustments in the plot can be made through Matplotlib syntax. Also, more keyword arguments can be added to the `map()` call or use the `fig_obj.set()` function. Functions like `fig.et_axis_labels()` can also be used for this.

## Flexibility in Seaborn Object

Although Seaborn objects can be challenging to use, we often have to open two or more documentation pages to get all of the parameters we are looking for. However, this extra effort makes faceting data variables a lot easier.

Seaborn Facet Grid sample plot code

```fig = sns.FacetGrid(pen, col="island", hue="species", aspect=.75, height=3.5)
fig.map_dataframe(sns.scatterplot, x="bill_depth_mm", y="body_mass_g")
fig.set_axis_labels("Bill Depths (mm)", "Body Mass (g)")

Plot:

## Font size adjustment on a Seaborn Plot

```iris_df = sns.load_dataset('iris')
fig = plt.gcf()

# Changing Seaborn Plot size
fig.set_size_inches(12, 8)

# Setting the font scale
sns.set(font_scale=2)
sns.scatterplot(x='sepal_length', y='petal_length',  data=iris_df)
```

## How to Save Seaborn Plots in Different File Formats?

Saving Seaborn plots is quite an important thing. This is completed using the `savefig()` method from Pyplot, which allows us to save our Seaborn in many different file formats such as PDF, eps, png, jpeg, etc. EPS is quite a handy file format, files saved in EPS have high resolution and are ideal to use while submitting our studies or research papers to scientific journals for publication.

### How to save the Seaborn plot in JPEG?

To handle this, we use the Pyplot `savefig()` method.  As we want our Seaborn plot in JPEG format, we have to provide the string `"jpeg"` in the argument. Then we have to add 70 dpi (dots per inch) for the resolution factor. The DPI can be altered to produce print-ready images

Here is how the code will shape up:

```import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="ticks")
g = sns.FacetGrid(iris_df, col="species")
g = g.map(plt.scatter, "petal_length", "petal_width")
g.fig.set_figheight(6)
g.fig.set_figwidth(10)
plt.savefig('our_plot_name.jpg', format='jpeg', dpi=70)
```

### How to save the Seaborn plot in EPS format?

It’s very much similar to the JPEG format. We need to change the plot name extension, format, and dpi value in the last line of the code.

Here is how code for (EPS) Encapsulated Postscript format will look:

```import matplotlib.pyplot as plt
import seaborn as sns

sns.set(style="ticks")
g = sns.FacetGrid(iris_df, col="species")
g = g.map(plt.scatter, "petal_length", "petal_width")

plt.savefig('our_plot_name.eps', format='eps', dpi=300)
```

### How to save Seaborn plot in PNG format?

We need to create a simple histogram using `sns.distplot` for this. Here are the required changes in the code to save the Seaborn plot in (Portable Network Graphics) png. We need to use `plt.savefig` functionality to achieve this.

Code and description:

```sns.distplot(df['mpg'])
# Saving the Seaborn Figure:
plt.savefig('save_as_a_png.png')```

We used `plt.savefig` functionality in the second line of the code. We want to save our Seaborn plot in PNG format so we used the file extension (`.png`). The png extension guides `plt.savefig` that this file should be saved as PNG.

### How to save the Seaborn plot as a high-resolution PNG?

Seaborn plot as high-resolution PNG needs to add dpi value 300. The file extension also needs to be `saving-a-high-resolution-seaborn-plot.png`.

Code:

```sns.distplot(df['mpg'])
plt.savefig('saving-a-high-resolution-seaborn-plot.png', dpi=300)```

### How to save a Seaborn plot as a Transparent PNG?

Saving the Seaborn plot as a transparent PNG requires a change in the file extension. The file extension for this will be `saving-a-seaborn-plot-as-png-file-transparent.png`.

Code:

```# Seaborn Save Figure:
plt.savefig('saving-a-seaborn-plot-as-png-file-transparent.png', transparent=True)```

## Uses and Applications of Seaborn

### Informational distributional summaries

The `display()` Seaborn functionality supports several approaches for visualizing distributions. These approaches include some classic techniques such as histograms and computationally-intensive approaches like kernel density estimation. Seaborn supports robust but less used processes like calculation and plotting of empirical cumulative distribution function of the data.

### Availability of specialized plots for categorical data plotting

Seaborn also have custom build plot types that have appropriate orientation to visualize categorical data. These specialized plots different levels to represent many parameters in granular form. We can also draw a “swarm” plot. A Swarm plot is a scatter plot that adjusts the positions of the points along the categorical axis so that the points don’t overlap at any position.

Here are the images of two different types of specialized plots:

### Composite views for multivariate datasets

Seaborn has some functions that combine multiple types of plots to provide quick informative summaries of any dataset. The `jointplot()` function focuses on a single relationship and plots the joint distribution between two variables and each variable’s marginal distribution.

Here is the code and image of such plot for multivariate datasets:

```penguins = sns.load_dataset("penguins")
sns.jointplot(data=penguins, x="flipper_length_mm", y="bill_length_mm", hue="species")```

### Functions and Classes for making complex graphics

These Seaborn tools become functional by combining axes-level plotting functions with objects that control the layout of the figure by linking the structure of a dataset to the grid of the axis. Both these elements are a part of public API and can be used directly to create a complex figure with only a few lines of code.

Code:

```g = sns.PairGrid(penguins, hue="species", corner=True)
g.map_lower(sns.kdeplot, hue=None, levels=5, color=".2")
g.map_lower(sns.scatterplot, marker="+")
g.map_diag(sns.histplot, element="step", linewidth=0, kde=True)
g.legend.set_bbox_to_anchor((.61, .6))```

Plot images:

### Opinionated defaults and flexible customization

Seaborn is enabled to design complete graphics with a single function call. Where it’s possible Seaborn function automatically adds informative axis labels and legends that explain the semantic mapping in the plot. Occasionally Seaborn will also pick default values for its parameters based on characteristics of the data set.

Code and image of plot with same functionality and tools:

```sns.relplot(
data=penguins,
x="bill_length_mm", y="bill_depth_mm", hue="body_mass_g"
)```

Before publishing work, we probably look to polish the figure beyond what the defaults achieve. Seaborn defines multiple built-in themes that apply to all figures and their standardized functions parameters, which can modify the semantic mappings for each plot. Once the plot is created, its properties can be modified through Seaborn API and dropping down to the matplotlib layer for fine-grained tweaking.