【问题标题】:How to save a plot in Seaborn with Python [duplicate]如何使用 Python 在 Seaborn 中保存绘图 [重复]
【发布时间】:2016-04-16 20:22:33
【问题描述】:

我有一个 Pandas 数据框并尝试将绘图保存在 png 文件中。但是,似乎有些东西不能正常工作。这是我的代码:

import pandas
import matplotlib.pyplot as plt
import seaborn as sns

sns.set(style='ticks')

df = pandas.read_csv("this_is_my_csv_file.csv")
plot = sns.distplot(df[['my_column_to_plot']])
plot.savefig("myfig.png")

我有这个错误:

AttributeError: 'AxesSubplot' object has no attribute 'savefig'

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    你可以像这样拯救任何海生人物。

    假设如果你想创建一个小提琴图来显示性别方面的工资分布。您可以这样做,并使用 get_figure 方法保存它。

    ax = sns.violinplot(x="Gender", y="Salary", hue="Degree", data=job_data)
    #Returns the :class:~matplotlib.figure.Figure instance the artist belongs to
    fig = ax.get_figure()
    fig.savefig('gender_salary.png')
    

    【讨论】:

      【解决方案2】:

      你可以使用plt.savefig,因为当你打电话给plt.show()时,你的图片就会出现

      【讨论】:

      • @Tasos 可能是他们用fig = plt.Figure() 制作了一个Figure 对象。然后你可以用fig.savefig()保存图
      【解决方案3】:

      使用plt.savefig('yourTitle.png')

      如果要传递变量:

      plt.savefig("yourTitleDataSet{0}.png".format(dataset))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多