39

I'm currently using the plotly service to graph some water quality data. I've added some lines to represent the the various stages of water quality, with them shaded so they are green, yellow, and red.

I've been able to remove some unnecessary lines from the legend, but they still show up when hovering over the data. I've looked here text and annotations but when trying to use the "hoverinfo" parameter, I get a

"plotly.exceptions.PlotlyDictKeyError: Invalid key, 'hoverinfo', for class, 'Scatter'."

error. Is there an alternative way to doing this for the Scatter plot? So far I've looked and have found nothing too helpful.

Here is how I'm currently trying to set up the trace:

badNTULevel = Scatter(                                                                              
x=[],                                                                                           
y=[100],                                                                                        
mode='lines',                                                                                   
line=Line(                                                                                      
    opacity=0.5,                                                                                
    color='rgb(253,172,79)',                                                                    
    width=1,                                                                                    
),                                                                                              
stream=Stream(                                                                                  
    token=stream_ids[3],                                                                        
    maxpoints=80                                                                                
),                                                                                              
hoverinfo='none',                                                                               
fill='tonexty',                                                                                 
name="Water Treatment Plants Can't Process over 100"
)                                        

Any help would be appreciated.

Lars Kotthoff
  • 107,425
  • 16
  • 204
  • 204
ChrisDevWard
  • 885
  • 2
  • 9
  • 20
  • 1
    You'll have to add the "`validate=False`" to your plot call and use `dict` instead of `Scatter`. That'll bypass the validation error that you're seeing. See this example for more: https://plot.ly/python/text-and-annotations/#disabling-hover-text – Chris P Aug 31 '15 at 21:17
  • @ChrisP I just had a chance to try that. Unfortunately it's still giving the same error, even with the validation set to false. Please see this larger code snippet [here](http://pastebin.com/N84UpBv0) It's throwing the error on line 134. – ChrisDevWard Sep 01 '15 at 02:43
  • 1
    OK, also change `Figure` to `dict` (it's just a simple subclass anyway). Long story short, the validation outdates the featured keys, which causes validation errors whenever a subclassed `dict` from `plotly.graph_objs` is used. – Chris P Sep 02 '15 at 05:00
  • @ChrisP That worked, thanks! – ChrisDevWard Sep 02 '15 at 08:33
  • Only managed to do it skipping the Data object from Scatter(dict) directly to Figure(also dict). – olivervbk Sep 29 '15 at 03:03

4 Answers4

63

On your trace add: hoverinfo='skip'

trace = dict(
             x=[1,2,3,4],
             y=[1,2,3,4],
             hoverinfo='skip'
            )
Henrique Florencio
  • 3,440
  • 1
  • 18
  • 19
12
from plotly.offline import plot
import plotly.graph_objs as go

def spline(x_axis,loop):

     trace = go.Scatter(
        x = x_axis,
        y = loop[i],
        fill = 'tonexty',
        mode ='lines',
        showlegend = False,
        hoverinfo='none'
        )

    data = [trace]



    layout = go.Layout(
        title='Graph title here',
        height=600,
        xaxis=dict(
            autorange=True
        ),
        yaxis=dict(
            autorange=True
        )
    )
    fig = go.Figure(data=data, layout=layout)
    # plot(fig, filename='spline.html')
    plot_div = plot(fig, output_type='div', include_plotlyjs=False)
    return plot_div
MrLeeh
  • 5,321
  • 6
  • 33
  • 51
Shinto Joseph
  • 2,809
  • 27
  • 25
  • 3
    @Shinto_Joseph Could you add some explanation to your code – MrLeeh Jan 23 '17 at 10:45
  • its written as a function... spline_graph = plots.spline(x_axis,loop,column,spline_percent,final_row) .this way you will get a script into variable spline_graph which you can pass and display into the html page using ajax without refreshing the page. – Shinto Joseph Jan 24 '17 at 11:05
  • in the above code just give the input for x axis and loop is just the y axis.showlegend = False, hoverinfo='none' will disable the hover and legends – Shinto Joseph Jan 24 '17 at 11:10
  • you have to import this from plotly.offline import plot import plotly.graph_objs as go and add go with the scatter like the above example – Shinto Joseph Jan 24 '17 at 11:12
4

If you use plotly.express, you can use this line, instead:

fig.update_layout(hovermode=False)
Hamzah
  • 8,175
  • 3
  • 19
  • 43
3

A more general-purpose solution may be to set the Layout 'hovermode' property, as follows:

#...
layout = go.Layout(hovermode=False)
fig = go.Figure(data=data, layout=layout)
#...

Reference for Python here: https://plot.ly/python/reference/#layout

NB: This will disable hover text for all traces associated with that layout... May not be the desired behaviour.

dpb
  • 3,672
  • 2
  • 20
  • 15