简体   繁体   中英

Putting box plot below scatter plot [Plotly]

I want a box plot to be placed on top of a scatter plot. Neither putting the box trace at first and adding the scatter plot on top nor adding an "layer='below'" attribute leads to the desired result. The box always stays in the back. Any suggestions?

import plotly.graph_objects as go
import numpy as np

np.random.seed(10)
rand = np.random.uniform(-100, 100, 100)

fig = go.Figure()
fig.add_trace(go.Box(
    x=rand,
    name='Markers',
    line_color='rgba(128, 128, 128, .0)',
    fillcolor='darkgrey'
))
fig.add_trace(go.Scatter(
    x=rand,
    y=['Markers']*len(rand),
    name='Markers',
    mode="markers",
    marker_color='orange',
    marker_size=8,
    # layer='below' # does not work
))
fig.show()

在此处输入图像描述

As suggested here , you have to attach the box plot to another axis:

fig = go.Figure()
fig.add_trace(go.Box(
    x=rand,
    name='Markers',
    line_color='rgba(128, 128, 128, .0)',
    fillcolor='darkgrey',
    yaxis='y2'
))
fig.add_trace(go.Scatter(
    x=rand,
    y=['Markers']*len(rand),
    name='Markers',
    mode="markers",
    marker_color='orange',
    marker_size=8
#     layer='below' # does not work
))
fig.update_layout(yaxis2=dict(
        matches='y',
        layer="above traces",
        overlaying="y",       
    ),)

fig.show()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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