Stop seaborn plotting multiple figures on top of one another

Question:

I’m starting to learn a bit of python (been using R) for data analysis. I’m trying to create two plots using seaborn, but it keeps saving the second on top of the first. How do I stop this behavior?

import seaborn as sns
iris = sns.load_dataset('iris')

length_plot = sns.barplot(x='sepal_length', y='species', data=iris).get_figure()
length_plot.savefig('ex1.pdf')
width_plot = sns.barplot(x='sepal_width', y='species', data=iris).get_figure()
width_plot.savefig('ex2.pdf')
Asked By: Alex

||

Answers:

Create specific figures and plot onto them:

import seaborn as sns
iris = sns.load_dataset('iris')

length_fig, length_ax = plt.subplots()
sns.barplot(x='sepal_length', y='species', data=iris, ax=length_ax)
length_fig.savefig('ex1.pdf')

width_fig, width_ax = plt.subplots()
sns.barplot(x='sepal_width', y='species', data=iris, ax=width_ax)
width_fig.savefig('ex2.pdf')
Answered By: mwaskom

You have to start a new figure in order to do that. There are multiple ways to do that, assuming you have matplotlib. Also get rid of get_figure() and you can use plt.savefig() from there.

Method 1

Use plt.clf()

import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset('iris')

length_plot = sns.barplot(x='sepal_length', y='species', data=iris)
plt.savefig('ex1.pdf')
plt.clf()
width_plot = sns.barplot(x='sepal_width', y='species', data=iris)
plt.savefig('ex2.pdf')

Method 2

Call plt.figure() before each one

plt.figure()
length_plot = sns.barplot(x='sepal_length', y='species', data=iris)
plt.savefig('ex1.pdf')
plt.figure()
width_plot = sns.barplot(x='sepal_width', y='species', data=iris)
plt.savefig('ex2.pdf')
Answered By: Leb

I agree with a previous comment that importing matplotlib.pyplot is not the best software engineering practice as it exposes the underlying library. As I was creating and saving plots in a loop, then I needed to clear the figure and found out that this can now be easily done by importing seaborn only:

since version 0.11:

import seaborn as sns
import numpy as np

data = np.random.normal(size=100)
path = "/path/to/img/plot.png"

plot = sns.displot(data) # also works with histplot() etc
plot.fig.savefig(path)
plot.fig.clf() # this clears the figure

# ... continue with next figure

alternative example with a loop:

import seaborn as sns
import numpy as np

for i in range(3):
  data = np.random.normal(size=100)
  path = "/path/to/img/plot2_{0:01d}.png".format(i)

  plot = sns.displot(data)
  plot.fig.savefig(path)
  plot.fig.clf() # this clears the figure

before version 0.11 (original post):

import seaborn as sns
import numpy as np

data = np.random.normal(size=100)
path = "/path/to/img/plot.png"

plot = sns.distplot(data)
plot.get_figure().savefig(path)
plot.get_figure().clf() # this clears the figure

# ... continue with next figure
Answered By: MF.OX
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.