08 Scatterplot
08 Scatterplot
1 Seaborn: scatterplot
[1]: import seaborn as sns
from matplotlib import pyplot as plt
diamonds.shape
[3]: diamonds.head()
diamonds.shape
[ ]: sns.set_style('white')
plt.rc('xtick', labelsize=14)
plt.rc('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]
plt.figure(figsize=(5, 5))
sns.scatterplot(pts_x, pts_y, s=150, color='xkcd:crimson', alpha=0.7,␣
↪edgecolor='black')
sns.despine()
plt.xlim(0, 4.2)
plt.ylim(0, 4.2)
plt.yticks([0, 1, 2, 3, 4])
plt.xticks([1, 2, 3, 4])
plt.tight_layout()
plt.savefig('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]
plt.figure(figsize=(5, 5))
sns.scatterplot(pts_x, pts_y, s=150, color='xkcd:crimson', alpha=0.7,␣
↪edgecolor='black')
sns.despine()
plt.xlim(0, 4.2)
plt.ylim(0, 4.2)
plt.yticks([0, 1, 2, 3, 4])
plt.xticks([1, 2, 3, 4])
plt.tight_layout()
plt.savefig('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]
plt.figure(figsize=(5, 5))
sns.scatterplot(pts_x, pts_y, s=150, color='xkcd:crimson', alpha=0.7,␣
↪edgecolor='black')
sns.despine()
plt.xlim(0, 4.2)
plt.ylim(0, 4.2)
plt.yticks([0, 1, 2, 3, 4])
plt.xticks([1, 2, 3, 4])
plt.tight_layout()
plt.savefig('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
plt.figure(figsize=(5, 5))
sns.scatterplot(pts_x, pts_y, s=150, color='xkcd:crimson', alpha=0.7,␣
↪edgecolor='black')
sns.despine()
plt.xlim(0, 4.2)
plt.ylim(0, 4.2)
plt.yticks([0, 1, 2, 3, 4])
plt.xticks([1, 2, 3, 4])
plt.tight_layout()
plt.savefig('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]
plt.figure(figsize=(5, 5))
sns.scatterplot(pts_x, pts_y, s=150, color='xkcd:crimson', alpha=0.7,␣
↪edgecolor='black')
sns.despine()
plt.xlim(0, 4.2)
plt.ylim(0, 4.2)
plt.yticks([0, 1, 2, 3, 4])
plt.xticks([1, 2, 3, 4])
plt.tight_layout()
plt.savefig('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]
plt.figure(figsize=(5, 5))
sns.scatterplot(pts_x, pts_y, s=150, color='xkcd:crimson', alpha=0.7,␣
↪edgecolor='black')
sns.despine()
plt.xlim(0, 4.2)
plt.ylim(0, 4.2)
plt.yticks([0, 1, 2, 3, 4])
plt.xticks([1, 2, 3, 4])
plt.tight_layout()
plt.savefig('08scatter_buildBasicFull.svg')
[ ]: cat = [0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1]
[ ]: plt.figure(figsize=(5, 5))
sns.scatterplot(pts_x, pts_y, s=200, hue=cat, alpha=0.7, edgecolor='black',␣
↪palette={0: 'xkcd:crimson', 1: '#3d9f98'})
3
sns.despine()
plt.xlim(0, 4.2)
plt.ylim(0, 4.2)
plt.yticks([0, 1, 2, 3, 4])
plt.xticks([1, 2, 3, 4])
plt.gca().get_legend().remove()
plt.tight_layout()
plt.savefig('08scatter_byHue.png')
[ ]: plt.figure(figsize=(5, 5))
sns.scatterplot(pts_x, pts_y, s=200, style=cat, alpha=0.7, edgecolor='black',␣
↪color='xkcd:crimson')
sns.despine()
plt.xlim(0, 4.2)
plt.ylim(0, 4.2)
plt.yticks([0, 1, 2, 3, 4])
plt.xticks([1, 2, 3, 4])
plt.gca().get_legend().remove()
plt.tight_layout()
plt.savefig('08scatter_byStyle.png')
[ ]: plt.figure(figsize=(5, 5))
sns.scatterplot(pts_x, pts_y, size=cat, alpha=0.7, edgecolor='black',␣
↪color='xkcd:crimson', sizes=[50, 300])
sns.despine()
plt.xlim(0, 4.2)
plt.ylim(0, 4.2)
plt.yticks([0, 1, 2, 3, 4])
plt.xticks([1, 2, 3, 4])
plt.gca().get_legend().remove()
plt.tight_layout()
plt.savefig('08scatter_bySize.png')
[ ]: plt.figure(figsize=(5, 5))
sns.scatterplot(pts_x, pts_y, size=cat, hue=cat, style=cat,
alpha=0.7, edgecolor='black',
sizes=[100, 300],
palette={0: 'xkcd:crimson', 1: '#3d9f98'}
)
sns.despine()
plt.xlim(0, 4.2)
plt.ylim(0, 4.2)
plt.yticks([0, 1, 2, 3, 4])
plt.xticks([1, 2, 3, 4])
plt.gca().get_legend().remove()
plt.tight_layout()
plt.savefig('08scatter_byAll.png')
4
[ ]: plt.rc('xtick', labelsize=10)
plt.rc('ytick', labelsize=10)
1.2 Basics
[5]: sns.set_style('dark')
5
[8]: sns.scatterplot(x='x', y='y', data=diamonds);
6
1.3 Semantic Variables
[9]: sns.set_style('darkgrid')
1.3.1 hue
[10]: sns.scatterplot(x='carat', y='price', hue='cut',
data=diamonds
);
7
[12]: sns.scatterplot(x='carat', y='price', hue='cut',
data=diamonds,
palette = ['purple', '#55CCCC'],
hue_order=['Good', 'Premium']
);
8
Continuous Variable
[13]: sns.scatterplot(x='carat', y='price', hue='depth',
data=diamonds
);
9
1.3.2 style
1.3.3 size
[15]: sns.scatterplot(x='carat', y='price', size='cut',
data=diamonds
);
10
[16]: sns.scatterplot(x='carat', y='price', size='cut',
data=diamonds,
sizes=[150, 50]
);
11
[17]: sns.scatterplot(x='carat', y='price', size='depth',
data=diamonds
);
12
[19]: sns.scatterplot(x='carat', y='price',
hue='cut',
style='color',
data=diamonds,
s=100
);
13
[20]: sns.scatterplot(x='carat', y='price',
hue='cut',
style='color',
size='depth',
data=diamonds);
14
1.4 Additional Styling
1.4.1 alpha
15
1.4.2 marker styling
Check the matplotlib scatter and the marker style docs for more styling of the markers themselves
[22]: sns.scatterplot(x='carat', y='price',
data=diamonds,
s=200,
marker='*'
);
16
[23]: sns.scatterplot(x='carat', y='price',
data=diamonds,
s=200,
marker='*',
edgecolor='black'
);
17
[ ]:
18