我可以为散景热图绘制颜色条吗?

     2023-02-16     15

关键词:

【中文标题】我可以为散景热图绘制颜色条吗?【英文标题】:Can I plot a colorbar for a bokeh heatmap? 【发布时间】:2015-12-13 09:57:48 【问题描述】:

散景是否有一种简单的方法来绘制热图的颜色条?

In this example 这将是一个条带,说明颜色如何与值对应。

在 matlab 中,它被称为“颜色条”,如下所示:

【问题讨论】:

【参考方案1】:

更新:现在要容易得多:请参阅

http://docs.bokeh.org/en/latest/docs/user_guide/annotations.html#color-bars


恐怕我没有一个很好的答案,这在 Bokeh 中应该更容易。但是我以前手动做过类似的事情。

因为我经常想把这些从我的情节中取出,所以我制作了一个新情节,然后将它与hplotgridplot 之类的东西组合在一起。

这里有一个例子:https://github.com/birdsarah/pycon_2015_bokeh_talk/blob/master/washmap/washmap/water_map.py#L179

在你的情况下,情节应该很简单。如果您制作了这样的数据源:

| value | color
| 1     | blue
.....
| 9     | red

然后你可以这样做:

legend = figure(tools=None)
legend.toolbar_location=None
legend.rect(x=0.5, y='value', fill_color='color', width=1, height=1, source=source)
layout = hplot(main, legend)
show(legend)

但是,这确实依赖于您知道您的值对应的颜色。您可以将调色板传递给您的热图图表调用 - 如下所示:http://docs.bokeh.org/en/latest/docs/gallery/cat_heatmap_chart.html 这样您就可以使用它来构建新的数据源。

我很确定至少有一个关于彩色地图的未解决问题。我知道我刚刚为场外传奇添加了一个。

【讨论】:

更新:这现在容易多了 - bokeh.pydata.org/en/latest/docs/user_guide/…【参考方案2】:

由于此处的其他答案似乎非常复杂,因此这里有一段易于理解的代码,可在散景热图上生成颜色条。

import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import LinearColorMapper, BasicTicker, ColorBar


data = np.random.rand(10,10)

color_mapper = LinearColorMapper(palette="Viridis256", low=0, high=1)

plot = figure(x_range=(0,1), y_range=(0,1))
plot.image(image=[data], color_mapper=color_mapper,
           dh=[1.0], dw=[1.0], x=[0], y=[0])

color_bar = ColorBar(color_mapper=color_mapper, ticker= BasicTicker(),
                     location=(0,0))

plot.add_layout(color_bar, 'right')

show(plot)

【讨论】:

【参考方案3】:

由于 0.12.3 版本 Bokeh 有 ColorBar。

这份文档对我非常有用:

http://docs.bokeh.org/en/dev/docs/user_guide/annotations.html#color-bars

【讨论】:

【参考方案4】:

为此,我和@birdsarah 做了同样的事情。作为一个额外的提示,如果你使用 rect 方法作为你的颜色图,那么在颜色栏中再次使用 rect 方法并使用相同的源。最终结果是您可以选择颜色条的各个部分,它也会在您的绘图中进行选择。

试试看:

http://simonbiggs.github.io/electronfactors

【讨论】:

【参考方案5】:

下面是一些基于birdsarah 生成颜色条响应的代码:

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

另外,如果您对模拟 matplot lib 颜色图感兴趣,请尝试使用:

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]

【讨论】:

这应该是答案 - 可能需要注意绘图大小以确保它与您的主绘图相匹配,并且我需要垂直条的宽度大于 120正确显示。另外,要使用 bk OR mpl 调色板字符串,我只使用了palette = getattr(bk.palettes, palette) if hasattr(bk.palettes, palette) else return_bokeh_colormap(palette) @user2561747,我同意。这是对我有用的答案。【参考方案6】:

这在我的愿望清单上也很重要。如果绘制的数据发生变化(例如,在 3D 数据集的一维中移动),它还需要自动调整范围。下面的代码做了一些人们可能会觉得有用的事情。诀窍是在颜色条中添加一个额外的轴,当数据发生变化时,您可以通过数据源进行控制。

import numpy

from bokeh.plotting import Figure

from bokeh.models import ColumnDataSource, Plot, LinearAxis
from bokeh.models.mappers import LinearColorMapper
from bokeh.models.ranges import Range1d
from bokeh.models.widgets import Slider
from bokeh.models.widgets.layouts import VBox

from bokeh.core.properties import Instance

from bokeh.palettes import RdYlBu11

from bokeh.io import curdoc

