我正在寻找一种解决方案来创建具有统一刻度标签的颜色条(沿颜色条等距),即使边界是非线性的。目前,由于刻度线是根据边界值按比例分布的,因此颜色条的顶部被拉伸得相当厉害,而底部被压缩得很厉害,以至于无法看到哪些颜色对应于哪个值。我想保持相同的颜色/值组合,但带有使颜色栏清晰可见的刻度标签间距。
我使用当前代码获得的颜色条:
这是我使用的代码:
import matplotlib as mpl
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.figure import Figure
# data
bounds = [0.1, 0.25, 0.5, 1, 2.5, 5, 7.5, 10, 15, 20, 25, 50, 100]
style_color = [[0, 0, 127],
[0, 0, 197],
[0, 21, 254],
[0, 126, 254],
[0, 231, 254],
[68, 253, 186],
[153, 254, 101],
[238, 254, 16],
[254, 187, 0],
[254, 101, 0],
[254, 16, 0],
[197, 0, 0],
[127, 0, 0],
[127, 0, 0]]
# transform color rgb value to 0-1 range
color_arr = []
for color in style_color:
rgb = [float(value)/255 for value in color]
color_arr.append(rgb)
# normalize bound values
norm = mpl.colors.Normalize(vmin=min(bounds), vmax=max(bounds))
normed_vals = norm(bounds)
# create a colormap
cmap = LinearSegmentedColormap.from_list(
'my_palette',
list(zip(normed_vals, color_arr[:-1])),
N=256
)
cmap.set_over([color for color in color_arr[-1]])
cmap.set_under([color for color in color_arr[0]])
# create a figure
fig = Figure(figsize=(2, 5))
canvas = FigureCanvasAgg(fig)
ax = fig.add_subplot(121)
# create the colorbar
cb = mpl.colorbar.ColorbarBase(ax,
cmap=cmap,
norm=norm,
extend='max',
ticks=bounds)
fig.savefig('non-linear_colorbar')
拉风的咖菲猫
相关分类