From d1234eff79612f21521e699685184b08af94fc55 Mon Sep 17 00:00:00 2001 From: zhenghu <1831829219@qq.com> Date: Tue, 14 Apr 2026 15:23:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20Plotly=20add=5Fvlin?= =?UTF-8?q?e=20=E5=AF=B9=20datetime.date=20=E7=9A=84=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E6=80=A7=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 手动使用 shape + annotation 添加生育期 milestone 竖线 - 替换 LAI 和干物质两图中的 add_vline 调用 - 避免 datetime.date 类型触发 Plotly 内部异常 --- app.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index 7ed69f8..4633487 100644 --- a/app.py +++ b/app.py @@ -17,6 +17,25 @@ def hex_to_rgba(hex_color: str, alpha: float = 0.1) -> str: return f"rgba({r}, {g}, {b}, {alpha})" +def add_milestone_line(fig, x, color: str, text: str, xref: str = "x"): + """用 shape + annotation 手动添加竖线,绕过 Plotly add_vline 对 datetime.date 的 bug。""" + fig.add_shape( + type="line", + x0=x, x1=x, y0=0, y1=1, + xref=xref, yref="paper", + line=dict(color=color, width=1.5, dash="dot"), + ) + fig.add_annotation( + x=x, y=1.02, + xref=xref, yref="paper", + text=text, + showarrow=False, + font=dict(color=color, size=10), + bgcolor="rgba(255,255,255,0.8)", + borderpad=2, + ) + + # ─── Page Config ──────────────────────────────────────────────────────────── st.set_page_config( page_title="麦麦智农", @@ -289,13 +308,9 @@ with chart_left: # 添加生育期标记 ms = summary.get("milestones", {}) if "flowering" in ms: - fig_lai.add_vline(x=ms["flowering"], line=dict(color="#d4a574", width=1.5, dash="dot"), - annotation_text="开花", annotation_font_color="#7c5e42", - annotation_position="top left") + add_milestone_line(fig_lai, x=ms["flowering"], color="#d4a574", text="开花") if "maturity" in ms: - fig_lai.add_vline(x=ms["maturity"], line=dict(color="#7c5e42", width=1.5, dash="dash"), - annotation_text="成熟", annotation_font_color="#7c5e42", - annotation_position="top right") + add_milestone_line(fig_lai, x=ms["maturity"], color="#7c5e42", text="成熟") fig_lai.update_layout( xaxis=dict(title="日期", color="#5a5a5a", gridcolor="rgba(0,0,0,0.06)"), yaxis=dict(title="LAI", color="#5a5a5a", gridcolor="rgba(0,0,0,0.06)"), @@ -327,9 +342,9 @@ with chart_right: )) ms = summary.get("milestones", {}) if "flowering" in ms: - fig_bio.add_vline(x=ms["flowering"], line=dict(color="#d4a574", width=1.5, dash="dot")) + add_milestone_line(fig_bio, x=ms["flowering"], color="#d4a574", text="开花") if "maturity" in ms: - fig_bio.add_vline(x=ms["maturity"], line=dict(color="#7c5e42", width=1.5, dash="dash")) + add_milestone_line(fig_bio, x=ms["maturity"], color="#7c5e42", text="成熟") fig_bio.update_layout( xaxis=dict(title="日期", color="#5a5a5a", gridcolor="rgba(0,0,0,0.06)"), yaxis=dict(title="干物质 (kg/ha)", color="#5a5a5a", gridcolor="rgba(0,0,0,0.06)"),