Title: Violin plot with python, matplotlib and seaborn Date: 2017-10-19 10:42 Modified: 2017-10-19 10:42 Slug: violin-plot-with-python-matplotlib-seaborn

Import the necessary packages


In [15]:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

In [16]:
%matplotlib inline

Read in the data

We will use pandas pd.read_csv() function to read in the data file. We'll bring in the .csv file and save it in a pandas dataframe called df.


In [17]:
df = pd.read_csv('LAB_3_large_data_set_cleaned.csv')

Make the violin plot

We will make the violin plot using seaborns sns.violinplot() function. Two arguments are added to the function call data=df which specifies the data included in the plot and palette="pastel" which specifies a nice looking color pallet.


In [18]:
ax = sns.violinplot(data=df, palette="pastel")
plt.show()


Save the figure

To save the violin plot, we will use matplotlibs ax.get_figure() method. This creates a figure object that we'll call fig. We then use matplotlibs fig.savefig() method to save the figure.


In [19]:
fig = ax.get_figure()
fig.savefig('sns_violin_plot.png', dpi=300)

The whole script


In [20]:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

df = pd.read_csv('LAB_3_large_data_set_cleaned.csv')
ax = sns.violinplot(data=df, palette="pastel")
plt.show()

fig = ax.get_figure()
fig.savefig('sns_violin_plot.png', dpi=300)