Title: Violin Plot using Python, matplotlib and seaborn Date: 2017-10-21 16:00

Import the necessary packages

Pandas is used to read in the .csv data. Seaborn to build the plot and Matplotlib for displaying the plot.


In [1]:
# seaborn violin plot

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

In [2]:
%matplotlib inline

Read in the .csv data file

Use pandas pd.read_csv() function to read in the .csv file


In [3]:
df = pd.read_csv('data.csv')

Build the violin plot

Use seaborns built-in violin plot function to make the plot. The two fuction arguments are data=df and pallette="pastel".


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


Save the figure

Save the figure using matplotlib's ax.get_figure() method and fig.savefig() method. Since seaborn produces a matplotlib axis object, we can use matplotlib methods on our ax object.


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

In [7]:
#seaborn violin plot

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.read_csv('data.csv')

ax = sns.violinplot(data=df, palette="pastel")

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



In [ ]: