chapter1
1.1 Introduction to Seaborn
import seaborn as snsimport matplotlib.pyplot as plt# Example 1: Scatter plotheight = [62, 64, 69, 75, 66, 68, 65, 71, 76, 73]weight = [120, 136, 148, 175, 137, 165, 154, 172, 200, 187]sns.scatterplot(x=height, y=weight)plt.show()# Example 2: Create a count plotgender = ["Female", "Female", "Female", "Female", "Male", "Male", "Male", "Male", "Male", "Male"]sns.countplot(x=gender)plt.show()
Count plots: take in a categorical list and return bars that represent the number of list entries per category
1.2 Using pandas with Seaborn

# Using DataFrames with countplot()import pandas as pdimport matplotlib.pyplot as pltimport seaborn as snsdf = pd.read_csv("masculinity.csv")sns.countplot(x="how_masculine", data=df)plt.show()
1.3 Adding a third variable with hue
import pandas as pdimport seaborn as snstips = sns.load_dataset("tips")tips.head()# basic scatterplotsns.scatterplot(x="total_bill", y="tip", data=tips)plt.show()# A scatter plot with huesns.scatterplot(x="total_bill", y="tip", data=tips, hue="smoker")plt.show()# Setting hue ordersns.scatterplot(x="total_bill", y="tip", data=tips, hue="smoker", hue_order=["Yes", "No"])plt.show()# specifying hue colors 使用字典传参,自定义颜色标签hue_colors = {"Yes": "black", "No": "red"}sns.scatterplot(x="total_bill", y="tip", data=tips, hue="smoker", palette=hue_colors)plt.show()# Using hue with count plotssns.countplot(x="smoker", data=tips, hue="sex")plt.show()
chapter 2
1.1 relational plots: scatter plots or line plots —- relplot()
1.2 subplots
import seaborn as snsimport matplotlib.pyplot as plt# subplots in columnssns.relplot(x="total_bill", y="tip", data=tips, kind="scatter", col="smoker")plt.show()# subplots in rowssns.relplot(x="total_bill", y="tip", data=tips, kind="scatter", row="smoker")plt.show()# Subplots in rows and columnssns.relplot(x="total_bill", y="tip", data=tips, kind="scatter", col="smoker", row="time")plt.show()# Wrapping columnssns.relplot(x="total_bill", y="tip", data=tips, kind="scatter", col="day", col_wrap=2)plt.show()# Ordering columnssns.relplot(x="total_bill", y="tip", data=tips, kind="scatter", col="day", \col_wrap=2, col_order=["Thur", "Fri", "Sat", "Sun"])plt.show()
1.3 Customizing scatter plots
# Subgroups with point size
sns.relplot(x="total_bill", y="tip", data=tips, kind="scatter", size="size")
plt.show()
# Point size and hue
sns.relplot(x="total_bill", y="tip", data=tips, kind="scatter", size="size", hue="size")
plt.show()
# Subgroups with point style
sns.relplot(x="total_bill", y="tip", data=tips, kind="scatter", hue="smoker", style="smoker")
plt.show()
# point transparency: Set alpha to be between 0 and 1
sns.relplot(x="total_bill", y="tip", data=tips, kind="scatter", alpha=0.4)
plt.show()
![]() |
![]() |
|---|---|
![]() |
![]() |
1.4 Introduction to line plots
Two types of relational plots: scatter plots and line plots
- Scatter plots: Each plot point is an independent observation.
- Line plots: Each plot point represents the same “thing”, typically tracked over time. ```python import matplotlib.pyplot as plt import seaborn as sns
line plot: 一个x对应一个点
sns.relplot(x=”hour”, y=”NO_2_mean”, data=air_df_mean, kind=”line”) plt.show()
Subgroups:线图展示分组
sns.relplot(x=”hour”, y=”NO_2_mean”, data=air_df_loc_mean, kind=”line”, style=”location”, hue=”location”, markers=True) plt.show()
Multiple observations per x-value: 一个x值对应多个点,自动汇总
sns.relplot(x=”hour”, y=”NO_2”, data=air_df, kind=”scatter”) plt.show()
sns.relplot(x=”hour”, y=”NO_2”, data=air_df, kind=”line”) plt.show()
sns.relplot(x=”hour”, y=”NO_2”, data=air_df, kind=”line”, ci=”sd”) plt.show() # 将置信区间替换成标准偏差
sns.relplot(x=”hour”, y=”NO_2”, data=air_df, kind=”line”, ci=None) plt.show() # 关闭置信区间显示
[seaborn_1_chapter3.pdf](https://www.yuque.com/attachments/yuque/0/2022/pdf/26718834/1648902304864-e32e5be2-24cd-46f3-a264-d2de179122fc.pdf)
<a name="qQ2pW"></a>
# chapter 3
<a name="ygjue"></a>
## 1.1 Count plots and bar plots: catplot()
**Categorical plots:count plots **and **bar plots**
- Involve a categorical variable
- Comparisons between groups
```python
## count plots
# countplot vs. catplot
sns.countplot(x="how_masculine", data=masculinity_data)
plt.show()
sns.catplot(x="how_masculine", data=masculinity_data, kind="count")
plt.show()
# Changing the order
category_order = ["No answer", "Not at all", "Not very", "Somewhat", "Very"]
sns.catplot(x="how_masculine",
data=masculinity_data,
kind="count",
order=category_order)
plt.show()
## Bar plots: Lines show 95% con,dence intervals for the mean
sns.catplot(x="day", y="total_bill", data=tips, kind="bar")
plt.show()
# Changing the orientation
sns.catplot(x="total_bill", y="day", data=tips, kind="bar")
plt.show()
1.2 box plot
sns.catplot(x="time", y="total_bill", data=tips, kind="box")
plt.show()
# change the order of categories
sns.catplot(x="time", y="total_bill",
data=tips, kind="box",
order=["Dinner", "Lunch"])
plt.show()
# Omitting the outliers using `sym`
sns.catplot(x="time", y="total_bill",
data=tips, kind="box",
sym="")
plt.show()
# Changing the whiskers using `whis`
# By default, the whiskers extend to 1.5 * the interquartile range
# Make them extend to 2.0 * IQR: whis=2.0
# Show the 5th and 95th percentiles: whis=[5, 95]
# Show min and max values: whis=[0, 100]
1.3 point plots
- Points show mean of quantitative variable
- Vertical lines show 95% condence intervals


