I was plotting a colorbar using a custom colormap and levels using ax.colorbar. However, sometimes I observed that the plot would miss out one level (and its color). After trying with different values I found that when the length of the level is too small, this level would be gone. I tried using values, norm=DiscreteNorm and using DiscreteNorm and DiscreteColormap, but all these methods failed to make the correct plot.
In the following I will demonstrate this with a colorbar consisting of colors 'blue', 'orange' and 'green', set at levels $5$, $60$ and $130$ respectively. In the three plots presented, one of the colors is missing.
As for why I am plotting a colorbar instead of using ScalerMappable, I am plotting a categorical map using geopandas. The ScalarMappable generated cannot be used to add a colorbar, so I have to build it from colors and values.
I do not fully use matplotlib to reproduce. Instead, I can simply use colors.BoundaryNorm and colors.ListedColormap to build the ScalarMappable to obtain the expected hebavior.
import proplot as pplt
from matplotlib import cm, colors
fig, ax = pplt.subplots()
ax.colorbar(
cm.ScalarMappable(
colors.BoundaryNorm([0, 5, 60, 130], ncolors=3),
colors.ListedColormap(['blue', 'orange', 'green']),
),
spacing='proportional',
ticks=10
)
Extra Info
If I reduce the maximum value from 130 to 120, then Method 2 gives the expected plot.
Description
I was plotting a colorbar using a custom colormap and levels using
ax.colorbar
. However, sometimes I observed that the plot would miss out one level (and its color). After trying with different values I found that when the length of the level is too small, this level would be gone. I tried usingvalues
,norm=DiscreteNorm
and usingDiscreteNorm
andDiscreteColormap
, but all these methods failed to make the correct plot.In the following I will demonstrate this with a colorbar consisting of colors 'blue', 'orange' and 'green', set at levels $5$, $60$ and $130$ respectively. In the three plots presented, one of the colors is missing.
As for why I am plotting a colorbar instead of using
ScalerMappable
, I am plotting a categorical map usinggeopandas
. TheScalarMappable
generated cannot be used to add a colorbar, so I have to build it from colors and values.Steps to reproduce
Method 1: Using
values
Method 2: Using
norm = pplt.DiscreteNorm
Method 3: Using
pplt.DiscreteNorm
andpplt.DiscreteColormap
incm.ScalarMappable
Expected behavior:
Actual behavior:
Method 1
Method 2
Method 3
Equivalent steps in matplotlib
I do not fully use
matplotlib
to reproduce. Instead, I can simply usecolors.BoundaryNorm
andcolors.ListedColormap
to build theScalarMappable
to obtain the expected hebavior.Extra Info
If I reduce the maximum value from 130 to 120, then Method 2 gives the expected plot.
Method 2 (reduced maximum)
Proplot version