question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DiscreteNorm does not support set_under/set_over colormap extremes

See original GitHub issue

[Description of the bug or feature.] Error when using boudary normalization for colormap.

Steps to reproduce

A “Minimal, Complete and Verifiable Example” will make it much easier for maintainers to help you.

import matplotlib.pyplot as plt
import proplot as pplt
import numpy as np
import cmaps
import matplotlib.colors as colors
import matplotlib.ticker as ticker


state = np.random.RandomState(51423)
data = (state.rand(20, 20))*50
data[5:15,5:15] = 0.002
data[10:20,10:20] = 0.6
print(data.max(),data.min())

levels = [0.1,0.5,1.,2.,3.,4.,5.,6.,8.,10.,20.,40]
norm = colors.BoundaryNorm(boundaries=np.array(levels), ncolors=len(levels)-1)

# figure with proplot
fig, ax = pplt.subplots()
m0 = ax.contourf(data, levels=levels, extend='both', cmap=cmaps.cmorph,
colorbar="r",colorbar_kw={'ticks':levels})
ax.format(suptitle='plot with proplot')
fig.savefig('color_proplot.png')

# figure with matplotlib
fig, ax = plt.subplots()
m1 = ax.contourf(data, levels=levels, norm=norm, extend='both',cmap=cmaps.cmorph[1:])
m1.cmap.set_under(cmaps.cmorph.colors[0])
cb = plt.colorbar(m1, ax=ax, ticks=levels, extendfrac='auto', 
                     aspect=18, shrink=.95)

cb.ax.minorticks_off()

ax.set_title("plot with matplotlib")
fig.savefig('color_matplotlib.png')

# figure with proplot and boudary normalization
fig, ax = pplt.subplots()
m0 = ax.contourf(data, levels=levels, norm=norm, extend='both', cmap=cmaps.cmorph[1:],
colorbar="r",colorbar_kw={'ticks':levels})
m0.cmap.set_under(cmaps.cmorph.colors[0])

ax.format(suptitle='plot with proplot1')
fig.savefig('color_proplot1.png')

Expected behavior: [What you expected to happen] color_matplotlib

Actual behavior: [What actually happened]

When using default segmented normalizaton in proplot:

color_proplot

And when using boudary normailiztion of matplotlib:

color_proplot1

Equivalent steps in matplotlib

See the code above.

Proplot version

0.6.1 Paste the result of import proplot; print(proplot.version) here.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
lacstormcommented, Jun 22, 2020

@lukelbd Hi, luke. It’s ok now. Thanks for developing such an effective package!

0reactions
lukelbdcommented, Jun 22, 2020

Sorry I couldn’t get to this sooner, but I finally figured it out (see https://github.com/lukelbd/proplot/commit/f23ff70d54ffa668212e65c1d80ede3a310f5518). This was actually two separate bugs:

  • plot.Colormap() automatically translates native matplotlib colormaps to proplot’s special colormap subclasses plot.colors.LinearSegmentedColormap and plot.colors.ListedColormap. However when doing this, proplot failed to copy the “extreme” color values.
  • plot.DiscreteNorm() failed to account for the possibility that there is a discrete transition between the minimum/maximum colors “within” the colormap range (colormap coordinates 0 and 1) and the out of bounds colors (coordianates 0 - eps and 1 + eps). Now it takes this into account.

Here’s your example on master (can be installed with pip install git+https://github.com/lukelbd/proplot):

import matplotlib.pyplot as plt
import proplot as pplt
import numpy as np
import matplotlib.colors as colors
import matplotlib.ticker as ticker


state = np.random.RandomState(51423)
data = (state.rand(20, 20))*50
data[5:15,5:15] = 0.002
data[10:20,10:20] = 0.6
#print(data.max(),data.min())

# define colormap
cmorph = [[255.,255,255],
   [0,225,225],
   [0,165,185],
   [0,185, 0],
   [0 ,255 , 45],
 [255, 255 ,  0],
 [225 ,205 ,  0],
 [160, 120 ,  0],
 [160 , 40 ,  0],
 [185 ,  0 ,  0],
 [255  , 0 ,155],
 [185  , 0 ,255],
 [125 ,  0, 255]]

cmorph = np.array(cmorph) / 255.0
cmap0 = colors.ListedColormap(cmorph[1:-1])
cmap0.set_under(cmorph[0])
cmap0.set_over(cmorph[-1])
levels = [0.1,0.5,1.,2.,3.,4.,5.,6.,8.,10.,20.,40]

# Figure with proplot
fig, ax = pplt.subplots()
m0 = ax.contourf(
  data, levels=levels, extend='both', cmap=cmap0,
  colorbar='b', colorbar_kw={'ticks':levels}
)

ax.format(suptitle='plot with proplot')

tmp

Read more comments on GitHub >

github_iconTop Results From Across the Web

DiscreteNorm does not support set_under/set_over colormap ...
[Description of the bug or feature.] Error when using boudary normalization for colormap. Steps to reproduce A "Minimal, Complete and ...
Read more >
DiscreteNorm — ProPlot documentation - Read the Docs
This normalizer makes sure that levels always span the full range of colors in the colormap, whether extend is set to 'min' ,...
Read more >
python - how to extract a subset of a colormap as a new ...
I'm interested to plot my data using the in-between colors. I think ppl use it quite often but I was searching over internet...
Read more >
Colormap Normalization — Matplotlib 3.6.2 documentation
Matplotlib does this mapping in two steps, with a normalization from the input data to [0, 1] occurring first, and then mapping onto...
Read more >
Control Colormap Limits - MATLAB & Simulink - MathWorks
Set the limits for scaling your data to a colormap. ... to values that are outside the range of C , so those...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found