首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实际上,添加跟踪时出现了值错误。

实际上,添加跟踪时出现了值错误。
EN

Stack Overflow用户
提问于 2022-08-31 17:31:31
回答 1查看 41关注 0票数 0

我正试图以巧妙的方式将数据添加到子图中,并且一直遇到一个值错误:

代码语言:javascript
复制
ValueError: 
    Invalid element(s) received for the 'data' property of 
        Invalid elements include: [Figure({
    'data': [{'hovertemplate': 'year=%{x}<br>value_sum=%{y}<extra></extra>',
              'legendgroup': '',
              'line': {'color': '#636efa', 'dash': 'solid'},
              'marker': {'symbol': 'circle'},
              'mode': 'lines',
              'name': '',
              'orientation': 'v',
              'showlegend': False,
              'type': 'scatter',
              'x': array(['2018', '2018', '2018', '2019', '2019', '2019', '2020', '2020', '2020',
                          '2021', '2021', '2021'], dtype=object),
              'xaxis': 'x',
              'y': array([1280, 1280, 1280,  747,  747,  747, 2596, 2596, 2596,  689,  689,  689],
                         dtype=int64),
              'yaxis': 'y'}],
    'layout': {'legend': {'tracegroupgap': 0},
               'margin': {'t': 60},
               'template': '...',
               'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'year'}},
               'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'value_sum'}}}
})]
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: ['bar', 'barpolar', 'box', 'candlestick',
                 'carpet', 'choropleth', 'choroplethmapbox',
                 'cone', 'contour', 'contourcarpet',
                 'densitymapbox', 'funnel', 'funnelarea',
                 'heatmap', 'heatmapgl', 'histogram',
                 'histogram2d', 'histogram2dcontour', 'icicle',
                 'image', 'indicator', 'isosurface', 'mesh3d',
                 'ohlc', 'parcats', 'parcoords', 'pie',
                 'pointcloud', 'sankey', 'scatter',
                 'scatter3d', 'scattercarpet', 'scattergeo',
                 'scattergl', 'scattermapbox', 'scatterpolar',
                 'scatterpolargl', 'scattersmith',
                 '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, ...}])

我已经尝试将数据转换为列表和元组,但是仍然会遇到这个错误。此外,如果我只是简单地绘制没有子图的线,它就能工作。我也尝试切换到plotly.graph_objects,但它给了我某种类型的模块错误。守则如下:

代码语言:javascript
复制
from io import StringIO
import pandas as pd
import plotly.express as px

data='''
    Use-Cases   2018    2019    2020    2021
0   Consumer    50      251     2123    210
1   Education   541     52      32      23
2   Government  689     444     441     456
'''

df = pd.read_csv(StringIO(data), sep='\s+')

# Go from a wide to long dataframe using melt
df = pd.melt(df, id_vars=[ 'Use-Cases'], value_vars=['2018', '2019', '2020', '2021'])
df = df.rename(columns={ 'variable': 'year'})

# get totals for each year so the percent calculation can be done
aggregated_df = df[[ 'value', 'year']].groupby(['year']).agg(['sum']).reset_index()
aggregated_df.columns = ['year', 'value_sum']
df = pd.merge(df, aggregated_df, on=['year'], how='left')

# Caclulate percents and format the column
df['percent'] = (df['value']/df['value_sum']*100).round(1).astype(str) + "%"
df

fig = make_subplots(rows=1, cols=2)
fig.add_trace(px.line(df,df.year,df.value_sum))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-31 19:20:21

最后一行应是:

代码语言:javascript
复制
fig.add_trace(px.line(df,df.year,df.value_sum).data[0])

  • add_trace()添加了一个跟踪,而不是一个图形。figure
  • .data返回一个px.line()是一个跟踪列表,因此[0]选择第一个跟踪
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73559774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档