Matthias Michler wrote:
[...]
First of all I think my previously described error in the over-color is due to a bug in the set up of the colors.ListedColormap during the initialisation of ContourSet if 'self.colors' is not None. For the number of entries in the map 'N' the number of layers is passed "N=len(self.layers)", which is not useful if self.extend is 'both'. Let's say I want 3 regions and provide 3 colors. If extend is 'both' the number of layers is the #{colored regions between min and max} + 2 = 5. For N=5 in the initialisation of the ListedColormap the list of colors is extended to have length 'N=5' (by repetition). Therefore an additional layer (more precisely 2) occurs in the colormap and although the over_color is set to something different the (repeted) first element of 'colors' is used to color values above the highest level.

I propose the following case differentiation: if self.extend == "both":
    N = len(self.layers) - 2
elif self.extend in ('max', 'min'):
    N = len(self.layers) - 1
else:
    N = len(self.layers)

Maybe N = len(self.levels) - 1 is always a good solution.

Is this a bug and does my proposal resolve it without destroying something else?



Matthias,

I think I have fixed the problem. I also changed the second plot in contourf_demo to include the extended colorbar. Attached is a modification of one of your illustrative scripts with some unnecessary kwargs stripped out. In particular, when contourf makes a ListedColormap, you don't need or want to specify the norm--it uses the appropriate one, which is NoNorm. Also, you probably don't need to specify the view limits when making the subplot (contourf will autoscale with tight boundaries), and you don't need to pass the extend kwarg to colormap--it gets it from the ContourSet object.

Eric
""" make a contour plot with the levels specified,
    and with the colormap generated automatically from a list of colors;
    add specification of colors for values above and below the
    specified levels.
"""
import numpy as np
import matplotlib.pyplot as plt

# using some repeatable data
X, Y = np.meshgrid(np.arange(51), np.arange(61))
Z = 1.75 +  (np.sin(X/5.0) + np.sin(Y/5.0))

ax = plt.subplot(111)

# levels or boundaries of your colormap (len(bounds) = len(colorlist) + 1 !)
bounds = [1.0, 1.5, 2.0, 2.5]

# plot contour lines with filling in between:
conset = plt.contourf(X, Y, Z, bounds,
                           colors=['r', 'g', 'b'],
                           extend='both')
conset.cmap.set_over('cyan')                # specify color for larger
conset.cmap.set_under('yellow')             # and smaller values


# add a colorbar to the figure
cbar = plt.colorbar(conset, fraction=0.10, aspect=3)

plt.show()

------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to