Bokeh气泡图Labelset标注重叠问题解决咨询
Bokeh气泡图标注重叠问题解决方案
问题说明
使用Bokeh绘制气泡图时,通过LabelSet添加Plans列标注出现重叠,手动调整x_offset、y_offset及旋转参数无法解决。
可行解决方案
方案1:使用HoverTool替代静态标注
静态标注在数据点密集时极易重叠,改用鼠标悬停显示标注的方式,既不影响视觉整洁,又能完整查看信息:
import numpy as np import pandas as pd from bokeh.io import output_notebook, show from bokeh.plotting import figure from bokeh.models import ColumnDataSource, HoverTool output_notebook() df_dict = { 'Plans': ['ABC ValueCharge', 'Bizbazar 200CarbonNatural', 'EqualiserClient SuperSaverCombo'], 'Rating': [17, 15, 14], 'Sales': [1330.443, 1312.271, 1312.168], 'Spends': [72.49, 47.7, 13.2417514] } df = pd.DataFrame.from_dict(df_dict) p = figure(height=600, width=800, x_range=(0, 25), y_range=(800, 2000)) source = ColumnDataSource(data=df) p.circle(x='Rating', y='Sales', size='Spends', source=source, alpha=0.8) p.xaxis.axis_label = "Rating" p.yaxis.axis_label = "Sales" # 添加HoverTool,鼠标悬停显示计划名称 hover = HoverTool(tooltips=[("Plan", "@Plans"), ("Rating", "@Rating"), ("Sales", "@Sales")]) p.add_tools(hover) show(p)
方案2:基于数据计算动态偏移量
根据每个数据点的Spends(气泡大小)和相邻点的位置,给标签设置差异化的偏移值,避免重叠:
import numpy as np import pandas as pd from bokeh.io import output_notebook, show from bokeh.plotting import figure from bokeh.models import ColumnDataSource, LabelSet output_notebook() df_dict = { 'Plans': ['ABC ValueCharge', 'Bizbazar 200CarbonNatural', 'EqualiserClient SuperSaverCombo'], 'Rating': [17, 15, 14], 'Sales': [1330.443, 1312.271, 1312.168], 'Spends': [72.49, 47.7, 13.2417514] } df = pd.DataFrame.from_dict(df_dict) # 基于Spends计算y偏移,大气泡偏移更大,且给相邻点设置反向偏移 df['y_offset'] = df['Spends'].apply(lambda x: x/2) # 给后两个Sales接近的点设置不同偏移方向 df.loc[1, 'y_offset'] = 30 df.loc[2, 'y_offset'] = -30 p = figure(height=600, width=800, x_range=(0, 25), y_range=(800, 2000)) source = ColumnDataSource(data=df) p.circle(x='Rating', y='Sales', size='Spends', source=source, alpha=0.8) p.xaxis.axis_label = "Rating" p.yaxis.axis_label = "Sales" # 使用动态计算的偏移量 labels = LabelSet(x='Rating', y='Sales', text='Plans', source=source, x_offset=-50, y_offset='y_offset', text_font_size='7pt') p.add_layout(labels) show(p)
方案3:结合第三方库自动避障
使用adjustText库先计算标签的最优位置,再传入Bokeh的LabelSet,实现自动避障:
import numpy as np import pandas as pd import matplotlib.pyplot as plt from bokeh.io import output_notebook, show from bokeh.plotting import figure from bokeh.models import ColumnDataSource, LabelSet from adjustText import adjust_text output_notebook() df_dict = { 'Plans': ['ABC ValueCharge', 'Bizbazar 200CarbonNatural', 'EqualiserClient SuperSaverCombo'], 'Rating': [17, 15, 14], 'Sales': [1330.443, 1312.271, 1312.168], 'Spends': [72.49, 47.7, 13.2417514] } df = pd.DataFrame.from_dict(df_dict) # 用adjustText计算标签位置 x = df['Rating'].values y = df['Sales'].values texts = [plt.text(xi, yi, txt) for xi, yi, txt in zip(x, y, df['Plans'])] adjust_text(texts, x=x, y=y, arrowprops=dict(arrowstyle="-", color='gray', lw=0.5)) # 提取计算后的偏移量 df['x_offset'] = [t.get_position()[0] - xi for t, xi in zip(texts, x)] df['y_offset'] = [t.get_position()[1] - yi for t, yi in zip(texts, y)] p = figure(height=600, width=800, x_range=(0, 25), y_range=(800, 2000)) source = ColumnDataSource(data=df) p.circle(x='Rating', y='Sales', size='Spends', source=source, alpha=0.8) p.xaxis.axis_label = "Rating" p.yaxis.axis_label = "Sales" labels = LabelSet(x='Rating', y='Sales', text='Plans', source=source, x_offset='x_offset', y_offset='y_offset', text_font_size='7pt') p.add_layout(labels) show(p)
注意:方案3需要先安装
adjustText库,执行pip install adjustText即可。
内容的提问来源于stack exchange,提问作者Abhay
相关产品推荐
相关产品推荐

