title: Matplotlib subplot using nested for loop
body: <p>first time poster so apologize in advance for mistakes.</p>
  <p>I'm trying to create a matplotlib subplots for a nested for loop.</p>
  <p>My data is advertisement data keyword, clicks, bid, day_of_week columns</p>
  <p>I want to create 8 plots for each keyword, with x = bids and y = clicks.
  I would like the first plot to have bids and clicks for that keyword, and 7 others to have bids and clicks for each day of the week (one plot per day of the week)</p>
  <p>Right now, I'm able to get the first plot onto the subplot grid (with the formatting looking weird) and the other 7 plots are appearing on their own instead of showing in the subplots. I created a fake df and included the code I'm using below.</p>
  <p>What I would like to do:</p>
  <ol>
  <li>the formatting of the first plot to be fixed</li>
  <li>I would like the plot for Monday to be in the second subplot spot</li>
  </ol>
  <p>I would appreciate any and all tips. Thank you!</p>
  <p><a href="https://i.stack.imgur.com/jMLWm.png" rel="nofollow noreferrer">image of my data</a></p>
  <p><a href="https://i.stack.imgur.com/ukshV.png" rel="nofollow noreferrer">My current output</a></p>
  <p>My code:</p>
  <pre><code>#creating a unique list of days for the 
  day_list = df['day_of_week'].unique()
  
  #create a plot for each keyword  by day of week
  
  def keyword_plots(x, y):
      #create dict so that each day has a spot on the figure subplot
      ndict = {'Monday': 2, 
           'Tuesday': 3, 
           'Wednesday': 4,
           'Thursday': 5,
           'Friday': 6,
           'Saturday' : 7,
           'Sunday' :8}
  
      #dealing with colors
      color_labels = y['day_of_week'].unique()
      rgb_values = sns.color_palette(&quot;husl&quot;, 7)
      color_map = dict(zip(color_labels, rgb_values))
  
      #loop through each keyword and add it to the plot (first spot)
      for each in x:
      
          #create subset for each keyword
          subset = y[y[&quot;keyword&quot;]==each][[&quot;clicks&quot;,&quot;bid&quot;, &quot;day_of_week&quot;]]
      
          #create one figure per keyword with 8 spaces
          fig, axes = plt.subplots(2, 4, figsize=(20, 8))
          fig.tight_layout()
      
          #add this keyword plot to the first subplot space
          ax=fig.add_subplot(4,4,1)
          plt.scatter(subset['bid'], subset['clicks'], c = subset['day_of_week'].map(color_map), alpha=0.5)
      
          #labels
          plt.title(each)
          plt.xlabel(&quot;bid&quot;)
          plt.ylabel(&quot;clicks&quot;)
      
          #trendlines
          z = np.polyfit(subset[&quot;bid&quot;], subset['clicks'], 1)
          p = np.poly1d(z)
          pylab.plot(subset['bid'],p(subset['bid']),&quot;r--&quot;)
          plt.show()
      
          #loop through each day of the week and create one plot per week per keyword (7 total for Mon-Sun)
          for i in day_list:
          
              #subset the data
              day_sub = subset[subset[&quot;day_of_week&quot;]==i][[&quot;clicks&quot;,&quot;bid&quot;, &quot;day_of_week&quot;]]
          
              #add the plot to the space corresponding to day of week (in ndict)
              ax=fig.add_subplot(4, 4, ndict[i])
          
              #create plot
              plt.scatter(day_sub['bid'], day_sub['clicks'], c = day_sub['day_of_week'].map(color_map), alpha=0.5)
          
              #plot lables
              plt.title(each + &quot; &quot; + i)
              plt.xlabel(&quot;bid&quot;)
              plt.ylabel(&quot;clicks&quot;)
          
              #trendline
              z = np.polyfit(day_sub[&quot;bid&quot;], day_sub['clicks'], 1)
              p = np.poly1d(z)
              pylab.plot(day_sub['bid'],p(day_sub['bid']),&quot;r--&quot;)
              plt.show()
  
  keyword_plots(list_of_keywords_I_want, keywords_df)
  </code></pre>