title: Colour coding a figure representing discrete data in Python
body: <p>I am trying to visualise how the thickness of a beam varies across its' length.</p>
  <pre><code>import matplotlib.pyplot as plt
  import matplotlib as mpl
  import numpy as np
  
  fig, (ax) = plt.subplots(1)
  fig.tight_layout(pad=1)
  fig.set_size_inches(8, 1)
  
  ax.set_xlabel('Length along MP', fontsize=10)
  plt.xticks(fontsize=10)
  plt.xlim([-8, 80+5])
  
  ax.set_ylabel('Width across MP', fontsize=10)
  plt.yticks(fontsize=10)
  
  ax.set_xlabel('Length along MP', fontsize=10)
  plt.xticks(fontsize=10)
  plt.xlim([-8, 80])
  
  ax.set_ylabel('Width across MP', fontsize=10)
  plt.yticks(fontsize=10)
  
  
  MPcan_Dos = (8.0,8.0,8.0, 8.0, 8.0, 8.0, 8.0, 8.4, 8.8, 8.8, 8.8, 8.8, 8.8, 8.8, 8.8, 8.8, 8.8, 8.8, 8.8, 8.8,8.8, 8.8, 8.8)
  
  bnd = ( 0.   ,  0.195,  0.318,  2.408,  5.508,  8.6  , 11.7  ,
         15.5  , 19.3  , 23.1  , 26.5  , 29.198, 31.798, 35.317,
         38.917, 42.517, 46.117, 48.717, 52.117, 55.117, 57.717,
         60.417, 63.017) # the x-coordinates of the different sections in the beam
  
  # plotting beam outline
  x_outline = (-0.   ,  0.195,  0.318,  2.408,  5.508,  8.6  , 11.7  , 15.5  ,
         19.3  , 23.1  , 26.5  , 29.198, 31.798, 35.317, 38.917, 42.517,
         46.117, 48.717, 52.117, 55.117, 57.717, 60.417, 63.017, 66.62) # x outline of the beam for plotting - the x coordinates of the beam outline
  y_outline = (4. , 4. , 4. , 4. , 4. , 4. , 4. , 4.2, 4.4, 4.4, 4.4, 4.4, 4.4,
         4.4, 4.4, 4.4, 4.4, 4.4, 4.4, 4.4, 4.4, 4.4, 4.4, 4.4) # y outline of the beam for plotting - the y coordinates of the beam outline
  
  thii = (0.4  , 0.099, 0.099, 0.086, 0.087, 0.086, 0.077, 0.076,
         0.076, 0.077, 0.078, 0.081, 0.082, 0.084, 0.083, 0.081, 0.076,
         0.077, 0.068, 0.068, 0.075, 0.095) # thicknesses of each of the sections of the beam (indicated by 'bnd'). for eg: for the section between 0-0.195, the thickness would be 0.4 and for the section between 0.195-0.318, the thickness would be 0.099 etc. 
  
  ax.plot(np.array(x_outline), np.array(y_outline), color=&quot;black&quot;, linewidth=1)
  ax.plot(np.array(x_outline), -np.array(y_outline), color=&quot;black&quot;, linewidth=1)
  
  # plotting the sections in the beam 
  
  for bd, Do in zip(np.array(bnd), np.array(MPcan_Dos)):
      ax.plot([bd, bd], [Do/2, -Do/2], color=&quot;black&quot;, linestyle=&quot;dashed&quot;, linewidth=1)
  
  
  </code></pre>
  <p>This code produces the following figure:
  <a href="https://i.stack.imgur.com/xYg7D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xYg7D.png" alt="enter image description here" /></a></p>
  <p>Essentially, the 'sections' in the beam have to be filled by colors, representing the thickness of the section and a key has to be provided with labels including the thickness in the corresponding section. Predefining a list of colours and creating a filled plot works, but the plots would have to be generated multiple times, where the number of sections could change (hence, the predefined colour list will not work). Randomly generating a colour list also did not work, as it repeated the same colours in some runs.</p>
  <p>Is there a way to plot this figure accordingly?</p>
  <p>thanks :)</p>