简体   繁体   English

混合 plot 结合 Plotly 快递分散和 go.Table

[英]Mixed plot compbining Plotly express scatter and go.Table

I want to generate a subplot figure with 2col, 1 row with:我想用 2col,1 行生成一个子图图形:

  • row1,col1: px.scatter plot row1,col1: px.scatter plot
  • row1,col2: go.Table行 1,列 2:go.Table

it should look like this:它应该是这样的: 在此处输入图像描述

I am unable to put the figures together, this is the code so far, it generates the table, and commented out is the px.scatter:我无法将这些数字放在一起,这是到目前为止的代码,它生成表格,并注释掉 px.scatter:

import plotly.express as px
df = px.data.iris()

fig = make_subplots(
    rows=1, cols=2,
    shared_xaxes=False,
    vertical_spacing=0.03,
    specs=[[{"type": "scatter"}, {"type": "table"}]]
)


#HERE IS THE PX.SCATTER PLOT (commented out since i cannot add it) 
# fig1 = px.scatter(df, x="sepal_width", y="sepal_length", color="species")

fig.add_trace(
    go.Table(
        header=dict(
            values=list(df.columns),
            font=dict(size=10),
            align="left"
        ),
        cells=dict(values=[df.sepal_length, df.sepal_width, df.petal_length, df.petal_width, df.species, df.species_id],
               fill_color='lavender',
               align='left')
    ),
    row=1, col=2
)
fig.show()

You have done all the hard work.你已经完成了所有艰苦的工作。 It's just a case of iterating over traces and adding to sub-plot.这只是迭代跟踪并添加到子图的情况。

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

df = px.data.iris()

fig = make_subplots(
    rows=1,
    cols=2,
    shared_xaxes=False,
    vertical_spacing=0.03,
    specs=[[{"type": "scatter"}, {"type": "table"}]],
)

# HERE IS THE PX.SCATTER PLOT (commented out since i cannot add it)
for t in px.scatter(df, x="sepal_width", y="sepal_length", color="species").data:
    fig.add_trace(t, row=1, col=1)

fig.add_trace(
    go.Table(
        header=dict(values=list(df.columns), font=dict(size=10), align="left"),
        cells=dict(
            values=[
                df.sepal_length,
                df.sepal_width,
                df.petal_length,
                df.petal_width,
                df.species,
                df.species_id,
            ],
            fill_color="lavender",
            align="left",
        ),
    ),
    row=1,
    col=2,
)
fig.show()

在此处输入图像描述

I couldn't use plotly.express in the subplot, so I used graph objects for both.我不能在子图中使用 plotly.express,所以我对两者都使用了图形对象。 why do you want to use plotly,express?为什么要用plotly,express?

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

df = px.data.iris()

fig = make_subplots(
    rows=1, cols=2,
    shared_yaxes=False,
    horizontal_spacing=0.03,
    specs=[[{"type": "scatter"},{"type": "table"}]]
)

fig.add_trace(
    go.Scatter(x=df["sepal_width"], y=df["sepal_length"], mode='markers',marker=dict(color=df['species_id'])),
              row=1,col=1)

fig.add_trace(
    go.Table(
        header=dict(
            values=list(df.columns),
            font=dict(size=10),
            align="left"
        ),
        cells=dict(values=[df.sepal_length, df.sepal_width, df.petal_length, df.petal_width, df.species, df.species_id],
               fill_color='lavender',
               align='left')
    ),
    row=1, col=2
)

fig.show()

在此处输入图像描述

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

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