python - Plotly:如何从两个数据框列中绘制两条线并从其他两列中分配悬停信息?

标签 python plotly timeserieschart

如何从数据帧的两列绘制两条线图,并且另一列表示 x 轴,另外两列表示前两列的悬停值(工具提示)?

最佳答案

使用go.Scatter,您可以将任何数据帧列y=df['A']显示为具有关联索引x=df.index的行,并使用 hovertext=df['A_info'] 将任何 pandas 数据框列指定为悬停信息的源来获取:

enter image description here

完整代码:

import pandas as pd
import plotly.graph_objects as go

# sample data
d={'A':[3,3,2,1,5],
   'B':[4,4,1,4,7],
   'A_info':['nothing', '', '', 'bad', 'good'],
   'B_info':['', '', 'bad', 'better', 'best']}

# pandas dataframe
df=pd.DataFrame(d, index=[10,11,12,13,14])

# set up plotly figure
fig = go.Figure()

# add line / trace 1 to figure
fig.add_trace(go.Scatter(
    x=df.index,
    y=df['A'],
    hovertext=df['A_info'],
    hoverinfo="text",
    marker=dict(
        color="blue"
    ),
    showlegend=False
))

# add line / trace 2 to figure
fig.add_trace(go.Scatter(
    x=df.index,
    y=df['B'],
    hovertext=df['B_info'],
    hoverinfo="text",
    marker=dict(
        color="green"
    ),
    showlegend=False
))

fig.show()

关于python - Plotly:如何从两个数据框列中绘制两条线并从其他两列中分配悬停信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60372991/

相关文章:

python - 如何摆脱 Perl 和 Python [两者] 中的非 ascii 字符?

python - 将字典重新映射到数据帧的更快方法

r - 为由knitr中的循环创建的每个图表添加标题

python - Plotly:使用 Python 在多行图中添加色阶

d3.js - 使用 D3.js 解析时间序列数据

python - ubuntu中gpu上的tensorflow安装

python - Python 中的 Marching Square 算法

pandas - 通过在 Pandas 中调用袖扣来改变身材大小

python - 如何将颜色条添加到已经存在的图形中?

java - 如何仅显示 "last 24 hours"JFreeChart TimeSeries 中的 "last 72 hours"