如何使用Python在Seaborn中保存绘图

如何使用Python在Seaborn中保存绘图,python,pandas,matplotlib,seaborn,Python,Pandas,Matplotlib,Seaborn,我有一个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']]) pl

我有一个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'

您可以使用
plt.savefig
,因为当您调用
plt.show()
使用
plt.savefig('yourttitle.png')

如果要传递变量,请执行以下操作:

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

你可以拯救任何像这样的海洋生物

假设您想要创建一个小提琴图来显示性别工资分布。您可以这样做,并使用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')

@Tasos可能是他们用
fig=plt.Figure()
制作了一个Figure对象。然后,您可以使用
fig.savefig()保存图形。