当我使用matplotlib在python中绘制颜色条时我得到了一个错误

时间:2015-01-25 10:20:56

标签: python matplotlib colorbar

我正在使用python 2.7并希望制作一个简单的颜色栏。我正在做的是:

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')

我收到此错误消息:

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

我认为问题出现在matplotlib.colorbar.ColorbarBase的方向部分。我一直在环顾四周,也许它可能是Pyhton版本的问题。而不是vhorizontal我试图将verticalvh放在方向论中,但无论如何都没有发生。

1 个答案:

答案 0 :(得分:0)

此代码适用于我:

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, norm=norm, orientation='horizontal')

结果如下:

plot