Can I plot a colorbar for a bokeh heatmap?

前端 未结 6 1554
再見小時候
再見小時候 2020-12-30 06:26

Does bokeh have a simple way to plot the colorbar for a heatmap?

In this example it would be a strip illustrating how colors correspond to values.

6条回答
  •  一个人的身影
    2020-12-30 07:04

    Here is some code loosely based on birdsarah's response for generating a colorbar:

    def generate_colorbar(palette, low=0, high=15, plot_height = 100, plot_width = 500, orientation = 'h'):
    
        y = np.linspace(low,high,len(palette))
        dy = y[1]-y[0]
        if orientation.lower()=='v':
            fig = bp.figure(tools="", x_range = [0, 1], y_range = [low, high], plot_width = plot_width, plot_height=plot_height)
            fig.toolbar_location=None
            fig.xaxis.visible = None
            fig.rect(x=0.5, y=y, color=palette, width=1, height = dy)
        elif orientation.lower()=='h':
            fig = bp.figure(tools="", y_range = [0, 1], x_range = [low, high],plot_width = plot_width, plot_height=plot_height)
            fig.toolbar_location=None
            fig.yaxis.visible = None
            fig.rect(x=y, y=0.5, color=palette, width=dy, height = 1)
        return fig
    

    Also, if you are interested in emulating matplot lib colormaps, try using this:

    import matplotlib as mpl
    def return_bokeh_colormap(name):
        cm = mpl.cm.get_cmap(name)
        colormap = [rgb_to_hex(tuple((np.array(cm(x))*255).astype(np.int))) for x in range(0,cm.N)]
        return colormap
    def rgb_to_hex(rgb):
        return '#%02x%02x%02x' % rgb[0:3]
    

提交回复
热议问题