Plotly:将 timeseries/candlestick 图表与 bubble/scatter 图表相结合

Plotly: combine timeseries/candlestick chart with bubble/scatter plot

是否可以将烛台图与气泡散点图与 plotly 相结合?示例数据:

| date       | open  | high  | low   | close | value |
| --------   | ------| ------| ------| ------| ------|
| 01/09/2021 | 64.00 | 65.25 | 64.80 | 65.00 | 1000  |
| 02/09/2021 | 65.00 | 66.25 | 65.00 | 66.00 | NaN   |
| 03/09/2021 | 66.00 | 67.00 | 65.95 | 67.00 | NaN   |
| 04/09/2021 | 68.00 | 68.25 | 66.85 | 68.00 | -500  |
| 07/09/2021 | 68.00 | 68.50 | 66.75 | 67.00 | NaN   |
| 08/09/2021 | 67.00 | 67.25 | 64.50 | 65.00 | NaN   |
| 09/09/2021 | 65.00 | 65.50 | 64.00 | 64.00 | 2000  |

例如,我希望在 65、68、64 的价格水平上出现一个气泡,而具有正值的气泡为绿色(如 01/09/2021 和 09/09/2021)以及那些负值为红色(如 04/09/2021)。

现在我有以下代码,它给我一个图表,如附图所示。

fig.add_trace(
    go.Candlestick(x=df['date'],
                open=df['open'],
                high=df['high'],
                low=df['low'],
                close=df['close'],
                yaxis= "y2"  
            ), 
        row = 1, col=2
    )

fig.add_trace(go.Scatter(line=dict(color='rgb(30,46,185)'),
    x=df['date'], y = df['close'],
    mode='markers',
             ),
        row = 1, col=2
    )
  • 问题样本数据集不完整,所以使用了plotly样本数据集。重命名列以使其与您的代码一致
  • 您可以在同一图中创建散点图和烛台图。一个关键要求,确保两者都使用相同的 yaxis。默认情况下烛台使用 y2
  • 颜色或气泡/散点由数组定义,其中没有要绘制的值(NaN)它使用透明。
  • 您似乎使用了子图,因此需要注意使 yaxis 一致
# fmt: off
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
df = df.rename(columns={s: t for s, t in zip(df.columns, [c.strip("APPL").strip(".").lower() for c in df.columns])})
df = df.head(20)
df["value"] = np.random.choice([1000, -500, np.nan], len(df))
# fmt: on

go.Figure(
    [
        go.Scatter(
            x=df["date"],
            y=df["close"],
            mode="markers",
            marker_color=np.select(
                [df["value"] > 0, df["value"] < 0], ["green", "red"], "rgba(0,0,0,0)"
            ),
            yaxis="y2",
            name="Bubble"
        ),
        go.Candlestick(
            x=df["date"],
            open=df["open"],
            high=df["high"],
            low=df["low"],
            close=df["close"],
            yaxis="y2",
            name="Candlestick"
        ),
    ]
).update_layout(xaxis_rangeslider_visible=False)

“气泡”的连续颜色

import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px

# fmt: off
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
df = df.rename(columns={s: t for s, t in zip(df.columns, [c.strip("APPL").strip(".").lower() for c in df.columns])})
df = df.head(20)
df["value"] = np.random.randint(-500, 1000, len(df))
df.loc[df.sample(10).index, "value"] = np.nan
# fmt: on

go.Figure(
    [
        px.scatter(
            df.loc[
                ~df["value"].isna(),
            ],
            x="date",
            y="close",
            color="value",
            color_continuous_scale="Viridis",
        )
        .update_traces(yaxis="y2", marker_size=20)
        .data[0],
        go.Candlestick(
            x=df["date"],
            open=df["open"],
            high=df["high"],
            low=df["low"],
            close=df["close"],
            yaxis="y2",
            name="Candlestick",
        ),
    ]
).update_layout(xaxis_rangeslider_visible=False, coloraxis_colorbar_y=0.35)