Tuesday, June 27, 2017

Data Visualisation: Seaborn



In [153]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline
In [154]:
# see the pre-defined styles provided.
plt.style.available
Out[154]:
['seaborn-dark',
 'seaborn-ticks',
 'seaborn',
 'bmh',
 'seaborn-pastel',
 'seaborn-whitegrid',
 'seaborn-colorblind',
 'seaborn-muted',
 'seaborn-notebook',
 'dark_background',
 'seaborn-bright',
 'grayscale',
 'seaborn-deep',
 'fivethirtyeight',
 'classic',
 'seaborn-dark-palette',
 'seaborn-darkgrid',
 'ggplot',
 'seaborn-white',
 'seaborn-poster',
 'seaborn-talk',
 'seaborn-paper']
In [155]:
# use the 'seaborn-colorblind' style
plt.style.use('fivethirtyeight')
In [156]:
plt.plot(4,5,"o")
Out[156]:
[<matplotlib.lines.Line2D at 0x121224940>]
In [157]:
np.random.seed(123)

df = pd.DataFrame({'A': np.random.randn(365).cumsum(0), 
                   'B': np.random.randn(365).cumsum(0) + 20,
                   'C': np.random.randn(365).cumsum(0) - 20}, 
                  index=pd.date_range('1/1/2017', periods=365))
df.head()
Out[157]:
A B C
2017-01-01 -1.085631 20.059291 -20.230904
2017-01-02 -0.088285 21.803332 -16.659325
2017-01-03 0.194693 20.835588 -17.055481
2017-01-04 -1.311601 21.255156 -17.093802
2017-01-05 -1.890202 21.462083 -19.518638
In [158]:
df.plot(); # add a semi-colon to the end of the plotting call to suppress unwanted output
In [159]:
df.plot('A','B', kind = 'scatter');
You can also choose the plot kind by using the DataFrame.plot.kind methods instead of providing the kind keyword argument.
kind :
  • 'line' : line plot (default)
  • 'bar' : vertical bar plot
  • 'barh' : horizontal bar plot
  • 'hist' : histogram
  • 'box' : boxplot
  • 'kde' : Kernel Density Estimation plot
  • 'density' : same as 'kde'
  • 'area' : area plot
  • 'pie' : pie plot
  • 'scatter' : scatter plot
  • 'hexbin' : hexbin plot
In [160]:
# create a scatter plot of columns 'A' and 'C', with changing color (c) and size (s) based on column 'B'
df.plot.scatter('A', 'C', c='B', s=df['B'], colormap='viridis')
Out[160]:
<matplotlib.axes._subplots.AxesSubplot at 0x1213407f0>
In [161]:
ax = df.plot.scatter('A', 'C', c='B', s=df['B'], colormap='viridis')
ax.set_aspect('equal')
In [162]:
df.plot.box();
In [163]:
df.plot.hist(alpha=0.7);
In [164]:
df.plot.kde();
In [165]:
iris = pd.read_csv('iris.csv')
iris.head()
Out[165]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
0 1 5.1 3.5 1.4 0.2 Iris-setosa
1 2 4.9 3.0 1.4 0.2 Iris-setosa
2 3 4.7 3.2 1.3 0.2 Iris-setosa
3 4 4.6 3.1 1.5 0.2 Iris-setosa
4 5 5.0 3.6 1.4 0.2 Iris-setosa
In [166]:
iris = iris.drop("Id", axis = 1)
In [167]:
plt.figure(figsize=(10,8))
pd.tools.plotting.parallel_coordinates(iris, 'Species');

Seaborn

In [168]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
In [169]:
np.random.seed(1234)

v1 = pd.Series(np.random.normal(0,10,1000), name='v1')
v2 = pd.Series(2*v1 + np.random.normal(60,15,1000), name='v2')
In [170]:
plt.figure()
plt.hist(v1, alpha=0.7, bins=np.arange(-50,150,5), label='v1');
plt.hist(v2, alpha=0.7, bins=np.arange(-50,150,5), label='v2');
plt.legend();
In [171]:
# plot a kernel density estimation over a stacked barchart
plt.figure()
plt.hist([v1, v2], histtype='barstacked', normed=True);
v3 = np.concatenate((v1,v2))
sns.kdeplot(v3);
In [172]:
plt.figure()
# we can pass keyword arguments for each individual component of the plot
sns.distplot(v3, hist_kws={'color': 'Teal'}, kde_kws={'color': 'Navy'});
In [173]:
sns.jointplot(v1, v2, alpha=0.4);
In [174]:
grid = sns.jointplot(v1, v2, alpha=0.4);
grid.ax_joint.set_aspect('equal')
In [175]:
sns.jointplot(v1, v2, kind='hex');
In [176]:
# set the seaborn style for all the following plots
sns.set_style('white')

sns.jointplot(v1, v2, kind='kde', space=0);
In [177]:
sns.pairplot(iris, hue='Species', diag_kind='kde', size=2);
In [178]:
plt.figure(figsize=(8,6))
plt.subplot(121)
sns.swarmplot('Species', 'PetalLengthCm', data=iris);
plt.subplot(122)
sns.violinplot('Species', 'PetalLengthCm', data=iris);
In [ ]:
 

No comments :

Post a Comment