【问题标题】:Draw plotly bar traces under scatter traces rather than over?在散布迹线下而不是在上面绘制绘图条形迹线?
【发布时间】:2018-04-06 18:42:13
【问题描述】:

我试图在 plotly/dash 中显示同一图上的几条痕迹。无论我在data 变量中指定的轨迹顺序如何,条形轨迹最终都会覆盖散点轨迹。这是我生成情节的内容。

每个散点轨迹都是用

生成的
go.Scatter(
    x=list(dfN['DATE'])
    ,y=list(dfN['VALUE'])
    ,text=list(dfN['VALUE'].round(decimals=3))
    ,hoverinfo='text+name'
    ,name='Scatter N'
    ,mode='lines+markers'
    ,marker=dict(
        color='rgb(230,159,0)',
        size=15,
        opacity=1,
        line={'width': 0.5, 'color': 'white'}
    )
)

每个条形轨迹都是用

生成的
dropin_style=dict(
    hoverinfo='text+name'
    ,hoverlabel=dict(
        namelength=-1
    )
    ,textposition = 'auto'
    ,yaxis='y2'
)
go.Bar(
    legendgroup='bars'
    ,x=list(dfM['DATE'])
    ,y=list(dfM['VALUE'])
    ,text=list(dfM['VALUE'])
    ,name='Bar M'
    ,**dropin_style
    ,marker={'color':'#cccccc'}
    ,textfont=dict(color='#000000')
)

然后所有的痕迹都放在一起

data = [bar1, bar2, bar3, bar4, bar5,
                scatter1, scatter2, scatter3,]
layout = go.Layout(
    showlegend=True
    ,legend=dict(orientation="h",x=0, y=1.3)
    ,barmode='stack'
    ,xaxis=dict(
        dtick='M1'
    )
    ,yaxis=dict(
        range=[0,1.1],
        fixedrange=True,
        title='Percent'
    )
    ,yaxis2=dict(
        range=[0, ymax]
        ,fixedrange=True
        ,side='right'
        ,title='Count'
        ,overlaying='y'
        ,zeroline=False
        ,showgrid=False
    )
)
return {'data': data, 'layout': layout}

我该怎么做才能使条形迹线绘制在散点迹线下方?我尝试设置条形的不透明度,但它使整个图表更难看。

【问题讨论】:

  • 离线绘图时是否有同样的问题,例如在 Jupyter 笔记本中?

标签: python python-3.x plotly plotly-dash


【解决方案1】:

我认为您的代码中缺少某些内容,但我推断**dropin_style 指定条形图具有yaxis: "y2",并且根据我的试验,它看起来与yaxis 关联的痕迹被绘制,然后与yaxis2 关联的痕迹被绘制,所以如果你希望你的散点轨迹在你的条形轨迹的顶部,你会想要与yaxis2相关的散点轨迹,你可以切换side,这样正确的轨迹就会出现在右边/左边。

【讨论】:

  • 哎呀。刚刚将 dropin_style 的定义添加到我的 OP 中。而这个假设是正确的。我将条形图切换到axis1,看看是否可以修复它。
  • K,进行了更改,这似乎正是原因所在。耻辱没有记录在某处,但我想这是有道理的。如果 javascript 库按轴顺序组装 SVG,那么 yaxis 将出现在 yaxis2 之前,并且由于 SVG 不使用 z-index 并且只是乱序,那么 yaxis2 将不可能放在yaxis下面。谢谢!
  • plot_bgcolor 设置为rgba(0,0,0,0)(透明)也有帮助。
猜你喜欢
  • 2017-01-18
  • 2015-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-27
  • 2022-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多