urs*_*rei 19 python matplotlib color-mapping
我在一个等值线图旁边放了一个彩条.因为绘制的数据是离散的而不是连续的值,所以我使用了一个LinearSegmentedColormap(使用scipy cookbook中的配方),我用我的最大计数值+1初始化,以显示0的颜色.但是,我现在有两个问题:

刻度标签间距不正确(除了5或更多或更少) - 它们应位于它们识别的颜色的中间; 即0 - 4应向上移动,6 - 10应向下移动.
如果我初始化colorbar drawedges=True,以便我可以设置其dividers属性,我得到这个:

我正在创建我的色彩映射和颜色条:
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)
Run Code Online (Sandbox Code Playgroud)
所以我想知道我将计数转换为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)
Run Code Online (Sandbox Code Playgroud)
只是为了确认,labels绝对有正确的价值观:
In [3]: np.arange(0, int(cbmax) + 1, 1)
Out[3]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Run Code Online (Sandbox Code Playgroud)
unu*_*tbu 19
你正遭受一个错误的错误.你有11种颜色的10个滴答标签.您可以通过使用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()
Run Code Online (Sandbox Code Playgroud)

Hoo*_*ked 12
您可以手动控制放置和标签.我会从产生线性CMAP开始cmap_discretize对你的链接页面:
import numpy as np
import pylab as plt
# The number of divisions of the cmap we have
k = 10
# Random test data
A = np.random.random((10,10))*k
c = cmap_discretize('jet', k)
# First show without
plt.subplot(121)
plt.imshow(A,interpolation='nearest',cmap=c)
plt.colorbar()
# Now label properly
plt.subplot(122)
plt.imshow(A,interpolation='nearest',cmap=c)
cb = plt.colorbar()
labels = np.arange(0,k,1)
loc = labels + .5
cb.set_ticks(loc)
cb.set_ticklabels(labels)
plt.show()
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
28735 次 |
| 最近记录: |