Additional measures in hover text


#1

How do I get the values for different dimensions in hover text (i.e. dimensions that aren’t on the graph).

For instance, let’s say we have the mean values for iris data:

target	      sepal length (cm)	sepal width (cm)	petal length (cm)	petal width (cm)
setosa	      5.006	            3.418	            1.464	            0.244
versicolor    5.936	            2.770	            4.260	            1.326
virginica     6.588	            2.974	            5.552	            2.026

I want to show a bar graph of sepal length, but want to additionally have sepal width (and petal width, petal length) in the hover text.

from sklearn.datasets import load_iris
iris=load_iris()
df = pd.DataFrame(data= iris['data'],
                     columns= iris['feature_names'], dtype=float)
df = df.assign(target=iris.target_names[iris['target']])
df=df.groupby(['target'], as_index=False).mean()
trace1 = go.Bar(
            name='sepal length',
            x=df['sepal length (cm)'],
            y=df.target,
            orientation = 'h',
            text=df.apply(lambda x: "{:.2}".format(x['sepal length (cm)']), axis=1),
            textposition = 'auto',
            marker=dict(
                color='rgb(158,202,225)',
                line=dict(
                    color='rgb(8,48,107)',
                    width=1.5),
            ),
            hoverinfo="name+text"
)
data=[trace1]
iplot(data)

Any ideas?


#2

@ Hey @QuinRiva,
In order to display all information on hover, insert before trace1 the following list of strings:

my_text=['(sepal length: '+'{:.2f}'.format(sl)+', sepal width:'+'{:.2f}'.format(sw)+')'+
  '<br>(petal length: '+'{:.2f}'.format(pl)+', petal width:'+'{:.2f}'.format(pw)+')'
  for sl, sw, pl, pw in zip(list(df['sepal length (cm)']), list(df['sepal width (cm)']),
                           list(df['petal length (cm)']), list(df['petal width (cm)'])) ] 

In the definition of trace1, set:

    text=my_text,
    hoverinfo='text'

and comment/delete your text=df.apply...
You can comment textposition='auto' or not, depending on whether you want the text to be displayed only on hover or not.
Here is the image illustrating how the plot looks with the text displayed on bars, too, not only on hover:text-iris


#3

Thanks @empet, that’s helpful but not quite there. What I’m trying to do is display basic info on the graph, and provide additional info only on hover.

I only want sepal length displayed as text on the graph, but on hover display all four. I.e I want the charts to look as they are in my code in the OP, but have the hovertext as your code.

Is that possible, or is the marker text always the same as the hover text (and I can only choose whether or not to show it)?


#4

@QuinRiva if you want to display only the sepal length as annotation (the text displayed on your bars), then define the plot layout and insert within this dict the annotations, as follows:

layout=dict(width=650,
            height=400, 
            autosize=False,
            annotations=[dict(font= dict(color='rgb(100,100,100)'),
                              showarrow=False,
                              text= 'sepal length: '+'{:.2f}'.format(df.loc[k, 'sepal length (cm)']),
                              x= df.loc[k, 'sepal length (cm)']-1,#here I subtracted 1 from the sepal length to avoid displaying 
                                                                  #the text at the bar end 
                              xref= 'x',
                              y= df.loc[k, 'target'],
                              yref= 'y') for k in range(len(df))] 
       )

Comment textposition=‘auto’ in trace1.

If you don’t want to display these annotations, just don’t include them within layout definition.


#5

This is perfect! I genuinely didn’t think that it would be possible.