停止Seaborn在彼此之上绘制多个图形 [英] Stop seaborn plotting multiple figures on top of one another
本文介绍了停止Seaborn在彼此之上绘制多个图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开始学习一些用于数据分析的python(使用R).我正在尝试使用seaborn
创建两个图,但它始终将第二个图保存在第一个图上.如何停止这种行为?
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')
推荐答案
您必须先创建一个新图形.假设您拥有 matplotlib
,可以采用多种方法.也摆脱get_figure()
,您可以从那里使用plt.savefig()
.
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.
方法1
使用 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')
方法2
在每个呼叫之前呼叫plt.figure()
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')
这篇关于停止Seaborn在彼此之上绘制多个图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文