停止Seaborn在彼此之上绘制多个图形
发布于 2021-01-29 15:07:19
我开始学习一些用于数据分析的python(使用R)。我正在尝试使用创建两个图seaborn
,但它始终将第二个保存在第一个上。如何停止这种行为?
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')
关注者
0
被浏览
66
1 个回答
-
为此,您必须开始一个新的图形。假设您有,有多种方法可以做到这一点
matplotlib
。也摆脱它,get_figure()
您可以plt.savefig()
从那里使用。方法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')