4

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

S3DEV
  • 8,768
  • 3
  • 31
  • 42
windwalker
  • 359
  • 4
  • 14
  • Additional answers for hover annotations: [How to add hovering annotations in matplotlib](https://stackoverflow.com/q/7908636/7758804) – Trenton McKinney Aug 16 '21 at 17:02

1 Answers1

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.

S3DEV
  • 8,768
  • 3
  • 31
  • 42
  • 1
    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! – windwalker Oct 26 '20 at 06:30
  • Excellent, my pleasure. Really glad it’s working for you! – S3DEV Oct 26 '20 at 06:58