如何用Python保存Seaborn的情节

Tas*_*sos 10 python matplotlib pandas 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']])
plot.savefig("myfig.png")
Run Code Online (Sandbox Code Playgroud)

我有这个错误:

AttributeError: 'AxesSubplot' object has no attribute 'savefig'
Run Code Online (Sandbox Code Playgroud)

Ama*_*don 16

你可以保存这样的任何海鸥形象.

假设您想创建一个小提琴图,以显示性别明智的薪资分布.你可以这样做,并使用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')
Run Code Online (Sandbox Code Playgroud)


Ant*_*pov 7

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