【问题标题】:Is there a function in python that can easily plot such a subplot?python中是否有一个函数可以轻松绘制这样的子图?
【发布时间】:2021-06-03 10:45:03
【问题描述】:
  • 最终目标是分析数据并查看线性回归是否合适。
  • 目标是为 3 辆汽车(BMW、Audi A5、Mercedes Benz)中的每一辆获得 3 个 matplot.subplots
  • 我尝试编写代码,但无法正常工作:
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()

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

【问题讨论】:

  • 也许是seaborns sns.regplot(data=df1, x='Age(yrs)', y='Sell Price($)')docs
  • seaborn 不允许子图。所以这是不可能的。
  • Seaborn 允许很多子图。您需要通过 matplotlib 创建轴。如果您使用plt.subplot(1, 2, 1),seaborn 将使用最新创建的子图。您还可以为子图命名,通常这是ax 的变体,例如ax1 = plt.subplot(1, 2, 1) 之后使用sns.regplot(data=df1, x=..., y=..., ax=ax1)

标签: python matplotlib data-science subplot


【解决方案1】:

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)。例如:What is the difference between drawing plots using plot, axes or figure in matplotlib?

通过额外的变量和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() 一次性创建完整的子图集。为此,需要将两个 y 列“融合”以创建 "long form" dataframe

【讨论】:

  • 谢谢!我对seaborn有这样的错误印象。我很抱歉没有探索 seaborn 图书馆并对其进行评论。我是这个领域的新手。非常感谢。这回答了我的问题。
猜你喜欢
  • 1970-01-01
  • 2015-06-25
  • 2023-02-03
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
相关资源
最近更新 更多