Plotly中如何调整绘图轨迹的Z轴显示层级
Plotly 图层显示顺序调整问题
我使用Plotly.express与Plotly.graph_objects绘图时遇到显示顺序问题:散点图显示在灰色连续误差阴影区域的上方。手动调整fig.data数组中各轨迹的位置没有效果,希望将散点图强制置于最底层,同时让灰色阴影区域显示在黑色线条的下方。
效果示例:灰色误差阴影区域位于散点图后方,黑色线条在阴影区域上方
用户提供的原始代码
import pandas as pd import numpy as np import plotly.graph_objects as go import plotly.io as pio import plotly.express as px pio.renderers.default='browser' df3 = pd.read_csv('filename.csv') fig1 = px.scatter(df3, x="height_diff", y="flow_speed", trendline="ols", trendline_color_override = 'crimson', template='plotly', color_continuous_scale='Viridis', color=np.random.randn(32145), title='Linear Regression, HYCOM GOFS3.1, 2010-2020, 3hourly mean height difference vs. 3hourly mean Florida Current flow speed' ) fig1.add_traces(px.line(df3, x="hgt_roll_avg", y="flow_roll_avg",color_discrete_sequence=['black']).data) #fig2 = px.line(df3, x="hgt_roll_avg", y="flow_roll_avg",color_discrete_sequence=['black']) #fig3 = go.Figure(data=fig1.data + fig.data, layout = fig1.layout) fig1.update_layout(coloraxis_showscale=False,colorscale=dict(diverging = 'Viridis')) fig1.add_traces([ go.Scatter( name='Upper Bound', x=df3['hgt_roll_avg'], y=df3['flow_roll_avg']+df3['std'], mode='lines', marker=dict(color="#444"), line=dict(width=1), showlegend=False ), go.Scatter( name='Lower Bound', x=df3['hgt_roll_avg'], y=df3['flow_roll_avg']-df3['std'], marker=dict(color="#444"), line=dict(width=1), mode='lines', fillcolor='rgba(68, 68, 68, 0.3)', fill='tonexty', showlegend=False ) ]) fig1.show()
解决方法
直接调整fig.data顺序无效,是因为Plotly对Express生成的轨迹有默认渲染优先级,需要通过显式设置layer参数强制指定层级。
修改后的完整代码
import pandas as pd import numpy as np import plotly.graph_objects as go import plotly.io as pio import plotly.express as px pio.renderers.default='browser' df3 = pd.read_csv('filename.csv') fig1 = px.scatter(df3, x="height_diff", y="flow_speed", trendline="ols", trendline_color_override = 'crimson', template='plotly', color_continuous_scale='Viridis', color=np.random.randn(32145), title='Linear Regression, HYCOM GOFS3.1, 2010-2020, 3hourly mean height difference vs. 3hourly mean Florida Current flow speed' ) # 1. 将散点图强制设置为最底层 fig1.data[0].update(layer='below') # 2. 添加黑色滚动平均线(后添加的轨迹默认在上方) fig1.add_traces(px.line(df3, x="hgt_roll_avg", y="flow_roll_avg",color_discrete_sequence=['black']).data) fig1.update_layout(coloraxis_showscale=False,colorscale=dict(diverging = 'Viridis')) # 3. 添加误差阴影区域并设置为黑色线条下方 error_traces = [ go.Scatter( name='Upper Bound', x=df3['hgt_roll_avg'], y=df3['flow_roll_avg']+df3['std'], mode='lines', marker=dict(color="#444"), line=dict(width=1), showlegend=False, layer='below' ), go.Scatter( name='Lower Bound', x=df3['hgt_roll_avg'], y=df3['flow_roll_avg']-df3['std'], marker=dict(color="#444"), line=dict(width=1), mode='lines', fillcolor='rgba(68, 68, 68, 0.3)', fill='tonexty', showlegend=False, layer='below' ) ] fig1.add_traces(error_traces) # 可选:若需调整趋势线层级,取消下方注释 # fig1.data[1].update(layer='below') fig1.show()
关键逻辑说明
layer参数有三个可选值:'above'(默认,上方)、'below'(下方)、'auto'(自动)- 显式设置
layer='below'可以强制覆盖Plotly默认的轨迹渲染优先级 - 黑色线条在误差阴影之后添加,且阴影设置为
below,确保黑色线条在阴影上方;散点图提前设置为below,固定在最底层
内容的提问来源于stack exchange,提问作者Chris Love
相关产品推荐
相关产品推荐

