title: How to use and plot only a part of a colorbar in matplotlib?
body: <p>I have multiple curves that differ in one parameter and which I want to plot in one figure. To distinguish them, I want to use one of matplotlib's colorbars. To do so I produce a list of colors depending on said parameter. Additionally, I want to add a colorbar to explain the colors that are used. I can easily do all of that. The problem is now, that I want to use only a part of the available colormap, as it gets too bright and thus barely visible above some threshold. But when I now choose the colors only in a subrange, I did not find a way to adjust the range of the displayed colorbar.</p>
  
  <p>Here is a (nearly) minimal example of what I want to achieve:</p>
  
  <pre><code>import numpy as np
  import matplotlib as mpl
  import matplotlib.pyplot as plt
  import matplotlib.gridspec as gridspec
  
  gs = gridspec.GridSpec(2, 1,
                         height_ratios=[1, 4]
                         )
  ax = [plt.subplot(g) for g in gs]
  
  parameterToColorBy = np.linspace(5, 10, 6, dtype=float)
  
  maxColor = 0.85
  colors = [plt.get_cmap("inferno")(i)
            for i in np.linspace(0, maxColor, parameterToColorBy.shape[0])]
  
  norm = mpl.colors.Normalize(parameterToColorBy[0],
                              parameterToColorBy[0]+
                              (parameterToColorBy[-1]-parameterToColorBy[0])/
                              maxColor)
  cb = mpl.colorbar.ColorbarBase(ax[0],
                                 cmap="inferno",
                                 norm=norm,
                                 ticks=parameterToColorBy,
                                 orientation='horizontal')
  ax[0].xaxis.set_ticks_position('top')
  
  for p, c in zip(parameterToColorBy, colors):
      ax[1].plot(np.arange(2)/p, c=c)
  
  plt.show()
  </code></pre>
  
  <p>The result looks at follows:</p>
  
  <p><a href="https://i.stack.imgur.com/Y9Acf.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Y9Acf.png" alt="starting point"></a></p>
  
  <p>I now want the colorbar to stop at 10. But if I just adjust the <code>xlim</code> of the subplot by adding the line <code>ax[0].set_xlim(0, maxColor)</code>, the colored part is adjusted correctly, but the surrounding box is messed up:</p>
  
  <p><a href="https://i.stack.imgur.com/aZQ63.png" rel="noreferrer"><img src="https://i.stack.imgur.com/aZQ63.png" alt="Result of set_xlim"></a></p>
  
  <p>Alternatively, I found a function for colorbars <code>set_clim</code>. But this only changes the normalization and does not seem to work as I want. Adding <code>cb.set_clim(parameterToColorBy[0], parameterToColorBy[-1])</code> results in an changed colors but unchanged axis:</p>
  
  <p><a href="https://i.stack.imgur.com/jpa1m.png" rel="noreferrer"><img src="https://i.stack.imgur.com/jpa1m.png" alt="Result of set_xlim"></a></p>
  
  <p>What I seem to need is either an appropriate way to adjust the limits of the displayed colorbar, or a way to create an own colorbar as a subset of an available colorbar. Is there any way to achieve one of these things?</p>