Python 更正matplotlib颜色条记号
我在choropleth地图旁边放了一个颜色条。因为正在绘制的数据是离散的而不是连续的值,所以我使用了LinearSegmentedColormap(使用),我用最大计数值+1初始化了它,以显示0的颜色。然而,我现在有两个问题: 蜱虫标签不正确的间隔(除了5,或多或少)-它们应该位于它们识别的颜色的中间;i、 e.0-4应该向上移动,6-10应该向下移动Python 更正matplotlib颜色条记号,python,matplotlib,color-mapping,Python,Matplotlib,Color Mapping,我在choropleth地图旁边放了一个颜色条。因为正在绘制的数据是离散的而不是连续的值,所以我使用了LinearSegmentedColormap(使用),我用最大计数值+1初始化了它,以显示0的颜色。然而,我现在有两个问题: 蜱虫标签不正确的间隔(除了5,或多或少)-它们应该位于它们识别的颜色的中间;i、 e.0-4应该向上移动,6-10应该向下移动 如果我使用draudges=True初始化颜色栏,以便设置其分隔符的样式属性,我会得到以下结果: 我正在创建我的colormap和co
draudges=True
初始化颜色栏,以便设置其分隔符的样式
属性,我会得到以下结果:
cbmin, cbmax = min(counts), max(counts)
# this normalises the counts to a 0,1 interval
counts /= np.max(np.abs(counts), axis=0)
# density is a discrete number, so we have to use a discrete color ramp/bar
cm = cmap_discretize(plt.get_cmap('YlGnBu'), int(cbmax) + 1)
mappable = plt.cm.ScalarMappable(cmap=cm)
mappable.set_array(counts)
# set min and max values for the colour bar ticks
mappable.set_clim(cbmin, cbmax)
pc = PatchCollection(patches, match_original=True)
# impose our colour map onto the patch collection
pc.set_facecolor(cm(counts))
ax.add_collection(pc,)
cb = plt.colorbar(mappable, drawedges=True)
所以我想知道,将计数转换为0,1间隔是否是问题之一
更新:
尝试了Hooked的建议后,0值是正确的,但随后的值会逐渐设置得更高,即9是10的位置:
以下是我使用的代码:
cb = plt.colorbar(mappable)
labels = np.arange(0, int(cbmax) + 1, 1)
loc = labels + .5
cb.set_ticks(loc)
cb.set_ticklabels(labels)
仅需确认,标签
绝对具有正确的值:
In [3]: np.arange(0, int(cbmax) + 1, 1)
Out[3]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
可以手动控制放置和标签。我将从以下位置的
cmap\u discretize
生成的线性cmap开始:
您正遭受一次接一次的错误。您有10个标签,分布在11种颜色中。您可以使用
np.linspace
而不是np.arange
来更正错误。使用np.linspace
第三个参数是所需值的数量。这减少了避免一次失误所需的心理训练量:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm
import matplotlib.colors as mcolors
def colorbar_index(ncolors, cmap):
cmap = cmap_discretize(cmap, ncolors)
mappable = cm.ScalarMappable(cmap=cmap)
mappable.set_array([])
mappable.set_clim(-0.5, ncolors+0.5)
colorbar = plt.colorbar(mappable)
colorbar.set_ticks(np.linspace(0, ncolors, ncolors))
colorbar.set_ticklabels(range(ncolors))
def cmap_discretize(cmap, N):
"""Return a discrete colormap from the continuous colormap cmap.
cmap: colormap instance, eg. cm.jet.
N: number of colors.
Example
x = resize(arange(100), (5,100))
djet = cmap_discretize(cm.jet, 5)
imshow(x, cmap=djet)
"""
if type(cmap) == str:
cmap = plt.get_cmap(cmap)
colors_i = np.concatenate((np.linspace(0, 1., N), (0.,0.,0.,0.)))
colors_rgba = cmap(colors_i)
indices = np.linspace(0, 1., N+1)
cdict = {}
for ki,key in enumerate(('red','green','blue')):
cdict[key] = [ (indices[i], colors_rgba[i-1,ki], colors_rgba[i,ki])
for i in xrange(N+1) ]
# Return colormap object.
return mcolors.LinearSegmentedColormap(cmap.name + "_%d"%N, cdict, 1024)
fig, ax = plt.subplots()
A = np.random.random((10,10))*10
cmap = plt.get_cmap('YlGnBu')
ax.imshow(A, interpolation='nearest', cmap=cmap)
colorbar_index(ncolors=11, cmap=cmap)
plt.show()
嗯,这仍然不正确,但方式不同。请参阅编辑。看起来问题一定出在使用PatchCollection“手动”创建的颜色栏上。有什么理由不使用像你链接的scipy页面那样的线性离散颜色映射吗?你是说创建颜色映射,将其作为
cmap
参数传递给PatchCollection,然后pc.set_array
到计数?没有我的意思是像页面上那样使用cmap\u离散化。这就是我如何生成我的示例的,它不会受到奇怪位置的影响。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm
import matplotlib.colors as mcolors
def colorbar_index(ncolors, cmap):
cmap = cmap_discretize(cmap, ncolors)
mappable = cm.ScalarMappable(cmap=cmap)
mappable.set_array([])
mappable.set_clim(-0.5, ncolors+0.5)
colorbar = plt.colorbar(mappable)
colorbar.set_ticks(np.linspace(0, ncolors, ncolors))
colorbar.set_ticklabels(range(ncolors))
def cmap_discretize(cmap, N):
"""Return a discrete colormap from the continuous colormap cmap.
cmap: colormap instance, eg. cm.jet.
N: number of colors.
Example
x = resize(arange(100), (5,100))
djet = cmap_discretize(cm.jet, 5)
imshow(x, cmap=djet)
"""
if type(cmap) == str:
cmap = plt.get_cmap(cmap)
colors_i = np.concatenate((np.linspace(0, 1., N), (0.,0.,0.,0.)))
colors_rgba = cmap(colors_i)
indices = np.linspace(0, 1., N+1)
cdict = {}
for ki,key in enumerate(('red','green','blue')):
cdict[key] = [ (indices[i], colors_rgba[i-1,ki], colors_rgba[i,ki])
for i in xrange(N+1) ]
# Return colormap object.
return mcolors.LinearSegmentedColormap(cmap.name + "_%d"%N, cdict, 1024)
fig, ax = plt.subplots()
A = np.random.random((10,10))*10
cmap = plt.get_cmap('YlGnBu')
ax.imshow(A, interpolation='nearest', cmap=cmap)
colorbar_index(ncolors=11, cmap=cmap)
plt.show()