【问题标题】:I can't seem to get plotly to display multiple graphs我似乎无法显示多个图表
【发布时间】:2021-06-07 03:12:07
【问题描述】:

我想在 python 中创建一个漂亮的图表,所以我使用 plotly 来创建一个图表,但是我得到了一个错误。

可能是因为我是plotly 的新手,所以我不明白这段代码中的错误。
我唯一能说的是我的代码是错误的。
我想在 plotly 中显示多个图表。

fig = make_subplots(rows=math.ceil(len(file_names)/3),cols=3)

for j in range(len(file_names)):
    df_inst = df_list[j][df_list[j]["species"] == species_list[int(10)]] 

    fig.append_trace(
        px.scatter(
            x=df_inst[" VG"],
            y=df_inst[" ID"],
        ), row=1 + int(j / 3), col=1 + j % 3
    )
    fig.update_xaxes(type='linear' if xaxis_type == 'Linear' else 'log', 
                     row=1 + int(j / 3), col=1 + j % 3)

    fig.update_yaxes(type='linear' if yaxis_type == 'Linear' else 'log',
                     row=1 + int(j / 3), col=1 + j % 3)
    
fig.show()

错误消息。

ValueError: 
    Invalid element(s) received for the 'data' property of 
        Invalid elements include: [Figure({
    'data': [{'hovertemplate': 'x=%{x}<br>y=%{y}<extra></extra>',
              'legendgroup': '',
              'marker': {'color': '#636efa', 'symbol': 'circle'},
              'mode': 'markers',
              'name': '',
              'orientation': 'v',
              'showlegend': False,
              'type': 'scatter',
              'x': array([-0.5  , -0.498, -0.496, ...,  0.996,  0.998,  1.   ]),
              'xaxis': 'x',
              'y': array([ 1.8000e-13,  1.0200e-12, -1.6700e-12, ...,  1.5398e-06,  1.5725e-06,
                           1.5883e-06]),
              'yaxis': 'y'}],
    'layout': {'legend': {'tracegroupgap': 0},
               'margin': {'t': 60},
               'template': '...',
               'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'x'}},
               'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'y'}}}
})]

    The 'data' property is a tuple of trace instances
    that may be specified as:
      - A list or tuple of trace instances
        (e.g. [Scatter(...), Bar(...)])
      - A single trace instance
        (e.g. Scatter(...), Bar(...), etc.)
      - A list or tuple of dicts of string/value properties where:
        - The 'type' property specifies the trace type
            One of: ['area', 'bar', 'barpolar', 'box',
                     'candlestick', 'carpet', 'choropleth',
                     'choroplethmapbox', 'cone', 'contour',
                     'contourcarpet', 'densitymapbox', 'funnel',
                     'funnelarea', 'heatmap', 'heatmapgl',
                     'histogram', 'histogram2d',
                     'histogram2dcontour', 'image', 'indicator',
                     'isosurface', 'mesh3d', 'ohlc', 'parcats',
                     'parcoords', 'pie', 'pointcloud', 'sankey',
                     'scatter', 'scatter3d', 'scattercarpet',
                     'scattergeo', 'scattergl', 'scattermapbox',
                     'scatterpolar', 'scatterpolargl',
                     'scatterternary', 'splom', 'streamtube',
                     'sunburst', 'surface', 'table', 'treemap',
                     'violin', 'volume', 'waterfall']

        - All remaining properties are passed to the constructor of
          the specified trace type

        (e.g. [{'type': 'scatter', ...}, {'type': 'bar, ...}])

感谢您的阅读。

【问题讨论】:

    标签: python pandas plotly plotly.graph-objects


    【解决方案1】:

    根据the documentation on adding traces to subplotsadd_traceappend_trace 方法只接受graph_objects。因此,您的代码块:

    fig.append_trace(
        px.scatter(
            x=df_inst[" VG"],
            y=df_inst[" ID"],
        ), row=1 + int(j / 3), col=1 + j % 3
    )
    

    ... 应改为:

    fig.append_trace(
        go.Scatter(
            x=df_inst[" VG"],
            y=df_inst[" ID"],
        ), row=1 + int(j / 3), col=1 + j % 3
    )
    

    这是一个示例,我们可以在不同的子图上绘制一些随机生成的 DataFrame,每次添加一个 Scatter graph_objectsappend_trace

    import numpy as np
    import pandas as pd
    from plotly.subplots import make_subplots
    import plotly.express as px
    import plotly.graph_objects as go
    
    np.random.seed(42)
    
    df = pd.DataFrame(np.random.randint(0,100,size=(5, 2)), columns=list('AB'))
    
    fig = make_subplots(rows=2,cols=1)
    for row_idx, col_name in enumerate(df.columns):
    
        ## using px.scatter throws the same ValueError: 
        ## Invalid element(s) received for the 'data' property
    
        fig.append_trace(
            go.Scatter(
                x=list(range(5)),
                y=df[col_name]
            ),
            row=row_idx+1,col=1
        )
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 2016-05-25
      • 2010-12-01
      • 2013-11-13
      • 2014-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多