如何从 plotly 中为 go.Scattergl 设置日期时间滑块,如 px.scatter

How to have a datetime slider like in px.scatter for go.Scattergl from plotly

大家早上好,

我的代码是:

fig = go.Figure()
fig.add_trace(
    go.Scattergl(x=result2['DateTime'], y=result2['Voc (V)'], mode='markers'))
# Set title
fig.update_layout(
    title_text="VOC (V) vs Time", xaxis_title="Time", yaxis_title="VOC (V)")
# Add range slider
fig.update_layout(
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1,
                     label="1m",
                     step="month",
                     stepmode="backward"),
                dict(count=6,
                     label="6m",
                     step="month",
                     stepmode="backward"),
                dict(count=1,
                     label="YTD",
                     step="year",
                     stepmode="todate"),
                dict(count=1,
                     label="1y",
                     step="year",
                     stepmode="backward"),
                dict(step="all")
            ])
        ),
        rangeslider=dict(
            visible=True
        ),
        type="date"
    )
)
from plotly import offline
offline.plot(fig)

还有这段代码:

import plotly.express as px

df = result2
fig = px.scatter(df, x="DateTime", y="Voc (V)",
                 color="Eclairement (W/m²)")
fig["layout"].pop("updatemenus")
from plotly import offline
offline.plot(fig)

我得到这张图片作为输出: 我的问题:我怎样才能有滑块+第3轴颜色(即着色y轴= Voc(V))

PS : result2 是我的一些列的数据框。

  • 这是一种解决方法。 rangeslider 视觉对象不显示 scattergl trace
  • 模拟了一些数据来演示
  • 假设正在使用 scattergl,因为它对大量点的表现更好。概念
    • 散点图跟踪使用点的子集,这样它就不会使用那么多的资源
    • 第二条轨迹使用不同的 yaxis,因此可以通过设置 domain hidden ] 非常小
    • 根据点数,您可能希望为 nth 观察选择一个较大的点数,用于 rangeslider visual
import numpy as np
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go

df = pd.DataFrame(
    {
        "DateTime": pd.date_range("1-aug-2021", freq="1H", periods=1000),
        "Voc (V)": ((np.sin(np.tile(np.linspace(-np.pi, np.pi, 100), 10)) + 1) * 700)
        * np.random.uniform(0.8, 0.9, 1000),
        "Eclairement (W/m²)": (np.sin(np.tile(np.linspace(-np.pi, np.pi, 100), 10)) + 1)
        * 5,
    }
)

# every nth row for rangeslider
df2 = df.iloc[::10, :]

fig = go.Figure(
    [
        go.Scattergl(
            x=df["DateTime"],
            y=df["Voc (V)"],
            mode="markers",
            marker_color=df["Eclairement (W/m²)"],
            name="main",
        ),
        go.Scatter(
            x=df2["DateTime"],
            y=df2["Voc (V)"],
            mode="markers",
            marker_color=df2["Eclairement (W/m²)"],
            name="rangeslider",
            yaxis="y2",
        ),
    ]
)

fig.update_layout(
    xaxis_rangeslider_visible=True,
    yaxis2={"domain": [0, 0.001], "visible": False},
    showlegend=False,
)