Skip to content

contourf with custom colormap #16629

@Skillmon

Description

@Skillmon

Bug report

Using a custom colormap created with LinearSegmentedColormap.from_list, contourf doesn't seem to use the correct colours for every value.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap


def color_grid(xx, classes):
    x = xx.ravel()
    Z = np.zeros(x.shape)
    dx = (x.max() - x.min())/classes
    for i in range(classes):
        Z[(i * dx <= x) & ((i + 1) * dx >= x)] = i
    return Z.reshape(xx.shape)


colors = ['b',
          'r',
          'm',
          'c',
          'g',
          'xkcd:orange',
          'xkcd:peach',
          'xkcd:bright pink',
          'xkcd:crimson']


xx, yy = np.meshgrid(np.arange(0, 10, 0.02), np.arange(0, 10, 0.02))
fig, sub = plt.subplots(len(colors)-1, 1)

for i, ax in enumerate(sub.flatten()):
    cm = LinearSegmentedColormap.from_list('foo', colors[0:i+2], i+2)
    Z = color_grid(xx, i+2)
    ax.contourf(xx, yy, Z, cmap=cm)
plt.show()

Actual outcome

mwe

Expected outcome

I expected the colours to be correctly picked, meaning an increasing number of different colours in each subplot from top to bottom with each bar in a row roughly the same size (since that's what Z should look like).

Matplotlib version

  • Operating system: Arch Linux
  • Matplotlib version: 3.1.3
  • Matplotlib backend (print(matplotlib.get_backend())): PGF with PDF output, GTK3Agg
  • Python version: 3.8.1
  • Other libraries: numpy

Installed from Arch Linux repositories.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy