行子地块(时间序列)中的注释(图例文本)

2024-09-28 05:26:35 发布

您现在位置:Python中文网/ 问答频道 /正文

使用plotly(参考:herehere)在所有子地块中显示注释(图例)。在下面的语法中,我可以在其中一个子图(即在绘图中)中显示文本注释,如何才能在所有子图中显示文本注释

   volRatio pct_chg stkClose dailyRtnsStk   dailySPRtns     date
0   0.001   -1.078  19.710   0.072          0.029         2009-04-02
34  0.001   -1.079  19.710   0.072          0.029         2009-04-02
69  0.001   -0.910  75.870   0.034          0.018         2009-09-28
17  0.001   -0.910  75.870   0.034          0.018         2009-09-28
70  0.002   0.000   130.900 -0.013         -0.010         2009-12-31
74  0.002   0.000   130.900 -0.013         -0.010         2009-12-31

import plotly.graph_objects as go


fig = make_subplots(
    rows=2, cols=2,
    subplot_titles=("Volume Ratio", "Closing Value", "EPS Over Time", "Daily Returns Stock vs S&P"))

fig.append_trace(go.Scatter(
    x=fd_1.date,
    y=fd_1.volRatio,  name='Volume Ratio'
), row=1, col=1)


fig.append_trace(go.Scatter(
    x=fd_1.date,
    y=fd_1.stkClose, name='Closing Value'
), row=1, col=2)

fig.append_trace(go.Scatter(
    x=fd_1.date,
    y=fd_1.dailyRtnsStk, name='Daily Returns'
), row=2, col=2)

fig.append_trace(go.Scatter(
    x=fd_1.date,
    y=fd_1.dailySPRtns, name='S&P Returns'
), row=2, col=2)

fig.append_trace(go.Scatter(
    x=fd_1.date,
    y=fd_1.pct_chg, name='EPS Changes', legendgroup = '1'
), row=2, col=1)


fig.update_layout(title_text="Stacked Subplots", showlegend=False)


fig.update_annotations(dict(font_size=8))
for row in [1, 2]:
    if row == 1:
        #
        #
        fig.add_annotation(dict(x=col / 2 - 0.4, y=0.8, xref="paper", yref="paper", 
                                text='name %d' %row, showarrow=False))
        
    
fig.show()

Tags: name文本godateherefigtracecol
1条回答
网友
1楼 · 发布于 2024-09-28 05:26:35
  • 一种方法是添加额外的散点跟踪,其中包含所需注释的文本
  • 创建了一个熊猫系列,以获得文本的坐标
  • 使用了目录的列表,而不是复制粘贴的代码,这很难维护
  • textposition可以覆盖-不适合每日回报
import io
fd_1 = pd.read_csv(io.StringIO("""   volRatio pct_chg stkClose dailyRtnsStk   dailySPRtns     date
0   0.001   -1.078  19.710   0.072          0.029         2009-04-02
34  0.001   -1.079  19.710   0.072          0.029         2009-04-02
69  0.001   -0.910  75.870   0.034          0.018         2009-09-28
17  0.001   -0.910  75.870   0.034          0.018         2009-09-28
70  0.002   0.000   130.900 -0.013         -0.010         2009-12-31
74  0.002   0.000   130.900 -0.013         -0.010         2009-12-31
"""), sep="\s+")
fd_1["date"] = pd.to_datetime(fd_1["date"])

# where to place "legend" text
leg = fd_1.loc[fd_1["date"].eq(fd_1["date"].max())].iloc[-1]

在所需子批次中绘制每条线以及另一条轨迹,以标记它

import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(
    rows=2, cols=2,
    subplot_titles=("Volume Ratio", "Closing Value", "EPS Over Time", "Daily Returns Stock vs S&P"))

# copy paste hell, so represent each line/trace as a list of dictionaries
for l in [{"r":1, "c":1, "col":"volRatio", "name":"Volume Ratio"},
          {"r":1, "c":2, "col":"stkClose", "name":"Closing Value"},
          {"r":2, "c":2, "col":"dailyRtnsStk", "name":"Daily Returns", "pos":"top center"},
          {"r":2, "c":2, "col":"dailySPRtns", "name":"S&P Returns"},
          {"r":2, "c":1, "col":"pct_chg", "name":"EPS Changes"},
         ]:
    fig.append_trace(go.Scatter(
        x=fd_1.date,
        y=fd_1[l["col"]],  name=l["name"]
    ), row=l["r"], col=l["c"])
    fig.append_trace(go.Scatter(mode="text", x=[leg["date"]], y=[leg[l["col"]]], text=[l["name"]], textposition=l.get("pos", "bottom left")), 
                     row=l["r"], col=l["c"])


fig.update_layout(title_text="Stacked Subplots", showlegend=False)
    
fig.show()

enter image description here

相关问题 更多 >

    热门问题