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

如何消除由lines+markers模式线条引发的图表左右大量内边距?

Plotly混合图表的左右内边距问题

问题现象

使用Plotly绘制包含柱状图与lines+markers模式散点线的组合图表时,若数据点数量为100,图表左右两侧会出现大量不必要的内边距。但以下两种情况可消除该内边距:

  • 不给散点图设置mode参数
  • 将数据点数量减少至10

复现代码

import numpy as np
import pandas as pd
import plotly.graph_objects as go

count = 100

df = pd.DataFrame(
    {
        "A": np.random.randint(10, 20, size=(count)),
        "B": np.random.randint(10, 20, size=(count)),
        "C": np.random.randint(100, 200, size=(count)),
    },
    index=[f"bar.{i}" for i in range(count)],
)

fig = go.Figure(
    data=[
        go.Bar(
            name=df.columns[0],
            orientation="v",
            x=df.index,
            y=df.iloc[:, 0],
            text=df.iloc[:, 0],
        ),
        go.Bar(
            name=df.columns[1],
            orientation="v",
            x=df.index,
            y=df.iloc[:, 1],
            text=df.iloc[:, 1],
        ),
        go.Scatter(
            name=df.columns[2],
            x=df.index,
            y=df.iloc[:, 2],
            text=df.iloc[:, 2],
            mode="lines+markers",
        ),
    ],
    layout=dict(
        width=1024,
        height=512,
        margin=dict(l=10, r=10, t=10, b=10),
    ),
)

fig.update_layout(
    xaxis_title=None,
    yaxis_title=None,
    legend_title=None,
)

fig.show()

效果对比

  • 问题场景(100个数据点+lines+markers模式):
    问题场景截图

  • 无mode参数效果:
    无mode参数效果截图

  • 10个数据点效果:
    10个数据点效果截图

相关参考

该问题与Stack Overflow上的同类讨论一致。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 11:32:38