0% found this document useful (0 votes)
6 views18 pages

08 Scatterplot

The document provides a tutorial on creating scatterplots using the Seaborn library in Python, specifically focusing on visualizing the 'diamonds' dataset. It includes various examples of scatterplots with different parameters such as hue, style, and size, demonstrating how to customize the plots. Additionally, it covers basic styling options and marker customization for enhanced visual appeal.

Uploaded by

kart238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views18 pages

08 Scatterplot

The document provides a tutorial on creating scatterplots using the Seaborn library in Python, specifically focusing on visualizing the 'diamonds' dataset. It includes various examples of scatterplots with different parameters such as hue, style, and size, demonstrating how to customize the plots. Additionally, it covers basic styling options and marker customization for enhanced visual appeal.

Uploaded by

kart238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

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')

diamonds.shape

[2]: (53940, 10)

[3]: diamonds.head()

[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[


diamonds.cut.isin(['Premium', 'Good']) &
diamonds.color.isin(['D', 'F', 'J'])
].sample(n=100, random_state=22)

diamonds.shape

[4]: (100, 10)

1.1 Intro Visuals


[ ]: import pandas as pd
import numpy as np

[ ]: 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')

[ ]: 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]

[ ]: 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')

[6]: sns.scatterplot(diamonds.carat, diamonds.price);

[7]: sns.scatterplot(x='carat', y='price', data=diamonds);

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
);

[11]: sns.scatterplot(x='carat', y='price', hue='cut',


data=diamonds,
palette=['purple', '#55CCCC']
);

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

[14]: sns.scatterplot(x='carat', y='price', style='cut',


data=diamonds,
s=100
);

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
);

1.3.4 hue + style + size

[18]: sns.scatterplot(x='carat', y='price',


hue='cut',
style='cut',
data=diamonds,
s=100
);

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

[21]: sns.scatterplot(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]: 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

You might also like