首页 > 解决方案 > 在 Plotly 中在图的底部显示所有子图的迹线值

问题描述

我正在使用 plotly 进行数据可视化。

我生成了一个 3 条轨迹图,它们使用make_subplots. 我需要通过将鼠标悬停在 x 轴的某处来显示图底部所有迹线的相应 y 值。

下面是生成子图的代码。

import plotly
import plotly.graph_objs as go
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

import pandas as pd
import re

df = pd.read_csv("finance-charts-apple.csv")

fig = make_subplots(
    rows=3, cols=1,
    shared_xaxes=True,
    vertical_spacing=0.0,
    horizontal_spacing = 0.0,
    specs=[[{"type": "scatter"}],
           [{"type": "scatter"}],
           [{"type": "scatter"}]])
    
fig.add_trace(
    go.Scattergl(
        x=df.Date,
        y=df['AAPL.High'],
        mode="lines",
        name="AAPL_high"
    ),
    row=1, col=1
)    

fig.add_trace(
    go.Scattergl(
        x=df.Date,
        y=df['AAPL.Low'],
        mode="lines",
        name="AAPL_low"
    ),
    row=2, col=1
)    

fig.add_trace(
    go.Scattergl(
        x=df.Date,
        y=df['AAPL.Close'],
        mode="lines",
        name="AAPL_Close"
    ),
    row=3, col=1
) 

fig.update_layout(
    height=800,
    showlegend=True,
    title_text="Apple market share",
    hovermode= 'x unified',
    hoverinfo= "x+y",
    spikedistance= -1,
    xaxis = dict(
            showspikes = True,
            spikemode = 'across + toaxis',
            spikesnap = 'cursor',
            showline= True,
            showgrid = True,
            spikedash = 'solid'))

fig.update_xaxes(matches='x')
fig.update_traces(xaxis='x1')
fig.show()
plotly.offline.plot(fig)

根据上面的代码,我可以在所有子图中垂直悬停绘制 3 个子图。

我想知道的是,有没有办法在图的底部显示每个轨迹的特定 x 值的 y 值,如下图所示?

在此处输入图像描述

标签: pythonplotlyplotly-python

解决方案


截至 2020 年 7 月 21 日,hovermodes尚未对 plotly subplots (v4.4.x) 实施统一。

在他们的 js 存储库中,这个特性存在一个未解决的问题,但它是对所有发行版的有效请求。您可以在https://github.com/plotly/plotly.js/issues/4755订阅它。


推荐阅读