0

I have a dataset of sold cars like this (by model):

model, year, price

Ford Focus, 2006, 5000

Ford Focus, 2000, 1500

I'd like to calculate several statistics ant plot them at the same image:

  1. average offer price by year
  2. year to year loose
  3. number of offers by year

I'm using pandas library to count statistics:

import pandas as pd
import matplotlib.pyplot as plt
#csv format
# model, year, price
# Ford Focus, 2004, 5000
cars = pd.read_csv(path)
focuses = cars[lambda x: x["model"] == "Ford Focus"]
avg_prices = focuses.groupby("year").mean()
offers_count = focuses.groupby("year").count()["price"]

avg_prices.plot()
offers_count.plot.bar(rot=0)

plt.title("Ford Focus")

plt.show()

But I have to images instead of one.

How to combine statistics to one image where plots has the same x axis (like stock charts)?

enter image description here

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Alex Klimashevsky
  • 2,457
  • 3
  • 26
  • 58
  • Does [How to plot multiple lines in one figure in Pandas Python based on data from multiple columns?](https://stackoverflow.com/q/40071096/8239061) answer your question? – SecretAgentMan Nov 10 '20 at 17:30
  • @SecretAgentMan no, because plots has different kinds. first one is the line and second one is the bar – Alex Klimashevsky Nov 10 '20 at 17:31
  • Does [How can I recreate this plot of a pandas DataFrame, line and bar](https://stackoverflow.com/q/63666509/8239061) answer your question? – SecretAgentMan Nov 10 '20 at 17:33
  • Possibly related: [How to plot a line over a bar chart](https://stackoverflow.com/q/62987103/8239061) | [Pandas: plot line and bar plot in the same figure _with custom index_](https://stackoverflow.com/q/63125109/8239061) – SecretAgentMan Nov 10 '20 at 17:35
  • @SecretAgentMan added image to clear what kind of plot I'd like to have – Alex Klimashevsky Nov 10 '20 at 17:35
  • Related: candlesticks: https://stackoverflow.com/a/6272093/8239061 – SecretAgentMan Nov 10 '20 at 17:37
  • A quick Google search found [this article](https://towardsdatascience.com/python-stock-analysis-candlestick-chart-with-python-and-plotly-e619143642bb) complete with code. One of the top results... – SecretAgentMan Nov 10 '20 at 17:38
  • @SecretAgentMan the issue to plot 2 statistics of different dimensions. I cannot show the price and count using the same axis. Financial charts has the same x axis but different y axis – Alex Klimashevsky Nov 10 '20 at 17:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224373/discussion-between-alex-klimashevsky-and-secretagentman). – Alex Klimashevsky Nov 10 '20 at 17:42

0 Answers0