08-scatterplot
August 13, 2024
1 Seaborn: scatterplot
[1]: import seaborn as sns
from matplotlib import pyplot as plt
[2]: diamonds = sns.load_dataset('diamonds')
[Link]
[2]: (53940, 10)
[3]: [Link]()
[3]: carat cut color clarity depth table price x y z
0 0.23 Ideal E SI2 61.5 55.0 326 3.95 3.98 2.43
1 0.21 Premium E SI1 59.8 61.0 326 3.89 3.84 2.31
2 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31
3 0.29 Premium I VS2 62.4 58.0 334 4.20 4.23 2.63
4 0.31 Good J SI2 63.3 58.0 335 4.34 4.35 2.75
[4]: diamonds = diamonds[
[Link](['Premium', 'Good']) &
[Link](['D', 'F', 'J'])
].sample(n=100, random_state=22)
[Link]
[4]: (100, 10)
1.1 Intro Visuals
[ ]: import pandas as pd
import numpy as np
[ ]: sns.set_style('white')
[Link]('xtick', labelsize=14)
[Link]('ytick', labelsize=14)
1
[ ]: blue, orange, green, red = sns.color_palette()[:4]
[ ]: pts_x = [1] #, 2, 1.5, 4, 3, 2.5, 2.1, 1.5, 3.5, 1, 2.6, 3.4, 1.3, 0.5]
pts_y = [1] #, 3, 2.5, 3, 3.5, 2, 2.5, 1.8, 4, 1.3, 3.2, 3, 2, 1]
[Link](figsize=(5, 5))
[Link](pts_x, pts_y, s=150, color='xkcd:crimson', alpha=0.7,␣
↪edgecolor='black')
[Link]()
[Link](0, 4.2)
[Link](0, 4.2)
[Link]([0, 1, 2, 3, 4])
[Link]([1, 2, 3, 4])
plt.tight_layout()
[Link]('08scatter_buildBasic1.svg')
[ ]: pts_x = [1, 2] #, 1.5, 4, 3, 2.5, 2.1, 1.5, 3.5, 1, 2.6, 3.4, 1.3, 0.5]
pts_y = [1, 3] #, 2.5, 3, 3.5, 2, 2.5, 1.8, 4, 1.3, 3.2, 3, 2, 1]
[Link](figsize=(5, 5))
[Link](pts_x, pts_y, s=150, color='xkcd:crimson', alpha=0.7,␣
↪edgecolor='black')
[Link]()
[Link](0, 4.2)
[Link](0, 4.2)
[Link]([0, 1, 2, 3, 4])
[Link]([1, 2, 3, 4])
plt.tight_layout()
[Link]('08scatter_buildBasic2.svg')
[ ]: pts_x = [1, 2, 1.5] #, 4, 3, 2.5, 2.1, 1.5, 3.5, 1, 2.6, 3.4, 1.3, 0.5]
pts_y = [1, 3, 2.5] #, 3, 3.5, 2, 2.5, 1.8, 4, 1.3, 3.2, 3, 2, 1]
[Link](figsize=(5, 5))
[Link](pts_x, pts_y, s=150, color='xkcd:crimson', alpha=0.7,␣
↪edgecolor='black')
[Link]()
[Link](0, 4.2)
[Link](0, 4.2)
[Link]([0, 1, 2, 3, 4])
[Link]([1, 2, 3, 4])
plt.tight_layout()
[Link]('08scatter_buildBasic3.svg')
[ ]: pts_x = [1, 2, 1.5, 4, 3, 2.5] #, 2.1, 1.5, 3.5, 1, 2.6, 3.4, 1.3, 0.5]
pts_y = [1, 3, 2.5, 3, 3.5, 2] #, 2.5, 1.8, 4, 1.3, 3.2, 3, 2, 1]
2
[Link](figsize=(5, 5))
[Link](pts_x, pts_y, s=150, color='xkcd:crimson', alpha=0.7,␣
↪edgecolor='black')
[Link]()
[Link](0, 4.2)
[Link](0, 4.2)
[Link]([0, 1, 2, 3, 4])
[Link]([1, 2, 3, 4])
plt.tight_layout()
[Link]('08scatter_buildBasic4.svg')
[ ]: pts_x = [1, 2, 1.5, 4, 3, 2.5, 2.1, 1.5, 3.5] #, 1, 2.6, 3.4, 1.3, 0.5]
pts_y = [1, 3, 2.5, 3, 3.5, 2, 2.5, 1.8, 4] #, 1.3, 3.2, 3, 2, 1]
[Link](figsize=(5, 5))
[Link](pts_x, pts_y, s=150, color='xkcd:crimson', alpha=0.7,␣
↪edgecolor='black')
[Link]()
[Link](0, 4.2)
[Link](0, 4.2)
[Link]([0, 1, 2, 3, 4])
[Link]([1, 2, 3, 4])
plt.tight_layout()
[Link]('08scatter_buildBasic5.svg')
[ ]: pts_x = [1, 2, 1.5, 4, 3, 2.5, 2.1, 1.5, 3.5, 1, 2.6, 3.4, 1.3, 0.5]
pts_y = [1, 3, 2.5, 3, 3.5, 2, 2.5, 1.8, 4, 1.3, 3.2, 3, 2, 1]
[Link](figsize=(5, 5))
[Link](pts_x, pts_y, s=150, color='xkcd:crimson', alpha=0.7,␣
↪edgecolor='black')
[Link]()
[Link](0, 4.2)
[Link](0, 4.2)
[Link]([0, 1, 2, 3, 4])
[Link]([1, 2, 3, 4])
plt.tight_layout()
[Link]('08scatter_buildBasicFull.svg')
[ ]: for x,y in zip(pts_x, pts_y):
print((x, y))
[ ]: cat = [0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1]
[ ]: [Link](figsize=(5, 5))
[Link](pts_x, pts_y, s=200, hue=cat, alpha=0.7, edgecolor='black',␣
↪palette={0: 'xkcd:crimson', 1: '#3d9f98'})
3
[Link]()
[Link](0, 4.2)
[Link](0, 4.2)
[Link]([0, 1, 2, 3, 4])
[Link]([1, 2, 3, 4])
[Link]().get_legend().remove()
plt.tight_layout()
[Link]('08scatter_byHue.png')
[ ]: [Link](figsize=(5, 5))
[Link](pts_x, pts_y, s=200, style=cat, alpha=0.7, edgecolor='black',␣
↪color='xkcd:crimson')
[Link]()
[Link](0, 4.2)
[Link](0, 4.2)
[Link]([0, 1, 2, 3, 4])
[Link]([1, 2, 3, 4])
[Link]().get_legend().remove()
plt.tight_layout()
[Link]('08scatter_byStyle.png')
[ ]: [Link](figsize=(5, 5))
[Link](pts_x, pts_y, size=cat, alpha=0.7, edgecolor='black',␣
↪color='xkcd:crimson', sizes=[50, 300])
[Link]()
[Link](0, 4.2)
[Link](0, 4.2)
[Link]([0, 1, 2, 3, 4])
[Link]([1, 2, 3, 4])
[Link]().get_legend().remove()
plt.tight_layout()
[Link]('08scatter_bySize.png')
[ ]: [Link](figsize=(5, 5))
[Link](pts_x, pts_y, size=cat, hue=cat, style=cat,
alpha=0.7, edgecolor='black',
sizes=[100, 300],
palette={0: 'xkcd:crimson', 1: '#3d9f98'}
)
[Link]()
[Link](0, 4.2)
[Link](0, 4.2)
[Link]([0, 1, 2, 3, 4])
[Link]([1, 2, 3, 4])
[Link]().get_legend().remove()
plt.tight_layout()
[Link]('08scatter_byAll.png')
4
[ ]: [Link]('xtick', labelsize=10)
[Link]('ytick', labelsize=10)
1.2 Basics
[5]: sns.set_style('dark')
[6]: [Link]([Link], [Link]);
[7]: [Link](x='carat', y='price', data=diamonds);
5
[8]: [Link](x='x', y='y', data=diamonds);
6
1.3 Semantic Variables
[9]: sns.set_style('darkgrid')
1.3.1 hue
[10]: [Link](x='carat', y='price', hue='cut',
data=diamonds
);
[11]: [Link](x='carat', y='price', hue='cut',
data=diamonds,
palette=['purple', '#55CCCC']
);
7
[12]: [Link](x='carat', y='price', hue='cut',
data=diamonds,
palette = ['purple', '#55CCCC'],
hue_order=['Good', 'Premium']
);
8
Continuous Variable
[13]: [Link](x='carat', y='price', hue='depth',
data=diamonds
);
9
1.3.2 style
[14]: [Link](x='carat', y='price', style='cut',
data=diamonds,
s=100
);
1.3.3 size
[15]: [Link](x='carat', y='price', size='cut',
data=diamonds
);
10
[16]: [Link](x='carat', y='price', size='cut',
data=diamonds,
sizes=[150, 50]
);
11
[17]: [Link](x='carat', y='price', size='depth',
data=diamonds
);
1.3.4 hue + style + size
[18]: [Link](x='carat', y='price',
hue='cut',
style='cut',
data=diamonds,
s=100
);
12
[19]: [Link](x='carat', y='price',
hue='cut',
style='color',
data=diamonds,
s=100
);
13
[20]: [Link](x='carat', y='price',
hue='cut',
style='color',
size='depth',
data=diamonds);
14
1.4 Additional Styling
1.4.1 alpha
[21]: [Link](x='carat', y='price',
data=diamonds,
s=100,
alpha=0.6
);
15
1.4.2 marker styling
Check the matplotlib scatter and the marker style docs for more styling of the markers themselves
[22]: [Link](x='carat', y='price',
data=diamonds,
s=200,
marker='*'
);
16
[23]: [Link](x='carat', y='price',
data=diamonds,
s=200,
marker='*',
edgecolor='black'
);
17
[ ]:
18