在子图的第三行中添加第二个散点图

时间:2019-09-22 13:16:09

标签: python plotly

我的代码呈现: 一个第一行-表。在第二行-2条形图。没关系。我要显示的第三件事是条形图,是第二行的2个条形图的组合。我正在使用fig.add_traces(...)并合并两个条形图。但它出现在pdf表中。我尝试使用specs,但一无所获。还尝试将row=3, col=1添加到fig.add_traces,但不允许我这样做。我的代码:

from plotly import graph_objs as go
import numpy as np
import os
from plotly.subplots import make_subplots

fig = make_subplots(rows=3, cols=2)

website_fig = go.Bar(
        x=[
            "01/09/2019 - 07/09/2019",
            "08/09/2019 - 14/09/2019", 
            "15/09/2019 - 21/09/2019",
            "22/09/2019 - 28/09/2019"
            ],
        y=[15, 25, 35, 32],
        name= "Website statistic"
    )

linkedin_fig = go.Bar(
        x=[
            "01/09/2019 - 07/09/2019",
            "08/09/2019 - 14/09/2019", 
            "15/09/2019 - 21/09/2019",
            "22/09/2019 - 28/09/2019"
            ],
        y=[153, 102, 187, 200],
        name= "LinkedIn statistic"
    )

# Chart product views
fig.add_trace(  
    website_fig,
    row=2,
    col=1
)

# Chart product views LinkedIn
fig.add_trace(  
    linkedin_fig,
    row=2,
    col=2
)

wesbite_views = [15, 25, 35, 32]
wesbite_views.append(sum(wesbite_views))
linkedin_views = [153, 102, 187, 200]
linkedin_views.append(sum(linkedin_views))
total_views  = [x + y for x, y in zip(wesbite_views, linkedin_views)]

# Product views table
fig.add_trace(
    go.Table(
        header=dict(values=["Period", "Website views", "LinkedIn views", "Total views"]),
        cells=dict(values=[
            [
                "01/09/2019 - 07/09/2019",
                "08/09/2019 - 14/09/2019", 
                "15/09/2019 - 21/09/2019",
                "22/09/2019 - 28/09/2019",
                "01/09/2019 - 28/09/2019",
            ],
            wesbite_views,
            linkedin_views,
            total_views
        ])
    )
)

fig.add_traces([
        go.Scatter(
            x=[
                "01/09/2019 - 07/09/2019",
                "08/09/2019 - 14/09/2019", 
                "15/09/2019 - 21/09/2019",
                "22/09/2019 - 28/09/2019"
                ],
            y=[15, 25, 35, 32],
        ),
        go.Scatter(
            x=[
                "01/09/2019 - 07/09/2019",
                "08/09/2019 - 14/09/2019", 
                "15/09/2019 - 21/09/2019",
                "22/09/2019 - 28/09/2019"
                ],
            y=[153, 102, 187, 200],
        ),
    ]
)

if not os.path.exists("files"):
    os.mkdir("files")

fig.update_yaxes(title_text="Product views", range=[0, 40], row=2, col=1)
fig.update_layout(height=700, width=1000, title_text="<b>Library Shelving</b> statistic")
fig.write_image("files/statistic.pdf")

1 个答案:

答案 0 :(得分:1)

如果我用add_tracesrow运行col,我会得到

TypeError: add_traces() got an unexpected keyword argument 'row'

看着these examples of subplots,看起来参数应该是rowscols

fig.add_traces([
        go.Scatter(
            x=[
                "01/09/2019 - 07/09/2019",
                "08/09/2019 - 14/09/2019",
                "15/09/2019 - 21/09/2019",
                "22/09/2019 - 28/09/2019"
                ],
            y=[15, 25, 35, 32],
        ),
        go.Scatter(
            x=[
                "01/09/2019 - 07/09/2019",
                "08/09/2019 - 14/09/2019",
                "15/09/2019 - 21/09/2019",
                "22/09/2019 - 28/09/2019"
                ],
            y=[153, 102, 187, 200],
        ),
    ],
    rows=[3, 3], cols=[1, 1]
)

我也将原来的通话从make_subplots更改为

fig = make_subplots(rows=3, cols=2, specs=[
    [{"colspan": 2}, None], [{}, {}], [{"colspan":2}, None]])

我没有更改代码中的其他任何内容(这些是散点图,而不是折线图)