title: Matplotlib box plots - plotting grouped values with a for loop
body: <p>I'm trying to plot <strong>box plots grouped by unique column values using a for loop</strong> without knowing the number of unique values in that column.</p>
  
  <p>You can see sample code below (without a for loop) and the desired output.</p>
  
  <pre><code>import pandas as pd
  from numpy import random
  import matplotlib.pyplot as plt
  
  #------------------------------ Preparing the data frame -------------------------------#
  
  df = pd.DataFrame(data = random.randn(5,4), index = ['A','B','C','D','E'],
  columns = ['W','X','Y','Z'])
  
  df['W'] = ['10/01/2018 12:00:00','10/03/2018 13:00:00',
             '10/03/2018 12:30:00','10/04/2018 12:05:00',
             '10/08/2018 12:00:15']
  
  df['W']=pd.to_datetime(df['W'])
  
  df['Entity'] = ['C201','C201','C201','C202','C202']
  print(df)
  
  #-----------------------------------Plotting the data ----------------------------------#
  
  fig, ax = plt.subplots()
  
  data_to_plot = [df['Y'][df['Entity']=='C201'], df['Y'][df['Entity']=='C202']]
  
  # Create the boxplot
  bp = ax.boxplot(data_to_plot, showfliers=False)
  ax.set_xticklabels(['C201' ,'C202'])      
  plt.show()
  </code></pre>
  
  <p><strong>Desired output:</strong>
  <a href="https://i.stack.imgur.com/79cwc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/79cwc.png" alt="enter image description here"></a></p>