生生不息

生生不息

AI美化交互式图像

2025-06-22
AI美化交互式图像

提示词

# Python代码
# 导入plotly
import plotly.graph_objects as go
from plotly.subplots import make_subplots


# 创建一个子图
fig = make_subplots(rows=2, cols=1, 
                    shared_xaxes=True, 
                    subplot_titles=("收盘价", "简单收益"))

# 添加收盘价的线图
fig.add_trace(go.Scatter(x=df.index, 
                         y=df['收盘'], 
                         mode='lines', 
                         name='收盘价'),
              row=1, col=1)

# 添加简单收益的线图
fig.add_trace(go.Scatter(x=df.index, 
                         y=df['简单收益'], 
                         mode='lines', 
                         name='简单收益'),
              row=2, col=1)

# 设置布局
fig.update_layout(title='股票数据子图展示')

# 显示图形
fig.show()

把上面的子图的标题去掉,改为y轴的标签,并在上面的图像中添加注释,请给出示例代码。

答案

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

# 创建子图(移除标题)
fig = make_subplots(rows=2, cols=1, 
                   shared_xaxes=True)

# 添加收盘价的线图
fig.add_trace(go.Scatter(x=df.index, 
                         y=df['收盘'], 
                         mode='lines', 
                         name='收盘价'),
              row=1, col=1)

# 添加简单收益的线图
fig.add_trace(go.Scatter(x=df.index, 
                         y=df['简单收益'], 
                         mode='lines', 
                         name='简单收益'),
              row=2, col=1)

# 设置Y轴标签(替代标题)
fig.update_yaxes(title_text="收盘价格", row=1, col=1)
fig.update_yaxes(title_text="收益率", row=2, col=1)

# 添加注释到第一个子图(关键点标注)
fig.add_annotation(
    x = df['收盘'].idxmax(),  # 标注最高点
    y=df['收盘'].max(),
    text="历史最高点",
    showarrow=True,
    arrowhead=2,
    ax=-50,  # 箭头水平偏移量[8](@ref)
    ay=-30,  # 箭头垂直偏移量[8](@ref)
    row=1,
    col=1
)

# 添加注释到第一个子图(趋势说明)
fig.add_annotation(
    x=df.index[10],
    y=df['收盘'].mean(),
    text="上升趋势开始",
    xref="x1",
    yref="y1",
    showarrow=False,
    font=dict(size=12, color="green"),
    bgcolor="rgba(255,255,255,0.5)"
)

# 统一布局设置
fig.update_layout(
    title='股票数据趋势分析',
    hovermode="x unified",  # 联动悬停效果
    xaxis_title="日期",
    height=600,
    margin=dict(l=50, r=30, t=80, b=50)
)

# 显示图形
fig.show()

图像: