首先,我对Matplotlib或Seaborn的颜色很新.我的目的是创建一个条形图,其条形图根据自定义调色板着色.这样的东西,但我的自定义调色板(见下面,红色,橙色,绿色和蓝色的调色板):

我已经使用该LinearSegmentedColormap方法创建了自定义顺序调色板,但我无法在简单中使用它plt.barplot().当然不难,但我看不出路.我使用下面的函数创建了调色板,从这个线程获得:使用matplotlib创建自己的颜色图并绘制颜色比例
def make_colormap(seq):
"""Return a LinearSegmentedColormap
seq: a sequence of floats and RGB-tuples. The floats should be increasing
and in the interval (0,1).
"""
seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3]
cdict = {'red': [], 'green': [], 'blue': []}
for i, item in enumerate(seq):
if isinstance(item, float):
r1, g1, b1 = seq[i - 1]
r2, g2, b2 = seq[i + 1]
cdict['red'].append([item, r1, r2])
cdict['green'].append([item, g1, g2])
cdict['blue'].append([item, …Run Code Online (Sandbox Code Playgroud) 我已经在论坛上搜索过,找到了这个,但我的问题有点不同。正如您从代码和下面的图像中看到的,我创建了一个带有颜色图“virdis”的地图。如何使用相同的颜色图创建单独的条形图?我想为 4 个颜色条着色(现在用简单颜色着色),以便 y 轴上的值与颜色条上的值相对应,这可能吗?
我有这个矩阵:
matrix=[[ 0 0 0 0 17 25 29 35 36 41]
[16 22 17 10 9 21 23 27 26 22]
[ 8 19 13 16 13 5 4 11 5 4]
[ 3 11 10 8 7 1 0 0 0 0]]
在这段代码中:
fig, ax = plt.subplots(figsize=(7, 10))
im = ax.imshow(matrix, cmap='viridian')
ax.set_xticks([0,1,2,3,4,5,6,7,8,9])
ax.set_xticklabels(['0.5','1.0','1.5','2.0','2.5','3.0','3.5','4.0','4.5','5.0'])
ax.set_xlabel('Redshift')
ax.set_yticks([-0.5,0.5,1.5,2.5,3.5])
ax.set_yticklabels(['50k','10k','1k','0.1k','0'])
ax.set_ylabel('counts')
divider = make_axes_locatable(ax)
axHistx1 = divider.append_axes("top", 1.2, pad=0.2, sharex=ax)
axHistx1.xaxis.set_tick_params(labelbottom=False)
axHistx2 = divider.append_axes("top", …Run Code Online (Sandbox Code Playgroud)