Create Horizontal Barchart
Tags: #plotly #chart #horizontalbar #dataviz #snippet
Author: Florent Ravenel
Learn more on the Plotly doc : https://plotly.com/python/horizontal-bar-charts/

Input

Import library

1
import plotly.graph_objects as go
Copied!

Model

Create the plot

1
fig = go.Figure(
2
go.Bar(
3
x=[20, 14, 23],
4
y=['giraffes', 'orangutans', 'monkeys'],
5
orientation='h',
6
text=[20, 14, 23],
7
)
8
)
9
fig.update_layout(
10
title="Horizontal barchart" ,
11
plot_bgcolor="#ffffff",
12
width=1200,
13
height=800,
14
margin_pad=10,
15
xaxis_showticklabels=False,
16
bargap=0.1, # gap between bars of adjacent location coordinates.
17
bargroupgap=0.2 # gap between bars of the same location coordinate.
18
)
19
config = {'displayModeBar': False}
20
fig.show(config=config)
Copied!

Output

Export in PNG and HTML

1
fig.write_image(output_image, width=1200)
2
fig.write_html(output_html)
Copied!

Generate shareable assets

1
link_image = naas.asset.add(output_image)
2
link_html = naas.asset.add(output_html, {"inline":True})
3
4
#-> Uncomment the line below to remove your assets
5
# naas.asset.delete(output_image)
6
# naas.asset.delete(output_html)
Copied!
Copy link
Edit on GitHub