簡體   English   中英

我似乎無法讓 plotly 顯示多個圖表

[英]I can't seem to get plotly to display multiple graphs

我想在 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, ...}])

感謝您的閱讀。

根據關於向子圖添加跟蹤的文檔add_traceappend_trace方法只接受 plotly 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
)

這是一個示例,我們可以在 plot 一些隨機生成的 DataFrame 在不同的子圖上使用append_trace graph_objects

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

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM