Skip to content

Instantly share code, notes, and snippets.

@skannan-maf
Last active November 29, 2023 08:11
Show Gist options
  • Save skannan-maf/15f7a160ddfd42c3397fe2b286d0b1eb to your computer and use it in GitHub Desktop.
Save skannan-maf/15f7a160ddfd42c3397fe2b286d0b1eb to your computer and use it in GitHub Desktop.
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
@skannan-maf
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment