0

Problem:

I have a list of ~108 dictionaries named list_of_dictionary and I would like to use Matplotlib to generate line graphs.

The dictionaries have the following format (this is one of 108):

{'price': [59990, 
           59890,
           60990,
           62990, 
           59990, 
           59690], 
 'car': '2014 Land Rover Range Rover Sport', 
 'datetime': [datetime.datetime(2020, 1, 22, 11, 19, 26), 
              datetime.datetime(2020, 1, 23, 13, 12, 33), 
              datetime.datetime(2020, 1, 28, 12, 39, 24), 
              datetime.datetime(2020, 1, 29, 18, 39, 36), 
              datetime.datetime(2020, 1, 30, 18, 41, 31), 
              datetime.datetime(2020, 2, 1, 12, 39, 7)]
}

Understanding the dictionary:

The car 2014 Land Rover Range Rover Sport was priced at:

  • 59990 on datetime.datetime(2020, 1, 22, 11, 19, 26)
  • 59890 on datetime.datetime(2020, 1, 23, 13, 12, 33)
  • 60990 on datetime.datetime(2020, 1, 28, 12, 39, 24)
  • 62990 on datetime.datetime(2020, 1, 29, 18, 39, 36)
  • 59990 on datetime.datetime(2020, 1, 30, 18, 41, 31)
  • 59690 on datetime.datetime(2020, 2, 1, 12, 39, 7)

Question:

With this structure how could one create mini-graphs with matplotlib (say 11 rows x 10 columns)?

Where each mini-graph will have:

  • the title of the graph frome car
  • x-axis from the datetime
  • y-axis from the price

What I have tried:

df = pd.DataFrame(list_of_dictionary)
df = df.set_index('datetime')
print(df)

I don't know what to do thereafter...

Relevant Research:

I've read these multiple times, but the more I read it, the more confused I get :(.

3kstc
  • 1,871
  • 3
  • 29
  • 53

1 Answers1

0

I don't know if it's sensible to try and plot that many plots on a figure. You'll have to make some choices to be able to fit all the axes decorations on the page (titles, axes labels, tick labels, etc...).

but the basic idea would be this:

car_data = [{'price': [59990, 
           59890,
           60990,
           62990, 
           59990, 
           59690], 
 'car': '2014 Land Rover Range Rover Sport', 
 'datetime': [datetime.datetime(2020, 1, 22, 11, 19, 26), 
              datetime.datetime(2020, 1, 23, 13, 12, 33), 
              datetime.datetime(2020, 1, 28, 12, 39, 24), 
              datetime.datetime(2020, 1, 29, 18, 39, 36), 
              datetime.datetime(2020, 1, 30, 18, 41, 31), 
              datetime.datetime(2020, 2, 1, 12, 39, 7)]
}]*108

fig, axs = plt.subplots(11,10, figsize=(20,22)) # adjust figsize as you please

for car,ax in zip(car_data, axs.flat):
    ax.plot(car["datetime"], car['price'], '-')
    ax.set_title(car['car'])

Ideally, all your axes could share the same x and y axes so you could have the labels only on the left-most and bottom-most axes. This is taken care of automatically if you add sharex=True and sharey=True to subplots():

fig, axs = plt.subplots(11,10, figsize=(20,22), sharex=True, sharey=True) # adjust figsize as you please
Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75