热图不显示

编程入门 行业动态 更新时间:2024-10-19 21:38:19
本文介绍了热图不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从看起来像这样的数据框中绘制一个简单的热图:

I am trying to plot a simple heatmap from a dataframe that looks like this:

row column content amount 0 x a c1 1 2 x b c3 3 4 x c c2 1 6 y a c1 1 8 y b c3 3 10 y c c2 1 12 z a c1 1 14 z b c3 3 16 z c c2 1

row和column指示单元格的位置,应根据content选择其颜色,我希望工具提示显示content和amount.

row and column indicate the position of the cell, the color of it should be chosen based on content and I want tooltips displaying the content and the amount.

我目前这样尝试(使用bokeh 1.2.0):

I currently try it like this (using bokeh 1.2.0):

import pandas as pd from bokeh.io import show from bokeh.models import CategoricalColorMapper, LinearColorMapper, BasicTicker, PrintfTickFormatter, ColorBar, ColumnDataSource from bokeh.plotting import figure from bokeh.palettes import all_palettes from bokeh.transform import transform df = pd.DataFrame({ 'row': list('xxxxxxyyyyyyzzzzzz'), 'column': list('aabbccaabbccaabbcc'), 'content': ['c1', 'c2', 'c3', 'c1', 'c2', 'c3'] * 3, 'amount': list('123212123212123212')}) df = df.drop_duplicates(subset=['row', 'column']) source = ColumnDataSource(df) rows = df['row'].unique() columns = df['column'].unique() content = df['content'].unique() colors = all_palettes['Viridis'][max(len(content), 3)] mapper = CategoricalColorMapper(palette=colors, factors=content) TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom" p = figure(title="My great heatmap", x_range=columns, y_range=rows, x_axis_location="above", plot_width=600, plot_height=400, tools=TOOLS, toolbar_location='below', tooltips=[('cell content', '@content'), ('amount', '@amount')]) p.grid.grid_line_color = None p.axis.axis_line_color = None p.axis.major_tick_line_color = None p.axis.major_label_text_font_size = "5pt" p.axis.major_label_standoff = 0 p.rect(x="row", y="column", width=1, height=1, source=source, fill_color=transform('content', mapper)) # color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="5pt", # location=(0, 0)) # p.add_layout(color_bar, 'right') show(p)

但是,有两个问题:

1)执行后,我得到一个空的热图:

1) When executed, I get an empty heatmap:

有什么想法吗?

2)当我超过color_bar = ...部分时,我收到一条错误消息:

2) When I outcomment the color_bar = ... part, I receive an error saying:

ValueError:预期为ContinuousColorMapper类型的实例,得到了 CategoricalColorMapper类型的CategoricalColorMapper(id ='3820',...)

ValueError: expected an instance of type ContinuousColorMapper, got CategoricalColorMapper(id='3820', ...) of type CategoricalColorMapper

我在做什么错了?

推荐答案

您的 x 和 y 坐标被交换,应该是:

Your x and y coordindates are swapped, should be:

p.rect(x="column", y="row", ...)

对于其他消息,它是不言自明的:从散景1.2开始,ColorBar只能配置有连续的颜色映射器(例如LinearColorMapper).您可以:

As for the other message, it is self-explanatory: As of Bokeh 1.2, ColorBar can only be configured with continuous color mappers (e.g. LinearColorMapper). You can either:

  • 使用Python代码自己计算颜色,并在source中包含一列颜色,或者
  • 重新绘制图以使用LinearColorMapper(即将content适当地映射到某个数字比例)
  • compute colors yourself in Python code, and include a column of colors in source, or
  • re-cast your plot to use a LinearColorMapper (i.e. map content appropriately to some numerical scale)

更多推荐

热图不显示

本文发布于:2023-07-18 12:51:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144950.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:热图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!