Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

259
Visualizações
How to modify intervals on matplotlib's qualitative color maps?
import matplotlib.pyplot as plt
import numpy as np

I have a 2D numpy array:

mock=\
np.array([[0.1,0.2,0.3],
          [0.2,0.3,0.4],
          [0.3,0.4,0.5],
          [0.4,0.5,0.6],
          [0.5,0.6,0.7],
          [0.6,0.7,0.8],
          [0.7,0.8,0.9]])

I plot these values using the tab20c colormap, from the so-called "Qualitative" colormaps:

plt.figure(figsize=(6,6))
im = plt.imshow(mock,cmap = "tab20c", vmin=0, vmax=1)
plt.colorbar(im)

enter image description here

How would I go about this if I wanted all the values below a certain value, let's say 0.25 having the same color as 0.25 has, replacing not only the colors in the plot itself, but also in the colorbar? The expected output would be then: all 3 squares in the top left corner to be red, bottom 5 rectangles in the colormap are also all red.

By doing this, I am essentially modifying the interval for the color red in the barchart: instead of showing values in the interval between 0.2 and 0.25 as red, the new version will show everything in the interval between 0 and 0.25 red.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

What about using numpy.clip to set the data below 0.25 to 0.25?

im = plt.imshow(np.clip(mock, 0.25, np.inf), cmap = "tab20c", vmin=0, vmax=1)

This seems to be the most straightforward solution to me as it doesn't require to mess with the plotting parameters.

output:

clipped data

over 4 years ago · Santiago Trujillo Relatório

0

You can adapt an existing colourmap:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap

tab20c = cm.get_cmap('tab20c', 256)
newcolors = tab20c(np.linspace(0, 1, 256))
red = np.array([1, 0, 0, 1])
newcolors[:int(256/4), :] = red
newcmp = ListedColormap(newcolors)


mock=\
np.array([[0.1,0.2,0.3],
          [0.2,0.3,0.4],
          [0.3,0.4,0.5],
          [0.4,0.5,0.6],
          [0.5,0.6,0.7],
          [0.6,0.7,0.8],
          [0.7,0.8,0.9]])


plt.figure(figsize=(6,6))
im = plt.imshow(mock,cmap = newcmp, vmin=0.0, vmax=1)
plt.colorbar(im)

enter image description here

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda