windwalker
windwalker

Reputation: 379

Pyplot: Display label values when mouse hover point

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

Upvotes: 5

Views: 6116

Answers (1)

s3dev
s3dev

Reputation: 9701

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.

Upvotes: 2

Related Questions