Python 使用matplotlib自定义类似于颜色栏的打印

Python 使用matplotlib自定义类似于颜色栏的打印,python,matplotlib,Python,Matplotlib,我希望制作一个类似于色条的绘图,如下所示: 但对于可控颜色,例如,我有以下x和y阵列: x = [0,1,2,4,7,8] y = [1,2,1,3,4,5] 然后我会有一个像上图一样的颜色条,但是当y=1时,它会变成红色,y=2:绿色,y=3:蓝色,y=4:黑色,等等 以下是我从matplotlib的图库中修改的python代码: from matplotlib import pyplot import matplotlib as mpl fig = pyplot.figure(figs

我希望制作一个类似于色条的绘图,如下所示:

但对于可控颜色,例如,我有以下x和y阵列:

x = [0,1,2,4,7,8]
y = [1,2,1,3,4,5]
然后我会有一个像上图一样的颜色条,但是当y=1时,它会变成红色,y=2:绿色,y=3:蓝色,y=4:黑色,等等

以下是我从matplotlib的图库中修改的python代码:

from matplotlib import pyplot
import matplotlib as mpl

fig = pyplot.figure(figsize=(8,1))
ax2 = fig.add_axes([0.05, 0.25, 0.9, 0.5])

cmap = mpl.cm.Accent
norm = mpl.colors.Normalize(vmin=5, vmax=10)
bounds = [1, 2, 4, 7, 8]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cmap,
                                 norm=norm,
                                 boundaries=[0]+bounds+[13],
                                 ticks=bounds, # optional
                                 spacing='proportional',
                                 orientation='horizontal')

在修改代码后,我成功地获得了您描述的东西。 在这种情况下,颜色映射是使用
ListedColormap
生成的,我为y=5添加了黄色。 请务必注意,在计算边界范数时,我使用的是包含您为y描述的值的间隔

from matplotlib import pyplot,colors
import matplotlib as mpl

fig = pyplot.figure(figsize=(8,1))
ax2 = fig.add_axes([0.05, 0.25, 0.9, 0.5])

cmap = colors.ListedColormap(['r', 'g', 'b', 'k','y'])

bounds =  [0, 1, 2, 4, 7, 8, 13]
yVals  =  [  1, 2, 1, 3, 4, 5]

cBounds = [i+0.5 for i in range(6)]

norm = mpl.colors.BoundaryNorm(cBounds, cmap.N)
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cmap,
                                 norm=norm,
                                 values=yVals,
                                 boundaries=bounds,
                                 ticks=bounds[1:-1], # optional
                                 spacing='proportional',
                                 orientation='horizontal')
--1月14日编辑(mrcl)--

或者,您可以使用
pcolormesh
绘制颜色贴图,并使用颜色条作为图例,如下面的示例所示

from pylab import *

from matplotlib import pyplot,colors
import matplotlib as mpl

fig = pyplot.figure(figsize=(8,1.5))
ax1 = fig.add_axes([0.05, 0.25, 0.82, 0.5])

cmap = colors.ListedColormap(['r', 'g', 'b', 'k','y'])

xBounds =  array([0, 1, 2, 4, 7, 8, 13])
yBounds =  array([0, 1])
Vals    =  array([[  1, 2, 1, 3, 4, 5]])

cBounds = [i+0.5 for i in arange(amax(Vals)+1)]

norm = mpl.colors.BoundaryNorm(cBounds, cmap.N)

c = ax1.pcolormesh(xBounds,yBounds,Vals,cmap=cmap,norm=norm)

ax1.set_xticks(xBounds[1:-1])
ax1.set_yticks([])
ax1.set_xlim(xBounds[0],xBounds[-1])
ax1.set_ylim(yBounds[0],yBounds[-1])


ax2 = fig.add_axes([0.9, 0.25, 0.05, 0.5])
colorbar(c,cax=ax2,ticks=arange(amax(Vals))+1)

希望能有帮助


干杯

好吧,我在尝试其他方法:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cmx
import matplotlib.colors as colors

close('all')


def ColorPlot(x,y):
    figure()
    jet = plt.get_cmap('jet') 
    cNorm  = colors.Normalize(vmin=min(y), vmax=max(y))
    scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet)

    if len(x) == len(y):
        x.insert(0,0)
        for kk in range(len(x)-1):
        colorVal = scalarMap.to_rgba(y[kk])
        plt.axvspan(x[kk], x[kk+1], facecolor=colorVal,                       
                       alpha=0.5,label=colorVal)

    plt.yticks([])
    plt.xticks(x)
    xlim([x[0],x[-1]])

    plt.show()

x = [1,3,5,6,10,12]
y = [1,3,4,1,4,3]

ColorPlot(x,y)

是否有机会添加图例?添加了一个修改后的示例,其中包括图例。