简体   繁体   English

Plotly:如何控制双 y 轴前面的轨迹?

[英]Plotly: How to control which trace is in front for double y-axes?

I have a double y-axis plot that looks like this:我有一个双 y 轴图,如下所示:

在此处输入图片说明

In this example, the bars are on the secondary y-axis.在此示例中,条形位于次要 y 轴上。 However, I want the line to be in front of the bars.但是,我希望这条线位于酒吧的前面。 I thought doing yaxis2=dict(overlaying='y1') would solve the issue, but nothing different occurred.我认为做yaxis2=dict(overlaying='y1')会解决这个问题,但没有发生任何不同。 The only hackish solution I have come up with so far is to turn the opacity down on the bars so you can actually see the line.到目前为止,我想出的唯一骇人听闻的解决方案是将条形图的不透明度调低,这样您就可以真正看到线条。 Surely though there is a way to fix this that I am missing.当然,尽管有一种方法可以解决我缺少的问题。

Answer:回答:

The trace on the secondary y-axis will appear on top.次 y 轴上的轨迹将出现在顶部。 No matter if its a go.Scatter() trace or a go.Bar() trace无论是go.Scatter()跟踪还是go.Bar()跟踪

Details:细节:

The overlaying attribute only applies to cases with more than two y-axes, like here :overlaying属性仅适用于案件有两个以上的y轴一样,在这里

在此处输入图片说明

From the docs you can see that:文档中您可以看到:

overlaying Parent: layout.xaxis Type: enumerated , one of ( "free" | "/^x([2-9]|[1-9][0-9]+)?$/" | "/^y([2-9]|[1-9][0-9]+)?$/" ) If set a same-letter axis id, this axis is overlaid on top of the corresponding same-letter axis, with traces and axes visible for both axes.重叠父:layout.xaxis 类型:枚举,其中之一 ( "free" | "/^x([2-9]|[1-9][0-9]+)?$/" | "/^y( [2-9]|[1-9][0-9]+)?$/" ) 如果设置了相同字母的轴 id,则该轴覆盖在相应的相同字母轴的顶部,带有轨迹和轴两个轴都可见。 If "False", this axis does not overlay any same-letter axes.如果为“False”,则此轴不会覆盖任何相同字母的轴。 In this case, for axes with overlapping domains only the highest-numbered axis will be visible.在这种情况下,对于具有重叠域的轴,只有编号最高的轴是可见的。

So, how about your case?那么,你的情况呢? Generally, traces are displayed in the order that they are added to the fig .通常,跟踪按它们添加到fig的顺序显示。 But not when it comes to a figure with two y-axes.但对于具有两个 y 轴的图形则不然。 It seems you will have to switch with trace you're displaying on the primary axis.看来您将不得不使用在主轴上显示的轨迹进行切换。 The first snippet produces a figure with bars on the primary axis.第一个片段生成一个在主轴上带有条形图的图形。 The second snippet producds a figure with bars on the secondary axis.第二个片段生成一个在辅助轴上带有条形的图形。 This also determines which trace is shown on top.这也决定了哪条轨迹显示在顶部。

Code 1:代码 1:

import plotly.graph_objects as go
from plotly.subplots import make_subplots

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

# Add traces
fig.add_trace(
    go.Bar(x=[1, 2, 3], y=[40, 50, 60], name="yaxis data"),
    secondary_y=True,
)

fig.add_trace(
    go.Scatter(x=[1, 2, 3], y=[45, 50, 45], name="yaxis2 data"),
    secondary_y=False,
)

fig.show()图.show()

Plot 1:情节 1:

在此处输入图片说明

Code 2:代码 2:

import plotly.graph_objects as go
from plotly.subplots import make_subplots

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

# Add traces
fig.add_trace(
    go.Bar(x=[1, 2, 3], y=[40, 50, 60], name="yaxis data"),
    secondary_y=False,
)

fig.add_trace(
    go.Scatter(x=[1, 2, 3], y=[45, 50, 45], name="yaxis2 data"),
    secondary_y=True,
)


fig.show()

Plot 2:情节 2:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM