gpt4 book ai didi

Python Bokeh 变空热图

转载 作者:太空宇宙 更新时间:2023-11-03 15:15:18 24 4
gpt4 key购买 nike

我正在尝试制作一个热图,就像 this one 一样使用 Bokeh 。这是我的数据框Data,我试图从中制作热图

    Day Code    Total
0 1 6001 44
1 1 6002 40
2 1 6006 8
3 1 6008 2
4 1 6010 38
5 1 6011 1
6 1 6014 19
7 1 6018 1
8 1 6019 1
9 1 6023 10
10 1 6028 4
11 2 6001 17
12 2 6010 2
13 2 6014 4
14 2 6020 1
15 2 6028 2
16 3 6001 48
17 3 6002 24
18 3 6003 1
19 3 6005 1
20 3 6006 2
21 3 6008 18
22 3 6010 75
23 3 6011 1
24 3 6014 72
25 3 6023 34
26 3 6028 1
27 3 6038 3
28 4 6001 19
29 4 6002 105
30 5 6001 52
...

这是我的代码:

 from bokeh.io import output_file
from bokeh.io import show
from bokeh.models import (
ColumnDataSource,
HoverTool,
LinearColorMapper
)
from bokeh.plotting import figure

output_file('SHM_Test.html', title='SHM', mode='inline')

source = ColumnDataSource(Data)
TOOLS = "hover,save"

# Creating the Figure
SHM = figure(title="HeatMap",
x_range=[str(i) for i in range(1,32)],
y_range=[str(i) for i in range(6043,6000,-1)],
x_axis_location="above", plot_width=400, plot_height=970,
tools=TOOLS, toolbar_location='right')

# Figure Styling
SHM.grid.grid_line_color = None
SHM.axis.axis_line_color = None
SHM.axis.major_tick_line_color = None
SHM.axis.major_label_text_font_size = "5pt"
SHM.axis.major_label_standoff = 0
SHM.toolbar.logo = None
SHM.title.text_alpha = 0.3

# Color Mapping
CM = LinearColorMapper(palette='RdPu9', low=Data.Total.min(), high=Data.Total.max())

SHM.rect(x='Day', y="Code", width=1, height=1,source=source,
fill_color={'field': 'Total','transform': CM})

show(SHM)

当我执行代码时,我没有收到任何错误,但只是得到一个空框架,如下图所示。

HeatMap

我一直在努力寻找我的错误在哪里,为什么我会得到这个? ¿我的错误在哪里?

最佳答案

您的代码的问题是您为 x 和 y 轴范围设置的数据类型与 ColumnDataSource 的数据类型不同。您将 x_rangey_range 设置为字符串列表,但从查看 csv 格式的数据来看,它将被视为整数。

就您而言,您需要确保您的“日期”和“代码”列位于字符串格式。

使用 pandas 包可以轻松完成此操作

Data['Day'] = Data['Day'].astype('str')
Data['Code'] = Date['Code'].astype('str')

关于Python Bokeh 变空热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43932168/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com