Skip to content

Instantly share code, notes, and snippets.

@janbenetka
Last active August 20, 2021 05:16
Show Gist options
  • Save janbenetka/db6a3583dbc6e920ad37a8597dad30a9 to your computer and use it in GitHub Desktop.
Save janbenetka/db6a3583dbc6e920ad37a8597dad30a9 to your computer and use it in GitHub Desktop.
[Bar chart in Plotly] #plotly #python
import plotly.graph_objects as go
countries = increase_per_country.country
fig = go.Figure()
fig.add_trace(go.Bar(
x=countries,
y=increase_per_country.identifier_count_new,
name='New data',
marker_color='#FF8000'
))
fig.add_trace(go.Bar(
x=countries,
y=increase_per_country.identifier_count_old,
name='New data',
marker_color='#1A4D60'
))
# Here we modify the tickangle of the xaxis, resulting in rotated labels.
fig.update_layout(barmode='group', xaxis_tickangle=-45, title='Number of dwells in 2020 by country - new vs old dataset')
fig.show()
# bar charts: https://plotly.com/python/bar-charts/
# As function
def bar_chart(x_data, y_data, title='', x_axis_title='', y_axis_title='', yaxis_range=[], show_legend=False, output_path=''):
fig = go.Figure()
fig.add_trace(go.Bar(
x = x_data,
y = y_data,
))
fig.update_layout(
barmode='group',
xaxis_tickangle=-45,
title = title,
xaxis_title = x_axis_title,
yaxis_title = y_axis_title,
template = 'unacast',
width=1000,
height=500,
showlegend=show_legend,
)
if(yaxis_range != []):
fig.update_layout(
yaxis_range = yaxis_range
)
fig.update_yaxes(visible=True, showticklabels=True)
if(len(output_path) > 0):
fig.write_image(output_path, scale=2)
fig.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment