How to save a plot in Siborn with Python

I have a Pandas framework and try to save the chart in a png file. However, it seems that something is not working properly. This is my code:

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' 
+6
python matplotlib pandas seaborn
Jan 12 '16 at 9:34
source share
3 answers

You can use plt.savefig because your image will appear when you call plt.show()

+3
Jan 12 '16 at 9:38 on
source share

You can save any marine figure like this.

Suppose if you want to create a violin plot to show the distribution of salaries by gender. You can do it this way and save it using the get_figure method.

 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') 
+10
Feb 27 '16 at 13:43
source share

Use plt.savefig('yourTitle.png')

If you want to pass a variable:

 plt.savefig("yourTitleDataSet{0}.png".format(dataset)) 
0
Jan 14 '16 at 17:57
source share



All Articles