Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: Hide `null` from hoverlabels

Tags:

I have a list called my_customdata which has some nan values. When I plot a sunburst chart and pass my list to customdata, it displays the values as desired. But for the nan values, it instead shows 0 (if I pass si prefix settings along with customdata) or null if I pass no custom si prefix formatting. I would want to hide the data in the hoverlabel only when there is a nan in the list. Is it possible to do so?

enter image description here

import pandas as pd
import plotly.express as px
import numpy as np


data = {
  'ids':['SA', 'NA', 'Brazil', 'Uruguay', 'USA', 'Canada', 'PFV Brazil', 'PV Brazil', 'PFV Uruguay', 'PV Uruguay', 'PFV USA', 'PV USA', 'PFV Canada', 'PV Canada'],
  'labels': ['SA', 'NA', 'Brazil', 'Uruguay', 'USA', 'Canada', 'PFV', 'PV', 'PFV', 'PV', 'PFV', 'PV', 'PFV', 'PV'],
  'parent': ['', '', 'SA', 'SA', 'NA', 'NA', 'Brazil', 'Brazil', 'Uruguay', 'Uruguay', 'USA', 'USA', 'Canada', 'Canada'],
  'value': [0, 0, 100, 100, 400, 200, 8, 40, 4, 20, 11, 80, 11, 80]
  }
my_customdata = [x/780*100 if x>80 else np.nan for x in data['value']]
my_customdata[0] = 200/780*100
my_customdata[1] = 600/780*100

fig =px.sunburst(data, names='labels', parents='parent',  values='value', ids='ids', color='value',
                color_continuous_scale='Blues')

fig.update_traces(customdata=my_customdata, hovertemplate='%{label}<br>%{customdata:,.5s}')
fig.show()
like image 902
callmeanythingyouwant Avatar asked Jun 16 '21 14:06

callmeanythingyouwant


1 Answers

Building on the answer from here, the same can be applied in this question to get a solution that works. (In fact, it is a one stop solution for all your plotly hoverlabel problems!)

import pandas as pd
import plotly.express as px
import numpy as np


data = {
  'ids':['SA', 'NA', 'Brazil', 'Uruguay', 'USA', 'Canada', 'PFV Brazil', 'PV Brazil', 'PFV Uruguay', 'PV Uruguay', 'PFV USA', 'PV USA', 'PFV Canada', 'PV Canada'],
  'labels': ['SA', 'NA', 'Brazil', 'Uruguay', 'USA', 'Canada', 'PFV', 'PV', 'PFV', 'PV', 'PFV', 'PV', 'PFV', 'PV'],
  'parent': ['', '', 'SA', 'SA', 'NA', 'NA', 'Brazil', 'Brazil', 'Uruguay', 'Uruguay', 'USA', 'USA', 'Canada', 'Canada'],
  'value': [0, 0, 100, 100, 400, 200, 8, 40, 4, 20, 11, 80, 11, 80]
  }
my_customdata = [x/780*100 if x>80 else np.nan for x in data['value']]
my_customdata[0] = 200/780*100
my_customdata[1] = 600/780*100

fig =px.sunburst(data, names='labels', parents='parent',  values='value', ids='ids', color='value',
                color_continuous_scale='Blues')

df = pd.DataFrame(my_customdata)

def human_format(num):
    num = float('{:.3g}'.format(num))
    magnitude = 0
    while abs(num) >= 1000:
        magnitude += 1
        num /= 1000.0
    return '{}{}'.format('{:f}'.format(num).rstrip('0').rstrip('.'), 
                         ['', 'K', 'M', 'B', 'T'][magnitude])

df["hover_data"] = df.apply(lambda r:f"{human_format(r[0])}",axis=1)
df = df.replace('nan','')

fig.update_traces(customdata=df['hover_data'], hovertemplate='%{label}<br>%{customdata}')
fig.show()

enter image description here

like image 116
callmeanythingyouwant Avatar answered Sep 30 '22 17:09

callmeanythingyouwant