I want to plot sales in Y axe and time in X axe. One line per each car model. I managed to plot one line for the total sales. Any suggestions to get lines for each car model?
cars = {"Date" : ["2020-01-29", "2020-01-02", "2020-01-24", "2020-05-21", "2020-12-31", "2020-12-01", "2020-12-01"],
"Brand": ["Honda Civic","Toyota Corolla","Honda Civic", "Ford Focus","Audi A4", "Ford Focus", "Ford Focus"],
"Price": [22000,25000,22000,27000,35000,27000,27000]
}
df2 = pd.DataFrame(cars, columns = ["Date", "Brand", "Price"])
df2["Date"] = pd.to_datetime(df2["Date"])
df2=df2.sort_values(by="Date")
df2.index = df2["Date"]
gb2=df2.groupby([df2.index.year.values,df2.index.month.values,df2.index.day.values]).sum()
gb2.plot.line()
plt.show()
You can plot a line per car model via the following. Note that the marker
is used because lines in isolation would not show any plot data for the Toyota Corolla or Audi A4, which are represented by only a single data point each.
fig,ax = plt.subplots()
for (i, b) in df2.groupby('Brand'):
b.plot(x='Date', y='Price', marker='o', ax=ax, label=i)
ax.legend()