我正在尝试使用Bokeh生成矩形图。我希望绘图设置尺寸,以便当我在x或y轴上增加元素时,单元格将覆盖整个绘图。
这是我想要做的:
from bokeh.plotting import figure, show, output_file
from bokeh.palettes import Reds
data_dict={'state':['New York','New Mexico','New York','New Mexico'],'feature':['poverty','poverty','unemployment','unemployment'],'color':=[Reds[9][0],Reds[9][1],Reds[9][2],Reds[9][3]}
p = figure(title="testing",tools="hover", toolbar_location=None,x_range=data_dict['feature'], y_range=data_dict['state'])
p.rect('feature','state',source=data_dict,
color='color',width=1,height=1)
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 = "10pt"
p.axis.major_label_standoff = 0
p.xaxis.major_label_orientation = np.pi/3
show(p)```
Eliminate all the whitespace in the plot.
如果我正确理解了您的问题,您正在寻找与Bokeh示例中的热图类似的热图:
https://docs.bokeh.org/en/latest/docs/gallery/categorical.html?highlight=heatmap
我稍微弄弄了您的代码,并提出了类似的内容。该代码的运行方式与发布时不完全相同,因此我不确定您看到的内容,但是您需要为范围标签使用单独的列表(因为data_dict列表中有重复项)。
from bokeh.plotting import figure, show
from bokeh.palettes import Reds
import numpy as np
data_dict = {
'state': ['New York', 'New York', 'New Mexico', 'New Mexico'],
'feature': ['poverty', 'unemployment', 'poverty', 'unemployment'],
'colors': [Reds[9][0], Reds[9][1],
Reds[9][2], Reds[9][3]]
}
x_labels = ['poverty', 'unemployment']
y_labels = ['New York', 'New Mexico']
p = figure(title="testing",tools="hover", toolbar_location=None, x_range=x_labels, y_range=y_labels)
p.rect('feature', 'state', source=data_dict,
color='colors', width=1, height=1)
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 = "10pt"
p.axis.major_label_standoff = 0
p.xaxis.major_label_orientation = np.pi/3
show(p)