class Colourbar(VBox):

    plot = Instance(Plot)
    cbar = Instance(Plot)

    power = Instance(Slider)

    datasrc = Instance(ColumnDataSource)
    cbarrange = Instance(ColumnDataSource)

    cmap = Instance(LinearColorMapper)

    def __init__(self):

        self.__view_model__ = "VBox"
        self.__subtype__ = "MyApp"

        super(Colourbar,self).__init__()

        numslices = 6
        x = numpy.linspace(1,2,11)
        y = numpy.linspace(2,4,21)
        Z = numpy.ndarray([numslices,y.size,x.size])
        for i in range(numslices):
            for j in range(y.size):
                for k in range(x.size):
                    Z[i,j,k] = (y[j]*x[k])**(i+1) + y[j]*x[k]

        self.power = Slider(title = 'Power',name = 'Power',start = 1,end = numslices,step = 1,
                            value = round(numslices/2))
        self.power.on_change('value',self.inputchange)

        z = Z[self.power.value]
        self.datasrc = ColumnDataSource(data='x':x,'y':y,'z':[z],'Z':Z)

        self.cmap = LinearColorMapper(palette = RdYlBu11)

        r = Range1d(start = z.min(),end = z.max())        
        self.cbarrange = ColumnDataSource(data = 'range':[r])

        self.plot = Figure(title="Colourmap plot",x_axis_label = 'x',y_axis_label = 'y',
                           x_range = [x[0],x[-1]],y_range=[y[0],y[-1]],
                           plot_height = 500,plot_width = 500)

        dx = x[1] - x[0]
        dy = y[1] - y[0]

        self.plot.image('z',source = self.datasrc,x = x[0]-dx/2, y = y[0]-dy/2,
                        dw = [x[-1]-x[0]+dx],dh = [y[-1]-y[0]+dy],
                        color_mapper = self.cmap)

        self.generate_colorbar()

        self.children.append(self.power)
        self.children.append(self.plot)
        self.children.append(self.cbar)

    def generate_colorbar(self,cbarlength = 500,cbarwidth = 50):

        pal = RdYlBu11

        minVal = self.datasrc.data['z'][0].min()
        maxVal = self.datasrc.data['z'][0].max()
        vals = numpy.linspace(minVal,maxVal,len(pal))

        self.cbar = Figure(tools = "",x_range = [minVal,maxVal],y_range = [0,1],
                           plot_width = cbarlength,plot_height = cbarwidth)

        self.cbar.toolbar_location = None 
        self.cbar.min_border_left = 10
        self.cbar.min_border_right = 10
        self.cbar.min_border_top = 0
        self.cbar.min_border_bottom = 0
        self.cbar.xaxis.visible = None
        self.cbar.yaxis.visible = None
        self.cbar.extra_x_ranges = 'xrange':self.cbarrange.data['range'][0]
        self.cbar.add_layout(LinearAxis(x_range_name = 'xrange'),'below')

        for r in self.cbar.renderers:
            if type(r).__name__ == 'Grid':
                r.grid_line_color = None

        self.cbar.rect(x = vals,y = 0.5,color = pal,width = vals[1]-vals[0],height = 1)

    def updatez(self):

        data = self.datasrc.data
        newdata = data
        z = data['z']
        z[0] = data['Z'][self.power.value - 1]
        newdata['z'] = z
        self.datasrc.trigger('data',data,newdata)

    def updatecbar(self):

        minVal = self.datasrc.data['z'][0].min()
        maxVal = self.datasrc.data['z'][0].max()
        self.cbarrange.data['range'][0].start = minVal
        self.cbarrange.data['range'][0].end = maxVal

    def inputchange(self,attrname,old,new):

        self.updatez()
        self.updatecbar()

curdoc().add_root(Colourbar())

【讨论】:

如何将数组绘制为热图时间序列

】如何将数组绘制为热图时间序列【英文标题】:Howtoplotanarrayasaheatmaptimeseries【发布时间】:2019-10-2316:04:41【问题描述】:我正在尝试创建一个sns热图,其中每一行代表一个数组条目。我的数组看起来像:arr=[[a,b,c],[b,c,d],[e,f,g]]... 查看详情

如何为地理视图(散景)创建自定义颜色图?

】如何为地理视图(散景)创建自定义颜色图?【英文标题】:HowcanIcreateacustomizedcolormapforgeoviews(bokeh)?【发布时间】:2021-04-1704:08:09【问题描述】:我正在尝试在Geoviews中绘制一个xarray数据集,如下所示:https://geoviews.org/gallery/bo... 查看详情

珍藏史上最全热图绘制工具及操作流程(一)

...会陌生,在很多重量级科学论文中非常常见。使用heatmap可以容易展示多组分之间关系或相关性,也能展示基因表达前后差异。heatmap其实还蕴含不少分析的秘诀,这么高大上的heatmap是怎样实现的呢?热图的应用性很广,在介绍热... 查看详情

使用散点数据集在 MatPlotLib 中生成热图

