Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何订购我的数据来制作博克的热图?_Python_Pandas_Heatmap_Bokeh - Fatal编程技术网

Python 如何订购我的数据来制作博克的热图?

Python 如何订购我的数据来制作博克的热图?,python,pandas,heatmap,bokeh,Python,Pandas,Heatmap,Bokeh,我有这样一个数据帧(mD): Pr Dt Cd Cn Rn GT 2017-01-12 60 1 'ZP0' GT 2017-01-12 60 6 'ZP5' GT 2017-01-12 61 0 'ZP0' GT 2017-01-12 61 7 'ZP6' GT 2017-01-12 65

我有这样一个数据帧(
mD
):

Pr      Dt           Cd      Cn      Rn
GT      2017-01-12   60      1       'ZP0'
GT      2017-01-12   60      6       'ZP5'
GT      2017-01-12   61      0       'ZP0'
GT      2017-01-12   61      7       'ZP6'
GT      2017-01-12   65      7       'ZP4'
GT      2017-01-10   63      1       'ZP4'
GT      2017-01-10   65      2       'ZP4'
GT      2017-01-10   68      3       'ZP2'
GT      2017-01-09   62      8       'ZP1'
GT      2017-01-12   62      1       'ZP1'
GT      2017-01-11   62      2       'ZP0'
GT      2017-01-11   60      2       'ZP0'
GT      2017-01-10   66      4       'ZP5'
GT      2017-01-10   60      1       'ZP6'
GT      2017-01-09   68      1       'ZP2'
GT      2017-01-09   65      1       'ZP0'
GT      2017-01-09   62      1       'ZP3'
GT      2017-01-09   62      1       'ZP3'
BW      2017-01-11   61      0       'ZP0'
BW      2017-01-10   61      1       'ZP1'
BW      2017-01-10   61      6       'ZP0'
BW      2017-01-11   61      0       'ZP5'
BW      2017-01-11   62      1       'ZP5'
BW      2017-01-12   62      6       'ZP7'
BW      2017-01-11   60      5       'ZP0'
BW      2017-01-09   66      4       'ZP2'
我想在博克制作一张热图,图中,
x=Cd
(从60到70),
y=Dt
(有日期时间格式)和
values=Cn
。换句话说,我想要每个产品(
Pr
)的热图,其中绘制了代码(
Cd
)与日期(
Dt
)的对比,颜色必须表示每个日期和代码的计数总和(
Cn

我的第一次尝试是这样的(当然是在导入库之后):

但是,这将导致一个空白画布。我尝试使用
.groupby
pd.pivot\u table
pd.crosstab
对数据进行排序,但到目前为止我失败了。我是Bokeh的新手,所以,有人有什么建议吗?

编辑:Bokeh图表回购不再维护,因此如果您想使用Bokeh的最新版本,您应该使用HoloView,或者编写自己的代码来制作热图。

尝试下面的代码,似乎可以工作。
注意:如果您想为每种颜色单独绘制热图,只需过滤df并创建单独的绘图即可。 或者创建一个小部件,让您选择希望可视化的数据集

import pandas as pd
from bokeh.charts import HeatMap, bins
from bokeh.plotting import figure, output_file, show,ColumnDataSource
from bokeh.models import DatetimeTickFormatter

Dt = ['2017-01-12','2017-01-12','2017-01-12','2017-01-12','2017-01-12',
      '2017-01-10','2017-01-10','2017-01-10','2017-01-09','2017-01-12',
      '2017-01-11','2017-01-11','2017-01-10','2017-01-10','2017-01-09',
      '2017-01-09','2017-01-09','2017-01-09','2017-01-11','2017-01-10',
      '2017-01-10','2017-01-11','2017-01-11','2017-01-12','2017-01-11',
      '2017-01-09']
Pr = ['GT','GT','GT','GT','GT','GT','GT','GT','GT','GT','GT','GT','GT','GT',
      'GT','GT','GT','GT','BW','BW','BW','BW','BW','BW', 'BW', 'BW']
Cd = [60,60,61,61,65,63,65,68,62,62,62,60,66,60,68,65,62,62,61,61,61,61,62,62,60,66]
Cn = [1,6,0,7,7,1,2,3,8,1,2,2,4,1,1,1,1,1,0,1,6,0,1,6,5,4]

df = pd.DataFrame({'Pr':Pr,'Dt':Dt,'Cd':Cd,'Cn':Cn})
datadict = df.to_dict(orient='list')
source = ColumnDataSource(datadict)

h1 = HeatMap(source.data, x=bins('Cd'), y='Dt', values='Cn')
show(h1)

import pandas as pd
from bokeh.charts import HeatMap, bins
from bokeh.plotting import figure, output_file, show,ColumnDataSource
from bokeh.models import DatetimeTickFormatter

Dt = ['2017-01-12','2017-01-12','2017-01-12','2017-01-12','2017-01-12',
      '2017-01-10','2017-01-10','2017-01-10','2017-01-09','2017-01-12',
      '2017-01-11','2017-01-11','2017-01-10','2017-01-10','2017-01-09',
      '2017-01-09','2017-01-09','2017-01-09','2017-01-11','2017-01-10',
      '2017-01-10','2017-01-11','2017-01-11','2017-01-12','2017-01-11',
      '2017-01-09']
Pr = ['GT','GT','GT','GT','GT','GT','GT','GT','GT','GT','GT','GT','GT','GT',
      'GT','GT','GT','GT','BW','BW','BW','BW','BW','BW', 'BW', 'BW']
Cd = [60,60,61,61,65,63,65,68,62,62,62,60,66,60,68,65,62,62,61,61,61,61,62,62,60,66]
Cn = [1,6,0,7,7,1,2,3,8,1,2,2,4,1,1,1,1,1,0,1,6,0,1,6,5,4]

df = pd.DataFrame({'Pr':Pr,'Dt':Dt,'Cd':Cd,'Cn':Cn})
datadict = df.to_dict(orient='list')
source = ColumnDataSource(datadict)

h1 = HeatMap(source.data, x=bins('Cd'), y='Dt', values='Cn')
show(h1)