Pyplot:鼠标悬停点时显示标签值 [英] Pyplot: Display label values when mouse hover point
问题描述
我有一个非常基本的图,我想为其添加当鼠标悬停在图上的数据点上时显示值的功能.
我用来创建折线图的代码如下:
df_all['count'] = pd.to_numeric(df_all['count'])累积 = df_all['count'].cumsum()累积.plot()plt.plot()打印(plt.show())
非常感谢
关于
TL;博士:
鉴于 Plotly 提供的广泛配置能力,很容易迷失在 Dash、Plotly Express、Figure Factories 等中,得出我真正需要什么解决方案?"的结论.- 我认为展示一个非常精简(但完全实用)的示例来说明如何绘制具有可悬停趋势的图表会很有帮助.
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
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.
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.
这篇关于Pyplot:鼠标悬停点时显示标签值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!