Seaborn
[Notice] [visualization_practice_seaborn_first]
Seaborn
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from IPython.display import Image
# seaborn
import seaborn as sns
plt.rc('font', family='NanumBarunGothic')
plt.rcParams["figure.figsize"] = (12, 9)
plots that are based on statistical in Seaborn
tips = sns.load_dataset("tips")
sns.violinplot(x = 'day', y = 'total_bill', data = tips)
plt.title('violin plot')
plt.show()
findfont: Font family ['NanumBarunGothic'] not found. Falling back to DejaVu Sans. findfont: Font family ['NanumBarunGothic'] not found. Falling back to DejaVu Sans.
sns.countplot(tips['day'])
plt.title('countplot')
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(
sns.relplot(x = 'tip', y = 'total_bill', data = tips)
plt.title('relplot')
plt.show()
sns.lmplot(x = 'tip', y = 'total_bill', data = tips)
plt.title('lmplot')
plt.show()
sns.heatmap(tips.corr(), annot = True, linewidths = 1)
plt.title('heatmap')
plt.show()
Styling seaborn
plt.bar(tips['day'], tips['total_bill'])
plt.show()
sns.barplot(x="day", y="total_bill", data=tips, palette='colorblind')
plt.show()
Color palette
sns.palplot(sns.light_palette((210, 90, 60), input="husl"))
sns.palplot(sns.dark_palette("muted purple", input="xkcd"))
sns.palplot(sns.color_palette("BrBG", 10))
sns.palplot(sns.color_palette("BrBG_r", 10))
sns.palplot(sns.color_palette("coolwarm", 10))
sns.palplot(sns.diverging_palette(255, 133, l=60, n=10, center="dark"))
sns.barplot(x="tip", y="total_bill", data=tips, palette='coolwarm')
<AxesSubplot:xlabel='tip', ylabel='total_bill'>
sns.barplot(x="tip", y="total_bill", data=tips, palette='Reds')
<AxesSubplot:xlabel='tip', ylabel='total_bill'>
It shows high compatibility with pandas dataframes
tips
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
... | ... | ... | ... | ... | ... | ... | ... |
239 | 29.03 | 5.92 | Male | No | Sat | Dinner | 3 |
240 | 27.18 | 2.00 | Female | Yes | Sat | Dinner | 2 |
241 | 22.67 | 2.00 | Male | Yes | Sat | Dinner | 2 |
242 | 17.82 | 1.75 | Male | No | Sat | Dinner | 2 |
243 | 18.78 | 3.00 | Female | No | Thur | Dinner | 2 |
244 rows × 7 columns
sns.catplot(x = 'sex', y = 'total_bill', data = tips, kind = 'bar')
plt.show()
# hue option
sns.catplot(x = 'sex', y = 'total_bill', hue = 'smoker', data = tips, kind = 'bar')
plt.show()
# col option
sns.catplot(x = 'sex', y = 'total_bill', hue = 'smoker',col = 'time', data = tips, kind = 'bar')
plt.show()
댓글남기기