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....
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.
Use sns. plt to save images.
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.
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.
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')
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With