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

如何将Plotly图表默认灰色背景改为白色或透明背景?

修改Plotly双轴折线图背景色的方法

要将图表背景从默认灰色改为白色或透明,只需在layout字典中添加两个核心参数:

  • paper_bgcolor:控制图表整体画布的背景色
  • plot_bgcolor:控制坐标轴内部绘图区域的背景色

改为白色背景的完整代码

# Add data
year = d['year']

Revenue = d['revenue']
Count = d['count']

# Create and style traces
trace0 = go.Scatter(
    x = year,
    y = Revenue,
    name = 'Revenue',
    line = dict(
        color = ('rgb(255, 255, 102)'),
        width = 4)
)
trace1 = go.Scatter(
    x = year,
    y = Count,
    name = 'Count',
    yaxis='y2',
    line = dict(
        color = ('rgb(8,48,107)'),
        width = 4,
        dash = 'dash'),
)
data = [trace0, trace1]

# Edit the layout - 新增背景色配置
layout = dict(title = 'Reject Rates - Spring 2017 through YTD 2018',
              xaxis = dict(title = 'Weekly Progession'),
              yaxis = dict(title = 'Rejects per delivery'),
              yaxis2 = dict(title='Minutes',
              overlaying='y',
              side='right',
              titlefont=dict(
                  color = ('rgb(8,48,107)'),
              ),
              tickfont=dict(
                  color=('rgb(8,48,107)')
              )),
              # 设置白色背景
              paper_bgcolor='white',
              plot_bgcolor='white'
)
              
fig = dict(data=data, layout=layout)
                            
iplot(fig)

改为透明背景的配置

只需将背景色参数替换为透明色值rgba(0,0,0,0):

# 在layout中替换背景色配置行
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 18:25:35