Last active
August 20, 2021 05:16
-
-
Save janbenetka/db6a3583dbc6e920ad37a8597dad30a9 to your computer and use it in GitHub Desktop.
[Bar chart in Plotly] #plotly #python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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