0

I have the charts I want but they show up in a single column. I would like two or three columns. This is what I have so far:

teamlist = ['ATL', 'BAL', 'BOS', 'LAA', 'CHA', 'CHN', 'CIN', 'CLE', 'DET',
       'HOU', 'KCR', 'LAD', 'MIN', 'MIL', 'MON', 'NYY', 'NYM', 'OAK',
       'PHI', 'PIT', 'SDP', 'SEA', 'SFG', 'STL', 'TEX', 'TOR', 'COL',
       'MIA', 'ARI', 'TBA', 'WAS']

for i in teamlist:
    data = teamdf[teamdf['teamID'] == i]
    fig, ax1 = plt.subplots(figsize=(5,3))
    ax1 = data.groupby('yearID').attendance.sum().plot(kind='line',label=i)
    ax1.set_yticks([0, 1000000, 2000000, 3000000, 4000000])
    ax1.set_yticklabels(labels=['0', '1M', '2M', '3M', '4M'], fontsize=10, color='#414141')
    ax1.set_xticks([1990, 1995, 2000, 2005, 2010, 2015, 2019])
    ax1.set_xticklabels(labels=['1990', '1995', '2000', '2005', '2010','2015','2019'], fontsize=10, color='#414141')
    mean = teamdf.groupby('yearID').attendance.mean().plot(kind='line',color='gray',label='League Avg.')
    ax1.set(title=i + ' Attendance 1990-2019', ylabel='Tickets Sold', xlabel='Year')
    ax1.legend(loc='best')

I tried changing the number of rows and columns in the plt.subplots line in the loop but it created multiple columns for every single chart, and that's not what I want. I think I might need to do something before the loop to get 3 columns of charts but not sure.

ChrisC
  • 1
  • 1
    read this: [how-to-make-two-plots-side-by-side-using-python](https://stackoverflow.com/questions/42818361/how-to-make-two-plots-side-by-side-using-python) – Peter Prescott Apr 08 '20 at 14:34
  • I tried that before posting my question, but when I set the rows and columns (inside the loop), it just creates multiple columns for every single chart. I want a 3 columns by 11 rows for my charts. I think I would have to put something before my loop clarifying the shape I want but I can't seem to figure it out. – ChrisC Apr 08 '20 at 15:04

2 Answers2

0

You need to define the plt.subplots() before you start iterating over your teamlist.

Then when you call your .plot() you need to specify that you want to plot on an axis that you've already created, by specifying .plot(ax=selected_ax).

Something like this:

fig, axs = plt.subplots(3,11, figsize=(20,20))

for i, team in enumerate(teamlist):
    row_number = i % 3
    column_number = i // 3
    selected_ax = axs[row_number, column_number]
    data = teamdf[teamdf['teamID'] == team]
    data.plot(kind='line',label=team, ax=selected_ax)
Peter Prescott
  • 768
  • 8
  • 16
0

You need to define the figure outside of the loop before creating the individual plots within the loop. Here's an example that will hopefully preserve the code you've already written by using some of the same variable names.

fig = plt.figure()

for id, i in enumerate(teamlist):
  # define grid 11 rows x 3 cols and create subplot in order beginning at top left
  ax1 = fig.add_subplot(11, 3, id)
  ... < the rest of your code > ...

The loop will create subplots beginning at the top left of the grid and ending at the bottom right.

floydn
  • 1,083
  • 7
  • 14