1
  • Ultimate goal is to analyse the data and see if linear regresssion is suitable or not.
  • Goal is to get 3 matplot.subplots for each of the 3 cars (BMW, Audi A5, Mercedes Benz)
  • I tried writing a code, but couldn't get it working:
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
n3a5p7s9t1e3r
  • 284
  • 2
  • 12
  • 3
    Maybe seaborns `sns.regplot(data=df1, x='Age(yrs)', y='Sell Price($)')`? [docs](https://seaborn.pydata.org/generated/seaborn.relplot.html) – JohanC Jun 03 '21 at 11:24
  • seaborn does not allow subplots. So that is out of the question. – n3a5p7s9t1e3r Jun 03 '21 at 16:54
  • Seaborn allows a lot of subplots. You need to create the axes via matplotlib. If you use `plt.subplot(1, 2, 1)`, seaborn will draw on the latest created subplot. You can also give the subplot a name, usually this is a variant of `ax`, e.g. `ax1 = plt.subplot(1, 2, 1)` and afterwards use `sns.regplot(data=df1, x=..., y=..., ax=ax1)`. – JohanC Jun 03 '21 at 17:02

1 Answers1

0

Seaborn's regplot() can be used to create such a plot. Here is some example code.

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()

grid of sns.regplot

PS: You might want to read about the difference between plt.subplots() and plt.subplot() (with and without final s). For example: What is the difference between drawing plots using plot, axes or figure in matplotlib?.

Via extra variables and for loops, fewer lines need to repeated. For example, the same code could be written as:

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 also allows to create the full set of subplots in one go, using sns.relplot(). For this to work, the two y-columns need to be "melted" to create a "long form" dataframe.

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Thank you! I was under such a wrong impression about seaborn. I'm sorry for not having explored the seaborn library and commentign about it. I'm new to this field. Thank you so much. This answers my question. – n3a5p7s9t1e3r Jun 04 '21 at 09:21