custom_tooltip#
使用元素周期表数据集绘制元素周期表。此示例演示了在创建绘图时使用自定义 CSS 工具提示。图表显示了元素周期表元素的原子质量与密度的相关性。
详情
- 示例数据:
- Bokeh API:
- 更多信息:
- 关键词:
绘图,工具提示,自定义
import pandas as pd
from bokeh.plotting import figure, show
from bokeh.sampledata.periodic_table import elements
elements = elements.copy()
elements = elements[elements.group != "-"]
elements.sort_values('metal', inplace=True)
colormap = {
"alkali metal" : "#a6cee3",
"alkaline earth metal" : "#1f78b4",
"halogen" : "#fdbf6f",
"metal" : "#b2df8a",
"metalloid" : "#33a02c",
"noble gas" : "#bbbb88",
"nonmetal" : "#baa2a6",
"transition metal" : "#e08e79",
}
data=dict(
atomic_number=elements["atomic number"],
sym=elements["symbol"],
name=elements["name"],
atomic_mass = pd.to_numeric(elements['atomic mass'], errors="coerce"),
density=elements['density'],
metal=[x.title() for x in elements["metal"]],
type_color=[colormap[x] for x in elements["metal"]],
)
TOOLTIPS = """
<div style="opacity: 0.8; padding: 0.5em; background-color: @type_color;">
<div style="font-size: 12px;">@atomic_number</div>
<div style="font-size: 24px;"><strong>@sym</strong></div>
<div style="font-size: 8px;"><strong>@name</strong></div>
<div style="font-size: 8px;">@atomic_mass{0.00}</div>
</div>
"""
p = figure(width=900, height=450, tooltips=TOOLTIPS, title='Densities by Atomic Mass')
p.background_fill_color = "#fafafa"
p.scatter('atomic_mass', 'density', size=12, source=data, color='type_color',
line_color="black", legend_field='metal', alpha=0.9)
p.legend.glyph_width = 30
p.legend.glyph_height = 30
p.xaxis.axis_label= 'Atomic Mass'
p.yaxis.axis_label= 'Density'
p.xgrid.grid_line_color = None
p.toolbar_location = None
legend = p.legend[0]
p.add_layout(legend, 'right')
legend.border_line_color = None
show(p)