Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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....

like image 626
Edward Avatar asked Mar 07 '16 09:03

Edward


People also ask

How do I save a boxplot as PNG in Python?

To save plot figure as JPG or PNG file, call savefig() function on matplotlib. pyplot object. Pass the file name along with extension, as string argument, to savefig() function.

How do you save a boxplot in SNS?

Use sns. plt to save images.

How do you save a plot in Python?

Saving a plot on your disk as an image file Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

What is hue in Seaborn boxplot?

hue : (optional) This parameter take column name for colour encoding. data : (optional) This parameter take DataFrame, array, or list of arrays, Dataset for plotting. If x and y are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.


2 Answers

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')
like image 141
ricoamor Avatar answered Oct 17 '22 00:10

ricoamor


lmplot does not return an AxesSubplot instance, boxplot does. You can get the figure ax belongs to and then savefig it:

ax.get_figure().savefig('ax.png')
like image 25
Stop harming Monica Avatar answered Oct 16 '22 23:10

Stop harming Monica