线子图(时间序列)中的注释(图例文本)
Posted
技术标签:
【中文标题】线子图(时间序列)中的注释(图例文本)【英文标题】:Annotations (legend text) in a line subplots (time series) 【发布时间】:2021-09-10 07:50:53 【问题描述】:使用 plotly 在所有子图中显示注释(图例)(参考:here,here)。在以下语法中,我可以在其中一个子图中(即在图中)显示文本注释,我如何能够在所有子图中显示文本注释?
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()
【问题讨论】:
【参考方案1】: 一种方法是添加额外的 Scatter 轨迹,其中包含您需要的注释文本 创建了一个熊猫系列来获取文本的坐标 已使用 dict 的list,而不是复制难以维护的粘贴代码 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()
【讨论】:
以上是关于线子图(时间序列)中的注释(图例文本)的主要内容,如果未能解决你的问题,请参考以下文章