sns.catplot(x="age", y="masculinity_important",
data=masculinity_data,
hue="feel_masculine", kind="point")
plt.show()
# Disconnecting the points
sns.catplot(x="age", y="masculinity_important",
data=masculinity_data,
hue="feel_masculine",
kind="point", join=False)
plt.show()
# Displaying the median
sns.catplot(x="smoker", y="total_bill",
data=tips, kind="point",
estimator=median)
plt.show()
# Customizing the confidence intervals
sns.catplot(x="smoker", y="total_bill",
data=tips, kind="point",
capsize=0.2)
plt.show()
chapter 4
1.1 Changing plot style and color
1.1.1 Changing the figure style :background and axes —- sns.set_style()
Preset options: “white”, “dark”, “whitegrid”, “darkgrid”, “ticks”
# Default figure style ("white") sns.set_style("whitegrid") sns.set_style("ticks") sns.set_style("dark") sns.set_style("darkgrid")1.1.2 Changing the palette:color of the elements in plot —- sns.set_palette()
Use preset palettes or create a custom palette


sns.set_palette("RdBu")
# custom palettes
custom_palette = ["red", "green", "orange", "blue", "yellow", "purple"]
sns.set_palette(custom_palette)
1.1.3 Changing the scale : scale of plot elements and labels — sns.set_context()
- Smallest to largest: “paper”, “notebook”, “talk”, “poster”
# Default context: "paper" sns.set_context("talk")1.2 Adding titles and labels
Seaborn plots create two different types of objects: FacetGrid and AxesSubplotg = sns.scatterplot(x="height", y="weight", data=df) type(g)
1.2.1 Adding a title to FacetGrid : g.fig.suptitle()
```python g = sns.catplot(x=”Region”, y=”Birthrate”, data=gdp_data, kind=”box”) g.fig.suptitle(“New Title”) plt.show()
Adjusting height of title in FacetGrid
g.fig.suptitle(“New Title”, y=1.03)
<a name="Yot0Q"></a>
### 1.2.2 Adding a title to AxesSubplot

```python
# Adding Titles for subplots
g = sns.catplot(x="Region", y="Birthrate", data=gdp_data, kind="box", col="Group")
g.fig.suptitle("New Title", y=1.03) # 整个图的标题
g.set_titles("This is {col_name}") # 子图标题
1.2.3 Adding axis labels
g = sns.catplot(x="Region", y="Birthrate", data=gdp_data, kind="box")
g.set(xlabel="New X Label", ylabel="New Y Label")
plt.show()
# Rotating x-axis tick labels
plt.xticks(rotation=90)
1.2.4 Adding a third variable
- as a subgroups: hue
- as a subplots: col/row




