Ive been using the following function from dkane.net/2020/better-horizontal-bar-charts-with-plotly to generate a horizontal bar graph with labels above each bar. This is needed because the labels in my case can get very long.
However, this takes many more seconds to render and the page almost completely freezes (heap from 20mb to 300mb) when I have more than about 80 rows.
Is there a better way?
from plotly.subplots import make_subplots
def horizontal_bar_labels(categories):
subplots = make_subplots(
rows=len(categories),
cols=1,
subplot_titles=[x["name"] for x in categories],
shared_xaxes=True,
print_grid=False,
vertical_spacing=(0.45 / len(categories)),
)
# add bars for the categories
for k, x in enumerate(categories):
subplots.add_trace(dict(
type='bar',
orientation='h',
y=[x["name"]],
x=[x["value"]],
text=["{:,.0f}".format(x["value"])],
hoverinfo='text',
textposition='auto',
marker=dict(
color="#7030a0",
),
), k+1, 1)
# update the layout
subplots['layout'].update(
showlegend=False,
)
for x in subplots["layout"]['annotations']:
x['x'] = 0
x['xanchor'] = 'left'
x['align'] = 'left'
x['font'] = dict(
size=12,
)
height_calc = 45 * len(categories)
height_calc = max([height_calc, 350])
return subplots