I am trying to plot a bubble chart in bokeh and adding the column labels as an annotation there using Labelset. Some of These Labels are getting overlapped to each other and creating visual clutter in the graph. Please guide me. Here is the code looks like -
import numpy as np
import pandas as pd
from bokeh.io import output_notebook, output_file
output_notebook()
from bokeh.io import show
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, LabelSet
from bokeh.palettes import Spectral10
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"
labels = LabelSet(x='Rating', y='Sales', text='Plans', source = source,
x_offset= -50, y_offset=10, text_font_size = '7pt')
p.add_layout(labels)
show(p)
Here is the visualization generated -
similar to what i am trying to accomplish - How to create a dynamic labelset on Bokeh that will avoid text overlapping?
i have already tried manual experimentation with x_offset, y_offset and label rotation to solve this issue, but none of them is working for me.
You can set x_offset
and y_offset
for each label as you did for x
and y
in the LabelSet
as long as you are using as ColumnDataSource
as source
. This gives you some freedom to move the in labels in different directions. There is also text_align
which can be useful.
import numpy as np
import pandas as pd
from bokeh.plotting import output_notebook, show, figure
from bokeh.models import ColumnDataSource, LabelSet
from bokeh.palettes import Spectral10
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],
'x_offset': [0, -50, 70],
'y_offset': [45, -5, -5],
'text_align': ['center', 'right', 'left']
}
df = pd.DataFrame.from_dict(df_dict)
source = ColumnDataSource(data = df)
# figure
p = figure(
height = 300,
width = 500,
x_range = (10, 20),
y_range = (1300, 1350)
)
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',
x_offset= 'x_offset',
y_offset='y_offset',
text_font_size = '7pt',
text_align='text_align',
source = source,
)
p.add_layout(labels)
show(p)
It is possible that the labels will be over and under each other if you use the zoom tools.
I hope this is a useable the solution to your problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.