如何保存图片boxplot seaborn

How to save picture boxplot seaborn

我创建了如下箱线图

import seaborn as sns
sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
ax = sns.boxplot(x=tips["total_bill"])

& 尝试保存

sns.boxplot.savefig('ax.png')

ax.savefig('ax.png')

但是

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

令人惊讶,因为它对 lmplot 等是正确的....

lmplot 不是 return 一个 AxesSubplot 实例,boxplot 是。你可以得到 ax 所属的数字然后 savefig 它:

ax.get_figure().savefig('ax.png')

一个选项是先生成 matplotlib 图和坐标轴

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

然后使用 seaborn 完成所有需要的绘图,指定要使用的轴,例如

sns.boxplot('A', 'B', data=your_dataframe, ax=ax) 

最后按常规方式保存

plt.savefig('your_figure.png')

使用sns.plt保存图像。

sns.plt.clf()
sns.boxplot(x=tips["total_bill"])
sns.plt.savefig('ax.png')