pythonmatplotlibcolorbar

When I'm plotting a colorbar in python using matplotlib I'm getting an error


I am using python 2.7 and want to make a simple colorbar. What I'm doing is this:

import matplotlib.pylab as plt
import matplotlib as mpl

redgreendict = {'red':[(0.0, 1.0, 1.0), (0.5, 1.0, 1.0) ,(1.0, 0.0, 0.0)],
                'green':[(0.0, 0.0, 0.0), (0.5, 1.0, 1.0), (1.0, 1.0, 1.0)],
                'blue':[(0.0, 0.2, 0.0), (0.5, 1.0, 1.0), (1.0, 0.2, 0.0)]}

fig = plt.figure()
ax1 = fig.add_axes([0.05, 0.08, 0.9, 0.08])
cmap = mpl.colors.LinearSegmentedColormap('redgreen', redgreendict, 1000)

cb1 = mpl.colorbar.ColorbarBase(ax1, cmap, orientation='vhorizontal')

And I get this error message:

File "figutils_used_one.py", line 1208, in make_europe_graph
cb1   = matplotlib.colorbar.ColorbarBase(ax1, cmap, orientation='vhorizontal')

File "C:\Anaconda\lib\site-packages\matplotlib\colorbar.py", line 320, in __init__
self.config_axis()

File "C:\Anaconda\lib\site-packages\matplotlib\colorbar.py", line 360, in config_axis
ax.xaxis.set_label_position(self.ticklocation)

File "C:\Anaconda\lib\site-packages\matplotlib\axis.py", line 1728, in set_label_position
assert position == 'top' or position == 'bottom'

AssertionError

I suppose that the problem lays in the orientation part of the matplotlib.colorbar.ColorbarBase. I have been looking around and maybe it could be a version-of-Pyhton problem. Instead of of vhorizontal i tried to put vertical, v, h in the argument for orientation but anyway nothing happens.


Solution

  • This code works for me:

    import matplotlib.pylab as plt
    import matplotlib as mpl
    
    min_val = -1
    max_val = 1
    
    redgreendict = {'red':[(0.0, 1.0, 1.0), (0.5, 1.0, 1.0) ,(1.0, 0.0, 0.0)],
                    'green':[(0.0, 0.0, 0.0), (0.5, 1.0, 1.0), (1.0, 1.0, 1.0)],
                    'blue':[(0.0, 0.2, 0.0), (0.5, 1.0, 1.0), (1.0, 0.2, 0.0)]}
    
    cmap = mpl.colors.LinearSegmentedColormap('redgreen', redgreendict, 1000)
    norm = mpl.colors.Normalize(min_val, max_val)
    
    fig = plt.figure()
    ax1 = fig.add_axes([0.05, 0.08, 0.9, 0.08])
    
    cb1   = mpl.colorbar.ColorbarBase(ax1, cmap=cmap, norm=norm, orientation='horizontal')
    

    This is the result:

    plot