How to use and display only part of colorbar in matplotlib?

I have several curves that differ in one parameter and which I want to build in one drawing. To distinguish them, I want to use one of the matplotlib color panels. To do this, I create a list of colors depending on the specified parameter. In addition, I want to add a color bar to explain the colors used. I can easily do all this. The problem is that I want to use only part of the available color map, as it becomes too bright and, thus, barely noticeable above a certain threshold. But when I now select colors only in the sub-range, I did not find a way to adjust the range of the displayed color panel.

Here is a (almost) minimal example of what I want to achieve:

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()

:

starting point

, 10. xlim , ax[0].set_xlim(0, maxColor), , :

Result set_xlim

, colorbars set_clim. , , , . cb.set_clim(parameterToColorBy[0], parameterToColorBy[-1]) , :

Result set_xlim

, , - , . ?

+4
1

truncate_colormap, . matplotlib.colors.LinearSegmentedColormap .

, Normalise maxColor, colormap colors colorbar.

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.colors as mcolors

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)

def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=-1):
    if n == -1:
        n = cmap.N
    new_cmap = mcolors.LinearSegmentedColormap.from_list(
         'trunc({name},{a:.2f},{b:.2f})'.format(name=cmap.name, a=minval, b=maxval),
         cmap(np.linspace(minval, maxval, n)))
    return new_cmap

minColor = 0.00
maxColor = 0.85
inferno_t = truncate_colormap(plt.get_cmap("inferno"), minColor, maxColor)

colors = [inferno_t(i)
          for i in np.linspace(0, 1, parameterToColorBy.shape[0])]

norm = mpl.colors.Normalize(parameterToColorBy[0],
                            parameterToColorBy[-1])

cb = mpl.colorbar.ColorbarBase(ax[0],
                               cmap=inferno_t,
                               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()

enter image description here

+3

Source: https://habr.com/ru/post/1662616/


All Articles