5

I have a very basic plot, for which I would like to add the ability to display values when mouse hovers over data points on the plot.

The code I use to create my line graph is as follows:

df_all['count'] = pd.to_numeric(df_all['count'])
cumulative = df_all['count'].cumsum()
cumulative.plot()

plt.plot()

print(plt.show())

Many thanks in advance

1

1 Answer 1

2

Further to the comment regarding Plotly, here is a very simple example of how to plot a graph, with hoverable trends.

Example code:

import random
import pandas as pd
from plotly.offline import plot

# Create a random list of values.
vals = [random.randint(0, 10) for _ in range(100)]

# Create a test DataFrame.
df = pd.DataFrame({'count': vals})
df['cumsum'] = df['count'].cumsum()

Create the graph using Plotly:

# Plot the results.
traces = []
traces.append({'y': df['count'], 'name': 'Single Counts'})
traces.append({'y': df['cumsum'], 'name': 'Cumulative'})

plot({'data': traces})

Output:
As you can see, my cursor was hovering at x: 50, y: 248. The displayed text is highly configurable, as can be reviewed in the hovertext documentation.

enter image description here

TL;DR:
Given the extensive configuration capability Plotly provides, it's very easy to get lost in Dash, Plotly Express, Figure Factories, etc., and come to a conclusion of 'What solution do I really need?' - I thought it would be helpful to show a very stripped down (yet entirely functional) example of how to plot a graph with hoverable trends.

Sign up to request clarification or add additional context in comments.

2 Comments

This was exactly what the Doctor ordered!!! I was having intermittent success displaying a plotly.express version but using plotly.offline seems to display without fail. Brilliant!
Excellent, my pleasure. Really glad it’s working for you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.