停止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 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    为此,您必须开始一个新的图形。假设您有,有多种方法可以做到这一点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')
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看