如何使用 Python 在 Seaborn 中保存绘图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34739950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save a plot in Seaborn with Python
提问by Tasos
I have a Pandas dataframe and try to save a plot in a png file. However, it seems that something doesn't work as it should. This is my code:
我有一个 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")
And I have this error:
我有这个错误:
AttributeError: 'AxesSubplot' object has no attribute 'savefig'
采纳答案by Anton Protopopov
You could use plt.savefig
because your picture will come up when you'll call plt.show()
你可以使用plt.savefig
因为当你打电话时你的照片会出现plt.show()
回答by galucero
Use plt.savefig('yourTitle.png')
用 plt.savefig('yourTitle.png')
If you want to pass a variable:
如果你想传递一个变量:
plt.savefig("yourTitleDataSet{0}.png".format(dataset))
回答by Aman Tandon
You could save any seaborn figure like this.
你可以像这样拯救任何海生人物。
Suppose If you want to create a violin plot to show the salary distribution gender wise. You could do it like this and will save it using the get_figure method.
假设如果你想创建一个小提琴图来显示性别明智的工资分配。您可以这样做,并使用 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')