Comparing seaborn and matplotlib
[Notice] [visualization_practice_seaborn_2]
Comparing seaborn and matplotlib
Scatterplot
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from IPython.display import Image
# seaborn
import seaborn as sns
np.random.rand(50)
array([0.18685692, 0.20077308, 0.5108591 , 0.98427194, 0.66913269, 0.25839253, 0.12455846, 0.36208195, 0.51940446, 0.98073977, 0.93164317, 0.47608911, 0.25808888, 0.3006359 , 0.07242951, 0.80247589, 0.62211407, 0.52930387, 0.81693106, 0.60306402, 0.6191746 , 0.42161375, 0.7631202 , 0.91829187, 0.49359464, 0.37838971, 0.53904915, 0.1098967 , 0.37282266, 0.01167494, 0.1693931 , 0.78190408, 0.56709795, 0.91464527, 0.29112148, 0.19379729, 0.86252864, 0.28122099, 0.84092134, 0.14645608, 0.4497282 , 0.38297336, 0.36435924, 0.25539882, 0.20209437, 0.65057991, 0.89264215, 0.74421442, 0.11566345, 0.43954391])
np.arange(50)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49])
x = np.random.rand(50)
y = np.random.rand(50)
colors = np.arange(50)
area = x * y * 1000
plt.scatter(x, y, s = area, c = colors)
plt.show()
sns.scatterplot(x, y, size=area, sizes=(area.min(), area.max()), hue=area, palette='gist_gray')
plt.show()
/Users/skhu_cv_mac/miniforge3/envs/dylan/lib/python3.10/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation. warnings.warn(
np.random.rand(50)
array([6.34052187e-01, 2.95522253e-01, 8.22591110e-01, 6.09301156e-01, 5.05997270e-01, 5.55471874e-01, 6.49305880e-01, 3.30711116e-01, 3.01480024e-01, 7.42033421e-02, 1.67224156e-01, 4.50868420e-02, 7.73980868e-01, 5.16018329e-01, 8.41756332e-02, 5.57568528e-02, 1.66156309e-01, 5.52816032e-01, 4.82509173e-01, 5.04840668e-01, 8.81870816e-01, 2.18563376e-01, 4.29132851e-02, 8.28928576e-01, 5.08317221e-01, 9.72110630e-01, 5.27448601e-01, 3.83239490e-01, 8.07644275e-01, 8.21329050e-01, 2.77209248e-01, 6.76474038e-01, 2.59683963e-01, 7.58729909e-01, 6.00792190e-01, 1.36635770e-03, 3.44861242e-01, 9.60223496e-01, 3.16422755e-01, 9.98945276e-01, 1.13091041e-04, 5.26750457e-01, 8.12279266e-01, 1.00306155e-01, 9.05878706e-01, 9.98101196e-01, 7.71502685e-01, 5.85003481e-01, 6.03538363e-01, 8.09506557e-01])
plt.figure(figsize=(12, 6))
plt.subplot(131)
plt.scatter(x, y, s=area, c='blue', alpha=0.1)
plt.title('alpha=0.1')
plt.subplot(132)
plt.title('alpha=0.5')
plt.scatter(x, y, s=area, c='red', alpha=0.5)
plt.subplot(133)
plt.title('alpha=1.0')
plt.scatter(x, y, s=area, c='green', alpha=1.0)
plt.show()
plt.figure(figsize=(12, 6))
plt.subplot(131)
sns.scatterplot(x, y, size=area, sizes=(area.min(), area.max()), color='blue', alpha=0.1)
plt.title('alpha=0.1')
plt.subplot(132)
plt.title('alpha=0.5')
sns.scatterplot(x, y, size=area, sizes=(area.min(), area.max()), color='red', alpha=0.5)
plt.subplot(133)
plt.title('alpha=1.0')
sns.scatterplot(x, y, size=area, sizes=(area.min(), area.max()), color='green', alpha=0.9)
plt.show()
/Users/skhu_cv_mac/miniforge3/envs/dylan/lib/python3.10/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation. warnings.warn( /Users/skhu_cv_mac/miniforge3/envs/dylan/lib/python3.10/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation. warnings.warn( /Users/skhu_cv_mac/miniforge3/envs/dylan/lib/python3.10/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation. warnings.warn(
Barplot
x = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
y = [66, 80, 60, 50, 80, 10]
plt.bar(x, y, align='center', alpha=0.7, color='red')
plt.xticks(x)
plt.ylabel('Scores')
plt.title('Subjects')
plt.show()
x = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
y = [66, 80, 60, 50, 80, 10]
sns.barplot(x, y, alpha = 0.8, palette = 'YlGnBu')
plt.ylabel('Scores')
plt.title('Subjects')
plt.show()
/Users/skhu_cv_mac/miniforge3/envs/dylan/lib/python3.10/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation. warnings.warn(
Barhplot
x = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
y = [66, 80, 60, 50, 80, 10]
plt.barh(x, y, align='center', alpha=0.7, color='green')
plt.yticks(x)
plt.xlabel('Scores')
plt.title('Subjects')
plt.show()
- make sure that they do not have a barhplot in seaborn therefore change the position of x and y
x = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
y = [66, 80, 60, 50, 80, 10]
ax = sns.barplot(y, x, alpha = 0.9, palette = 'YlOrRd')
plt.xlabel('Scores')
plt.title('Subjects')
plt.show()
/Users/skhu_cv_mac/miniforge3/envs/dylan/lib/python3.10/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation. warnings.warn(
x_label = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
x = np.arange(len(x_label))
y_1 = [66, 80, 60, 50, 80, 10]
y_2 = [55, 90, 40, 60, 70, 20]
width = 0.35
fig, axes = plt.subplots()
axes.bar(x - width/2, y_1, width, align='center', alpha=0.5)
axes.bar(x + width/2, y_2, width, align='center', alpha=0.8)
plt.xticks(x)
axes.set_xticklabels(x_label)
plt.ylabel('Number of Students')
plt.title('Scores')
plt.legend(['john', 'peter'])
plt.show()
x_label = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
x = np.arange(len(x_label))
y_1 = [66, 80, 60, 50, 80, 10]
y_2 = [55, 90, 40, 60, 70, 20]
width = 0.35
fig, axes = plt.subplots()
axes.barh(x - width/2, y_1, width, align='center', alpha=0.5, color='green')
axes.barh(x + width/2, y_2, width, align='center', alpha=0.8, color='red')
plt.yticks(x)
axes.set_yticklabels(x_label)
plt.xlabel('Score')
plt.title('Subjects')
plt.legend(['john', 'peter'])
plt.show()
tip
-
If you need to draw the graph arbitrarily ->
matplotlib
-
When drawing with a DataFrame ->
seaborn
titanic = sns.load_dataset('titanic')
titanic.head()
survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult_male | deck | embark_town | alive | alone | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 3 | male | 22.0 | 1 | 0 | 7.2500 | S | Third | man | True | NaN | Southampton | no | False |
1 | 1 | 1 | female | 38.0 | 1 | 0 | 71.2833 | C | First | woman | False | C | Cherbourg | yes | False |
2 | 1 | 3 | female | 26.0 | 0 | 0 | 7.9250 | S | Third | woman | False | NaN | Southampton | yes | True |
3 | 1 | 1 | female | 35.0 | 1 | 0 | 53.1000 | S | First | woman | False | C | Southampton | yes | False |
4 | 0 | 3 | male | 35.0 | 0 | 0 | 8.0500 | S | Third | man | True | NaN | Southampton | no | True |
sns.barplot(x = 'sex', y = 'survived', hue = 'pclass', data = titanic, palette = 'muted')
plt.show()
Line Plot
x = np.arange(0, 10, 0.1)
y = 1 + np.sin(x)
plt.plot(x, y)
plt.xlabel('x value', fontsize=15)
plt.ylabel('y value', fontsize=15)
plt.title('sin graph', fontsize=18)
plt.show()
# whitegrid, darkgrid, white, dark, ticks
sns.set_style("darkgrid")
sns.lineplot(x, y)
plt.xlabel('x value', fontsize=15)
plt.ylabel('y value', fontsize=15)
plt.title('sin graph', fontsize=18)
plt.show()
/Users/skhu_cv_mac/miniforge3/envs/dylan/lib/python3.10/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation. warnings.warn(
Areaplot (Filled Area)
y = np.random.randint(low=5, high=10, size=20)
y
array([8, 8, 5, 8, 5, 8, 7, 9, 6, 5, 9, 5, 5, 7, 8, 7, 9, 9, 9, 5])
x = np.arange(1,21)
y = np.random.randint(low=5, high=10, size=20)
plt.fill_between(x, y, color="green", alpha=0.6)
plt.show()
plt.fill_between( x, y, color="green", alpha=0.3)
plt.plot(x, y, color="green", alpha=0.8)
[<matplotlib.lines.Line2D at 0x17799f280>]
x = np.arange(1, 10, 0.05)
y_1 = np.cos(x)+1
y_2 = np.sin(x)+1
y_3 = y_1 * y_2 / np.pi
plt.fill_between(x, y_1, color="green", alpha=0.1)
plt.fill_between(x, y_2, color="blue", alpha=0.2)
plt.fill_between(x, y_3, color="red", alpha=0.3)
<matplotlib.collections.PolyCollection at 0x177a0b250>
Seaborn does not support area plots.
It should be implemented utilizing matplotlib
.
Histogram
N = 100000
bins = 30
x = np.random.randn(N)
plt.hist(x, bins=bins)
plt.show()
sns.distplot(x, bins = bins, kde = False, hist = True, color = 'g')
/Users/skhu_cv_mac/miniforge3/envs/dylan/lib/python3.10/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms). warnings.warn(msg, FutureWarning)
<AxesSubplot:>
If kde
is set to True, Density is displayed on the Y axis.
sns.distplot(x, bins = bins, kde = True, hist = False, color = 'g')
/Users/skhu_cv_mac/miniforge3/envs/dylan/lib/python3.10/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `kdeplot` (an axes-level function for kernel density plots). warnings.warn(msg, FutureWarning)
<AxesSubplot:ylabel='Density'>
sns.distplot(x, bins=bins, kde=True, hist=True, vertical=True, color='r')
/Users/skhu_cv_mac/miniforge3/envs/dylan/lib/python3.10/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms). warnings.warn(msg, FutureWarning) /Users/skhu_cv_mac/miniforge3/envs/dylan/lib/python3.10/site-packages/seaborn/distributions.py:1689: FutureWarning: The `vertical` parameter is deprecated and will be removed in a future version. Assign the data to the `y` variable instead. warnings.warn(msg, FutureWarning)
<AxesSubplot:xlabel='Density'>
Pie Chart
labels = ['Samsung', 'Huawei', 'Apple', 'Xiaomi', 'Oppo', 'Etc']
sizes = [20.4, 15.8, 10.5, 9, 7.6, 36.7]
explode = (0.3, 0, 0, 0, 0, 0)
patches, texts, autotexts = plt.pie(sizes, explode=explode,
labels=labels,
autopct='%1.1f%%',
shadow=True,
startangle=90)
plt.title('Smartphone pie', fontsize=15)
for t in texts:
t.set_fontsize(12)
t.set_color('gray')
for t in autotexts:
t.set_color("white")
t.set_fontsize(18)
plt.show()
Seaborn does not support pie plots
It should be implemented utilizing matplotlib
.
Box Plot
spread = np.random.rand(50) * 100
center = np.ones(25) * 50
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
data = np.concatenate((spread, center, flier_high, flier_low))
plt.boxplot(data)
plt.tight_layout()
plt.show()
sns.boxplot(data, orient = 'v', width = 0.2)
plt.show()
/Users/skhu_cv_mac/miniforge3/envs/dylan/lib/python3.10/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation. warnings.warn( /Users/skhu_cv_mac/miniforge3/envs/dylan/lib/python3.10/site-packages/seaborn/_core.py:1326: UserWarning: Vertical orientation ignored with only `x` specified. warnings.warn(single_var_warning.format("Vertical", "x"))
titanic = sns.load_dataset('titanic')
titanic.head()
survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult_male | deck | embark_town | alive | alone | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 3 | male | 22.0 | 1 | 0 | 7.2500 | S | Third | man | True | NaN | Southampton | no | False |
1 | 1 | 1 | female | 38.0 | 1 | 0 | 71.2833 | C | First | woman | False | C | Cherbourg | yes | False |
2 | 1 | 3 | female | 26.0 | 0 | 0 | 7.9250 | S | Third | woman | False | NaN | Southampton | yes | True |
3 | 1 | 1 | female | 35.0 | 1 | 0 | 53.1000 | S | First | woman | False | C | Southampton | yes | False |
4 | 0 | 3 | male | 35.0 | 0 | 0 | 8.0500 | S | Third | man | True | NaN | Southampton | no | True |
sns.boxplot(x = 'pclass', y = 'age', hue = 'survived', data = titanic)
plt.show()
How to change Box Plot’s axis
vert=False
plt.title('Horizontal Box Plot', fontsize=15)
plt.boxplot(data, vert=False)
plt.show()
댓글남기기