If you are viewing this file in preview mode, some links won't work. Find the fully featured Jupyter Notebook file on the website of Prof. Jens Flemming at Zwickau University of Applied Sciences. This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Seaborn is a Python library based on Matplotlib. It provides two useful features:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
The reason for importing as sns
is somewhat vague. sns
are the initials of Samuel Norman Seaborn, a fictional television character. See also issue #229 in Seaborns Github repository.
Seaborn styles are loaded by calling sns.set_style()
with one of the following string arguments: darkgrid
, whitegrid
, dark
, white
, ticks
. Subsequent calls to Matplotlib functions will create plots in Seaborn style.
sns.set_style('darkgrid')
fig, ax = plt.subplots()
x = [1, 2]
y = [1, 2]
ax.plot(x, y)
plt.show()
Seaborn also supports different scalings for different usecases. Scaling is set with sns.set_context
and one of the string arguments paper
, notebook
, talk
, poster
, where notebook
is the default. Different scalings allow for almost identical code to create figures for different channels of publication.
sns.set_context('talk')
fig, ax = plt.subplots()
x = [1, 2]
y = [1, 2]
ax.plot(x, y)
plt.show()
Seaborn comes with lots of functions which take a whole dataset (Pandas data frame) and create complex visualizations of the dataset. To get an overview have a look at the official Seaborn tutorials and at the Seaborn gallery.
sns.set_context('notebook')
rng = np.random.default_rng(0)
# parameters for point clouds
means = [[5, 0, 0], [-5, 2, 0], [0, -3, 5]] # mean vectors
covs = [[[1, 1, 0], [1, 1, 0], [0, 0, 1]],
[[10, 2, 0], [2, 10, 2], [0, 2, 10]],
[[0.1, 0, 0], [0, 3, 3], [0, 3, 7]]] # covariance matrices
names = ['cloud A', 'cloud B', 'cloud C'] # names
ns = [100, 1000, 100] # samples per cloud
# create data frame with named samples from each cloud
data = pd.DataFrame(columns=['x', 'y', 'z', 'name'])
for (mean, cov, name, n) in zip(means, covs, names, ns):
x, y, z = rng.multivariate_normal(mean, cov, n).T
cloud_data = pd.DataFrame(np.asarray([x, y, z]).T, columns=['x', 'y', 'z'])
cloud_data['name'] = name
data = data.append(cloud_data, ignore_index=True)
# show data frame structure
print(data.head())
# plot pairwise relations with Seaborn
sns.pairplot(data, hue='name', hue_order=['cloud B', 'cloud A', 'cloud C'])
plt.show()
x y z name 0 4.874270 -0.125730 -0.132105 cloud A 1 4.895100 -0.104900 -0.535669 cloud A 2 3.696000 -1.304000 0.947081 cloud A 3 6.265421 1.265421 -0.623274 cloud A 4 7.325031 2.325031 -0.218792 cloud A