混合图组合 Plotly 表达 scatter 和 go.Table

发布于 2025-01-10 02:49:21 字数 1057 浏览 1 评论 0原文

我想生成一个 2col、1 行的子图:

  • row1,col1: px.scatterplot
  • row1,col2: go.Table

它应该看起来像这样: 输入图片这里的描述

我无法将这些数字放在一起,这是到目前为止的代码,它生成表格,注释掉的是 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()

I want to generate a subplot figure with 2col, 1 row with:

  • row1,col1: px.scatter plot
  • row1,col2: go.Table

it should look like this:
enter image description here

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 :

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最美不过初阳 2025-01-17 02:49:21

你已经完成了所有的辛苦工作!这只是迭代轨迹并添加到子图的情况。

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()

输入图片此处描述

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()

enter image description here

感情废物 2025-01-17 02:49:21

我无法在子图中使用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()

输入图片此处描述

I couldn't use plotly.express in the subplot, so I used graph objects for both. why do you want to use 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()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文