You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python在同一列中插入两张Plotly可视化图表

Plotly同一列排列两张图表实现方案

你目前分别调用fig0.show()和fig1.show()会生成两张独立的图表,要将二者放在同一列展示,需要调用Plotly的子图工具创建组合画布,具体实现代码如下:

# 导入子图创建工具
from plotly.subplots import make_subplots
import plotly.express as px

# 创建2行1列的子图布局,调整垂直间距避免标题重叠
fig = make_subplots(
    rows=2, 
    cols=1, 
    vertical_spacing=0.2,
    subplot_titles=("按性别统计确诊病例", "按性别统计死亡病例")
)

# 处理第一张确诊病例图,此处保留你原有的fig0生成逻辑
fig0.update_traces(mode='markers', marker_line_width=1, marker_size=5)
# 将fig0的轨迹添加到第1行第1列
for trace in fig0.data:
    fig.add_trace(trace, row=1, col=1)
fig.update_yaxes(zeroline=False, row=1, col=1)
fig.update_xaxes(zeroline=False, row=1, col=1)

# 处理第二张死亡病例图
fig1 = px.line(timegender, x='date', y='deceased', color='sex', markers = True)
fig1.update_traces(mode='markers', marker_line_width=1, marker_size=5)
# 将fig1的轨迹添加到第2行第1列
for trace in fig1.data:
    fig.add_trace(trace, row=2, col=1)
fig.update_yaxes(zeroline=False, row=2, col=1)
fig.update_xaxes(zeroline=False, row=2, col=1)

# 调整整体画布高度避免内容挤压
fig.update_layout(height=800)
# 一次性展示同列排列的两张图
fig.show()

如果需要调整两张图的占比,可以在创建子图时添加row_heights=[0.5,0.5]参数自定义各行高度占比。

内容的提问来源于stack exchange,提问作者Ganaa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 15:27:03