Last active
November 29, 2023 08:11
-
-
Save skannan-maf/15f7a160ddfd42c3397fe2b286d0b1eb to your computer and use it in GitHub Desktop.
This file contains 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 | |
import plotly.graph_objects as go | |
def plotly_display_number(n, what_is_n, color='Black'): | |
''' | |
Use this display big numbers in plotly | |
Automatically recognizes Thousands/million/Billion and displays with a suitable prefix | |
''' | |
if n > 1e9: | |
n /= 1e9 | |
suffix = 'B' | |
elif n > 1e6: | |
n /= 1e6 | |
suffix = 'M' | |
elif n > 1e3: | |
n /= 1e3 | |
suffix = 'K' | |
else: | |
suffix = '' | |
fig = go.Figure() | |
fig.add_trace( | |
go.Indicator( | |
mode='number', | |
value=n, | |
align='center', | |
name=what_is_n, | |
number = {'valueformat' : '.02f', 'suffix' : suffix, 'font': {'color': color} } | |
) | |
) | |
fig.update_layout( | |
title=what_is_n, | |
title_font=dict( | |
family="Calibri", | |
size=36, | |
color=color | |
), | |
) | |
# | |
# These 2 calls are superflous and does nothing | |
# It was an attempt to draw borders for the chart which did not work for indicator charts | |
# Because indicator charts do not have X and Y axes.. Duh! | |
# Dont tell anyone that I was this stupid! | |
# | |
fig.update_xaxes(showline=True, | |
linewidth=1, | |
linecolor='black', | |
mirror=True) | |
fig.update_yaxes(showline=True, | |
linewidth=1, | |
linecolor='black', | |
mirror=True) | |
return fig |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: The last 2 calls to "update_axes" and "update_yaxes" were intended to draw borderlines for the chart. However, since Indicator charts do not have axes, they don't take any effect. These 2 calls can simply be removed.