在BokeH中绘制热图显示空图

时间:2019-09-27 13:43:57

标签: python pandas dataframe bokeh spectrogram

我试图在散景中绘制热图(频谱图),但是当热图显示为空时。.

这是仅包含一些示例数据的代码,但是可以扩展为通过json获取大型数据集。

from math import pi
import pandas as pd

from bokeh.io import show
from bokeh.models import LinearColorMapper, BasicTicker, PrintfTickFormatter, ColorBar
from bokeh.plotting import figure

# initialise data of lists. 
data = {'epoch':[63745131000000, 63745131000000, 63745131100000,63745131100000], 'energy':[1.06811, 1.22078, 1.59495, 1.82245],'value':[3981.9308143034305, 2868.5202872178324, 1330.887696894385, 745.6847248644897]} 

# Creates pandas DataFrame. 
df = pd.DataFrame(data) 

# print the data 
print(df)

# this is the colormap from the original NYTimes plot
colors = ['#00007F', '#0000ff', '#007FFF', '#00ffff', '#7FFF7F', '#ffff00', '#FF7F00', '#ff0000', '#7F0000']
mapper = LinearColorMapper(palette=colors, low=df.value.min(), high=df.value.max())

TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"

epochs = list(df.epoch.drop_duplicates())
print(epochs)
energies = list(df.energy.drop_duplicates())
print(energies)


p = figure(title="My Plot", 
           x_axis_location="below", 
           tools=TOOLS, toolbar_location='below',
           tooltips=[('epoch', '@epoch'), ('energy', '@energy'), ('value', '@value')])

p.xaxis.ticker = epochs
p.yaxis.ticker = energies

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.xaxis.major_label_orientation = pi / 3

p.rect(x="epoch", y="energy", width=1, height=1,
       source=df,
       fill_color={'field': 'value', 'transform': mapper},
       line_color=None)

color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="5pt",
                     ticker=BasicTicker(desired_num_ticks=len(colors)),
                     label_standoff=6, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')

show(p)

输出的帧看起来正确:

            epoch   energy        value
0  63745131000000  1.06811  3981.930814
1  63745131000000  1.22078  2868.520287
2  63745131100000  1.59495  1330.887697
3  63745131100000  1.82245   745.684725

,x和y的范围也看起来不错:

[63745131000000, 63745131100000]
[1.06811, 1.22078, 1.59495, 1.82245]

但是出现的图像没有绘制点: enter image description here

我应该提到,如果我只是简单地将第二个纪元更改为例如一个后一个纪元

'epoch':[63745131000000, 63745131000000, 63745131000001,63745131000001]

然后图表似乎正确显示:

enter image description here

非常感谢您的帮助。 谢谢

1 个答案:

答案 0 :(得分:2)

之所以没有显示任何信息,是因为在背景虚化的边缘,显然您不认为您的数据部分值得保留。

您应该更改的是映射器中的限制:

mapper = LinearColorMapper(palette=colors, low=df.value.min()-1, high=df.value.max()+1) #  this will make sure your data is inside the mapping

在图中,您的宽度也定义为1。当您的历元相差一百万时,在绘制此图形时,您仍然几乎看不到任何内容,所以更改

p.rect(x="epoch", y="energy", width=100000, height=1,  #  here width is set to an adequate level. 
       source=df,
       fill_color={'field': 'value', 'transform': mapper},
       line_color=None)