0

I have a list of data frames:

list_df = [df1, df2, df3, df4, df5, df6]

of which are are defined by fairly identical commands:

df6 = df_2016['Incident Type Description'].value_counts()[:20]

Where df1 = df_2011['Incident Type Description'].value_counts()[:20], df2 = df_2012['Incident Type Description'].value_counts()[:20] and so on to df6 = df_2016['Incident Type Description'].value_counts()[:20]. df_2011, df_2012...df_2016 are named variables from csv files I uploaded.

What I wish to do is join all these to create a 2x3 subplot instead of having them individually displayed of which I had with the command: df_2016['Incident Type Description'].value_counts()[:20].plot(kind='barh', title='Top 20 crimes in Oakland in 2016')

I have tried a few approaches so far that I have found on stackoverflow [How can I plot separate Pandas DataFrames as subplots? and the matplotlib page, but these do not work and I don't know why (I'm a beginner in python):

axes= plt.subplots(2, 3)
list_df = [df1, df2, df3, df4, df5, df6]

columns = [f'Top 20 crimes in {x}' for x in range(2011, 2017)]

list_df.plot.barh(subplots=True, layout=(2, 3), figsize=(15,7))

So far ValueError comes up as ValueError: Layout of 2x3 must be larger than required size 7. Any ideas?

  • What exactly is `df` that you are plotting? Aren't you trying to plot `df1,df2,...`? – Quang Hoang Sep 26 '19 at 12:46
  • @QuangHoang Yes that's supposed to be list_df, sorry only just realised I left that out. Now `AttributeError: 'list' object has no attribute 'plot'`. How do I get around this? – pragmatic learner Sep 26 '19 at 12:53

2 Answers2

0

I don't know if you planned to use another package. But Seaborn is quite often used to display nicely and quickly different kinds of plots. You can check the complete package doc : Seaborn Doc. There are some nice tutorials.

Especially FacetGrid to display suplots. FacetGrid Doc

Seaborn is built on top of matplot lib.

Edit : You could maybe need to append all you df, and then subplot based on a column specifying the input df(1-6).

Gwendal Yviquel
  • 382
  • 3
  • 13
0

You can try:

fig, axes = plt.subplots(2,3)

for d, ax in zip(list_df, axes.ravel()):
    d.plot.barh(ax=ax)
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74