Edward Edward - 3 months ago 15
Python Question

How to save picture boxplot seaborn

I create a boxplot as bellow

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


& try to save

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


or

ax.savefig('ax.png')


but

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


It's surprisely, beacause it's correct for lmplot etc....

Answer

One option is to first generate the matplotlib figure and axes

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

Then do all the plotting you need with seaborn, specifying the axes to use, e.g.

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

And finally save in the usual way

plt.savefig('your_figure.png')