...乎都已经从热图单元格值开始生成图像。有没有一种方法可以将一堆不同的x,y转换为热图(x,y频率较 查看详情

python中的热图 - 带有颜色

】python中的热图-带有颜色【英文标题】:Heatmapsinpython-withcolors【发布时间】:2018-01-2316:19:00【问题描述】:我尝试制作一个程序,用于根据参与者的点击绘制热图。有增加和减少情绪的两个身体。我想用蓝色(更强烈的蓝色=更... 查看详情

绘制时间序列热图时提高 ggplotly 的性能

】绘制时间序列热图时提高ggplotly的性能【英文标题】:Improveperformanceofggplotlywhenplottingtime-seriesheatmap【发布时间】:2020-06-3022:22:18【问题描述】:我正在使用Plotly和Shiny构建一个interactivetime-seriesheatmapinR。作为此过程的一部分,... 查看详情

r数据可视化——聚类热图pheatmap

...tmap和ComplexHeatmap假设我们有如下数据要绘制简单的热图,可以使用内置的heatmap函数更改颜色,并为列添加列样本的分类颜色条内置函数提供的样式较少,无法对某些图形属性进行设置。所以下面我们使用pheatmap包来绘制热图pheatm... 查看详情

【r】热图绘制

参考技术Atheme:是处理图美观的一个函数,可以调整横纵轴label的选择、图例的位置等。这里选择X轴标签45度。hjust和vjust调整标签的相对位置,具体见下图。简单说,hjust是水平的对齐方式,0为左,1为右,0.5居中,0-1之间可以取... 查看详情

如何将颜色条居中于 seaborn 热图的定义值?

】如何将颜色条居中于seaborn热图的定义值?【英文标题】:Howtocentreacolorbaratadefinedvalueforseabornheatmap?【发布时间】:2018-07-1308:40:00【问题描述】:我正在尝试使用seaborn绘制热图。我的值从0到5,我需要制作一个从蓝到红的色标,... 查看详情

如何在散景中绘制纬度和经度

】如何在散景中绘制纬度和经度【英文标题】:HowtoplotLatitudeandLongitudeinBokeh【发布时间】:2019-12-0208:43:21【问题描述】:我创建了一个散景图,其中X和Y轴标签显示纬度和经度坐标,但使用WMTS平铺地图,当然使用墨卡托投影。我... 查看详情

如何在 GUI pyqt 中更新热图的颜色条?

...我在更新带有heatmpan和颜色条的GUI时遇到问题,问题是我可以很好地更新热图,但在每次更新中都会添加一个新的颜色条。我的问题特别是颜色条,我试图删除颜色条,但是,如果我删除它,它就会从程序中消失,我需要它。问... 查看详情

如何使用 ggplot2 为散点图绘制多条趋势线?

】如何使用ggplot2为散点图绘制多条趋势线?【英文标题】:HowcanIdrawmultipletrendlinesforscatterplotswithggplot2?【发布时间】:2021-08-1022:03:08【问题描述】:我正在尝试绘制一个基本的差异实现图(对于那些学习过经济学的人来说,您可... 查看详情

如何用r语言绘制热图

#--enable-R-shlib需要设置,使得其他程序包括Rstudio可以使用R的动态库#--prefix指定软件安装目录,需使用绝对路径./configure--prefix=/home/ehbio/R/3.4.0--enable-R-shlib#也可以使用这个命令,共享系统的blas库,提高运输速度#./configure--prefix=/home... 查看详情

使用 Matplotlib 绘制 2D 热图

】使用Matplotlib绘制2D热图【英文标题】:Plottinga2DheatmapwithMatplotlib【发布时间】:2016-01-2119:28:05【问题描述】:使用Matplotlib,我想绘制2D热图。我的数据是一个n×nNumpy数组,每个数组的值都在0和1之间。所以对于这个数组的(i,j)元... 查看详情

热图在单细胞数据分析中的应用

...,把数据标准化到一个水平上。计算两个矩阵的相关性,可以得到两两的相关性,这时,用热图的颜色来表示相关性可以看出哪些配对相关性较高。这是一张典型的seurat做的热图,可以清楚地看出不同分群有着不同的表达模式。... 查看详情

散景折线图未绘制完整的熊猫数据框

】散景折线图未绘制完整的熊猫数据框【英文标题】:BokehLinechartnotplottingcompletepandasdataframe【发布时间】:2016-03-2323:15:12【问题描述】:我不确定这是否是个问题,因为图表当前正在Bokeh中更新,但我无法再在Jupyter笔记本中使用... 查看详情

运用r语言pheatmap包绘制热图进阶部分

...基础篇中介绍过使用z-score中心化和标准化的数据;具体可以参考上一期内容这是注释颜色,好像不是特别好看,根据自己美感慢慢调节运用R语言pheatmap包绘制热图进阶部分的内容就到这结束了。 查看详情

嵌入散景图时遇到问题

...。在示例中,我看到命令“create_html_sn-p”,它应该返回可以插入到模板中的html的sn-p:frombokeh 查看详情