【问题标题】:ValueError: Cannot accept list of column references or list of columns for both `x` and `y`ValueError:无法接受列引用列表或“x”和“y”的列列表
【发布时间】:2023-01-31 01:01:55
【问题描述】:

我无法在 Plotly 中获取热图并获取 ValueError: Cannot accept list of column references

def update_graph(xaxis_column_name, yaxis_column_name,value):
dff = df[df['Year'] == value]   

fig = px.density_heatmap(
                          
                          x=dff[dff['Population'] == xaxis_column_name]['Pop. Density (per sq. mi.)'],
                          y=dff[dff['Area (sq. mi.)'] == yaxis_column_name]['Pop. Density (per sq. mi.)'],
                          text_auto=True,
                          hover_name=dff[dff['Country'] == yaxis_column_name]['Region'])
return fig

【问题讨论】:

    标签: plotly-dash


    【解决方案1】:

    在尝试通过将数据帧传递给 px.line 来绘制 px.line 图表时,我遇到了同样的错误,而且我无法在网上找到解决我的特定问题的答案。我不确定你的问题是否和我的一样,但我发现当我试图将一个空数据帧传递给 px.line 时我的错误发生了——我传递给 px 的数据帧中的数据每次重新运行脚本时,折线图都会发生变化,有时数据框中没有数据,这就是为什么我有时会得到一个空数据框的原因。为了解决这个问题,我简单地写了一个 if 声明:如果我试图传递给 px.line 的数据框是空的,那么我将一个“虚拟”数据框传递给 px.line,它具有相同的列标题,但只有一行全为零的数据,否则,传递最初预期的数据帧,如下所示:

    # orig_df is the dataframe I am passing to px.line
    empty_df = orig_df.empty
    if empty_df = True:
        orig_df = pandas.DataFrame({'Col1':[0], 'Col2':[0], 'Col3':[0]}) # column names are same as in orig_df
    else:
        orig_df = orig_df
    

    它解决了我将动态数据框传递到 px.line 图表的所有问题。我希望这有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      相关资源
      最近更新 更多