proplot icon indicating copy to clipboard operation
proplot copied to clipboard

Stop overriding user-input colormap lookup table size

Open syrte opened this issue 3 years ago • 5 comments

Description

It seems proplot does not work with the conventional discrete color in matplotlib (which I used heavily...).

I noticed the document mentioned discrete several places, but it seems not what I'm looking for. I can specify discrete=True with scatter, but it gives 10 levels instead of 5 levels as I specified in the code.

Any hint for me? Many thanks!

Steps to reproduce

import numpy as np
from matplotlib import pyplot as plt
import proplot as pplt

fig, ax = pplt.subplot(journal='nat1', refaspect=1)
x, y, c = np.random.rand(3, 100)
cl = ax.scatter(x, y, c=c, cmap=plt.get_cmap('viridis', 5))
ax.colorbar(cl)

Expected behavior: [What you expected to happen] image

Actual behavior: [What actually happened] image

Equivalent steps in matplotlib

Please try to make sure this bug is related to a proplot-specific feature. If you're not sure, try to replicate it with the native matplotlib API. Matplotlib bugs belong on the matplotlib github page.

import numpy as np
from matplotlib import pyplot as plt
import proplot as pplt

ax = plt.subplot()
x, y, c = np.random.rand(3, 100)
cl = ax.scatter(x, y, c=c, cmap=plt.get_cmap('viridis', 5))
plt.colorbar(cl)

Proplot version

Paste the results of import matplotlib; print(matplotlib.__version__); import proplot; print(proplot.version)here.

syrte avatar Feb 19 '22 18:02 syrte

You can add discrete=True to ax.scatter():

image

zxdawn avatar Feb 19 '22 18:02 zxdawn

OK. I got it with

import numpy as np
from matplotlib import pyplot as plt
import proplot as pplt

fig, ax = pplt.subplot(journal='nat1', refaspect=1)
x, y, c = np.random.rand(3, 100)
cl = ax.scatter(x, y, c=c, cmap='viridis', norm=pplt.DiscreteNorm(np.linspace(0, 1, 6)))
ax.colorbar(cl)

image

syrte avatar Feb 19 '22 18:02 syrte

@syrte To reproduce the matplotlib tick level or set your own level, you can use

Update: Oh, levels=5 is enough ;)

ax.scatter(......, levels=5, discrete=True)

image

zxdawn avatar Feb 19 '22 18:02 zxdawn

Thanks a lot for your quick reply @zxdawn Your solution is nice and much simpler than mine! I didn't notice the usage of levels. Thanks.

syrte avatar Feb 19 '22 18:02 syrte

Thanks @syrte for the report and @zxdawn for the help!

That's interesting that proplot overrides the lookup table size of 5 in your example (note a "lookup table" is an N x 3 array of RGB colors generated from the linear transitions specified by the LinearSegmentedColormap). Indeed there are two ways of "discretizing" continuous colormaps: decreasing the lookup table size (as you have done, or could be done with rc['image.lut'] = 5), or pairing the dense lookup table with a matplotlib BoundaryNorm (or proplot DiscreteNorm) that selects a smaller subset of RGB colors in that table.

Proplot elects to use DiscreteNorm as the universal method for discretizing continuous colormaps, and leaves the "lookup table size" as something internal and mostly irrelevant for users, because DiscreteNorm is more flexible (e.g. for getting out-of-bounds colors right). However, we should not be silently overwriting a colormap's lookup table size when users want to discretize in this way, as I know this is a popular method.

Maybe this could be fixed by disabling the application of DiscreteNorm when a colormap's lookup table size is below a certain threshold (similar to the rc['cmap.listedthresh'] setting).

lukelbd avatar Feb 19 '22 19:02 lukelbd