title: Unable to plot 4 histograms of iris dataset features using matplotlib
body: <p>using the iris dataset</p>
  
  <pre><code>import matplotlib.pyplot as plt
  %matplotlib inline
  import matplotlib
  from sklearn import datasets
  iris= datasets.load_iris()
  
  x_index = 3
  colors = ['blue', 'red', 'green']
  
  for label, color in zip(range(len(iris.target_names)), colors):
      plt.hist(iris.data[iris.target==label, x_index], 
                       label=iris.target_names[label],
                                          color=color)
  
  plt.xlabel(iris.feature_names[x_index])
  plt.legend(loc='upper right')
  plt.show()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Qs1jn.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>This code is plotting only one histogram with sepal length (image attached) as the x-axis.
  To plot other features of iris dataset in a similar manner, I have to change the x_index to 1,2 and 3 (manually) and run this bit of code again.</p>
  
  <p>To plot all four histograms simultaneously, I tried the following code:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  %matplotlib inline
  import matplotlib
  from sklearn import datasets
  iris= datasets.load_iris()
  
  fig, axes = plt.subplots(nrows= 2, ncols=2)
  colors= ['blue', 'red', 'green', 'black']
  
  x_index= 0
  
  for ax in axes.flat:
      for label, color in zip(range(len(iris.target_names)), colors):
          ax= plt.hist(iris.data[iris.target==label, x_index], label=             
                              iris.target_names[label], color=color)
          plt.xlabel(iris.feature_names[x_index])  
          plt.legend(loc='upper right')
          x_index+=1
  
  plt.show()
  </code></pre>
  
  <p>This code gives me the following error:</p>
  
  <p>IndexError: index 4 is out of bounds for axis 1 with size 4</p>
  
  <p>Any advice?</p>