python中是否有函数可以轻松绘制出这样的子图?

Is there a function in python that can easily plot such a subplot?

fig, ax = plt.subplots()
  
# plot.subplot(nrows, ncols, index of figure, **kwargs)

plt.subplot(1, 2, 1)
x = df1.Age(yrs)
y = df1[['']]
plt.plot(kind = 'line', x = 'Age(yrs)', y  = 'Sell Price($)', color = "black")

plt.subplot(1, 2, 2)
plt.plot(kind = 'line', x = 'Age(yrs)', y = 'Mileage', color = "green")

plt.show()

plt.subplot(1, 3, 1)
plt.title("BMW X5")
plt.plot(x,y, color = "black")

# plot 2:
plt.subplot(1, 3, 2)
plt.title("Audi A5")
plt.plot(x, y, color = "green")

# plot 3:
plt.subplot(1, 3, 3)
plt.title("Mercedez Benz C class")
plt.plot(x, y, color = 'red')

plt.show()

车型 里程 售价($) 年龄(岁)
宝马X5 69000 18000 6
宝马X5 35000 34000 3
宝马X5 57000 26100 5
宝马X5 22500 40000 2
宝马X5 46000 31500 4
奥迪A5 59000 29400 5
奥迪A5 52000 32000 5
奥迪A5 72000 19300 6
奥迪A5 91000 12000 8
梅赛德斯奔驰 C class 67000 22000 6
梅赛德斯奔驰 C class 83000 20000 7
梅赛德斯奔驰 C class 79000 21000 7
梅赛德斯奔驰 C class 59000 33000 5

Seaborn 的regplot() 可以用来创建这样的情节。这是一些示例代码。

from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
from io import StringIO

data_str = '''"Car Model"   Mileage "Sell Price($)" Age(yrs)
"BMW X5"    69000   18000   6
"BMW X5"    35000   34000   3
"BMW X5"    57000   26100   5
"BMW X5"    22500   40000   2
"BMW X5"    46000   31500   4
"Audi A5"   59000   29400   5
"Audi A5"   52000   32000   5
"Audi A5"   72000   19300   6
"Audi A5"   91000   12000   8
"Mercedez Benz C class" 67000   22000   6
"Mercedez Benz C class" 83000   20000   7
"Mercedez Benz C class" 79000   21000   7
"Mercedez Benz C class" 59000   33000   5'''

df = pd.read_csv(StringIO(data_str), delim_whitespace=True)
fig, axs = plt.subplots(nrows=3, ncols=2, figsize=(12, 10))

sns.regplot(data=df[df["Car Model"] == "BMW X5"], x='Age(yrs)', y='Sell Price($)', ax=axs[0, 0])
sns.regplot(data=df[df["Car Model"] == "BMW X5"], x='Age(yrs)', y='Mileage', ax=axs[0, 1])
axs[0, 0].set_title("BWM X5")
axs[0, 1].set_title("BWM X5")

sns.regplot(data=df[df["Car Model"] == "Audi A5"], x='Age(yrs)', y='Sell Price($)', ax=axs[1, 0])
sns.regplot(data=df[df["Car Model"] == "Audi A5"], x='Age(yrs)', y='Mileage', ax=axs[1, 1])
axs[1, 0].set_title("Audi A5")
axs[1, 1].set_title("Audi A5")

sns.regplot(data=df[df["Car Model"] == "Mercedez Benz C class"], x='Age(yrs)', y='Sell Price($)', ax=axs[2, 0])
sns.regplot(data=df[df["Car Model"] == "Mercedez Benz C class"], x='Age(yrs)', y='Mileage', ax=axs[2, 1])
axs[2, 0].set_title("Mercedez Benz C class")
axs[2, 1].set_title("Mercedez Benz C class")
plt.tight_layout()
plt.show()

PS:您可能想了解 plt.subplots()plt.subplot() 之间的区别(有和没有最终 s)。例如:.

通过额外的变量和 for 循环,需要重复的行更少。例如,相同的代码可以写成:

fig, axs = plt.subplots(nrows=3, ncols=2, figsize=(12, 10))
car_models = df["Car Model"].unique()
for ax_row, car_model in zip(axs, car_models):
    for ax, y_column in zip(ax_row, ['Sell Price($)', 'Mileage']):
        sns.regplot(data=df[df["Car Model"] == car_model], x='Age(yrs)', y=y_column, ax=ax)
        ax.set_title(car_model)
plt.tight_layout()
plt.show()

Seaborn 还允许使用 sns.relplot(). For this to work, the two y-columns need to be "melted" to create a "long form" dataframe.

一次创建完整的子图集