Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop seaborn plotting multiple figures on top of one another

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') 
like image 392
Alex Avatar asked Mar 15 '16 17:03

Alex


People also ask

How do you plot multiple figures in Seaborn?

In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid() function and other by implicit with the help of matplotlib. data: Tidy dataframe where each column is a variable and each row is an observation.

How do I show two Seaborn plots side-by-side?

How to plot two Seaborn lmplots side-by-side (Matplotlib)? To create two graphs, we can use nrows=1, ncols=2 with figure size (7, 7). Create a data frame with keys, col1 and col2, using Pandas. Use countplot() to show the counts of observations in each categorical bin using bars.

What is Factorplot in Seaborn?

Factor Plot is used to draw a different types of categorical plot . The default plot that is shown is a point plot, but we can plot other seaborn categorical plots by using of kind parameter, like box plots, violin plots, bar plots, or strip plots.

How do you use subplots in Seaborn?

You can use the following basic syntax to create subplots in the seaborn data visualization library in Python: #define dimensions of subplots (rows, columns) fig, axes = plt. subplots(2, 2) #create chart in each subplot sns. boxplot(data=df, x='team', y='points', ax=axes[0,0]) sns.


2 Answers

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') 
like image 84
Leb Avatar answered Oct 01 '22 02:10

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 
like image 43
MF.OX Avatar answered Oct 01 '22 02:10

MF.OX