# Seaborn 子图

Manav Narula 2023年1月30日 2021年7月16日

## 在 Python 中使用 `matplotlib.pyplot.subplots` 函数绘制 Seaborn 子图

``````import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

fig, axes = plt.subplots(1, 2)
``````

``````import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({"Price 1": [7,1,5,6,3,10,5,8],
"Price 2" : [1,2,8,4,3,9,5,2],
"Day":[1,2,3,4,5,6,7,8]})

fig, axes = plt.subplots(1, 2)

sns.lineplot(x = 'Day', y = 'Price 1',data = df, ax = axes[0])
sns.lineplot(x = 'Day', y = 'Price 2',data = df, ax = axes[1])
axes[0].set_title("First")
axes[1].set_title("Second")
plt.suptitle("Main")
``````

## 在 Python 中使用 `matplotlib.pyploy.add_subplot()` 函数绘制 Seaborn 子图

``````import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({"Price 1": [7,1,5,6,3,10,5,8],
"Price 2" : [1,2,8,4,3,9,5,2],
"Day":[1,2,3,4,5,6,7,8]})

fig = plt.figure()

for i in range(1, 3):
axes = fig.add_subplot(1, 2, i)
if(i == 1):
sns.lineplot(x = 'Day', y = 'Price 1',data = df)
else:
sns.lineplot(x = 'Day', y = 'Price 2',data = df)
``````

Author: Manav Narula

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn