【问题标题】:TypeError: unhashable type: 'set' in dropdown plotly pythonTypeError:不可散列的类型:下拉菜单中的'set' plotly python
【发布时间】:2021-08-10 12:24:47
【问题描述】:

以下代码可以自行运行,但是当它在我的绘图图中时,我有一个“TypeError: unhashable type: 'set'”。你能帮我解决这个问题吗?我从未使用过 set 或 freezeset。

非常感谢!

可以自己运行的代码:

updatemenus = []
for n, ecole in enumerate(liste_ecole):
  visible = [False]*len(liste_ecole)
  visible[n] = True
  temp_dict = dict(label = str(liste_ecole),
                     method = 'update',
                     args = [{"visible" : visible,
                             {"title"} : "Prévisions des effectifs pour {}".format(liste_ecole)}])
updatemenus.append(temp_dict)

我的绘图图中的代码:

fig = go.Figure([
    go.Scatter(
        name='Effectif réel',
        x=df2['date_str'],
        y=df2['reel'],
        mode='markers+lines',
        marker=dict(color="#1e3d59"),
        line=dict(width=1),
        showlegend=True,
        text=df2['jour']
    ),
    go.Scatter(
        name='Prévision algorithme',
        x=df2['date_str'],
        y=df2['output'],
        mode='markers+lines',
        marker=dict(color="#ff6e40", size=4),
        line=dict(width=2),
        showlegend=True,
        text=df2['jour']
    )
])
fig.layout.plot_bgcolor = '#f5f0e1'


updatemenus = []
for n, ecole in enumerate(liste_ecole):
  visible = [False]*len(liste_ecole)
  visible[n] = True
  temp_dict = dict(label = str(liste_ecole),
                     method = 'update',
                     args = [{"visible" : visible,
                             {"title"} : "Prévisions des effectifs pour {}".format(liste_ecole)}]) **#THIS IS WHERE I GOT MY ERROR**
updatemenus.append(temp_dict)


fig.update_layout(
    updatemenus=list([dict(buttons= list_updatemenus)]),
    yaxis_title="Nombre de convives",
    hovermode="x",

    direction="down",
    pad={"r": 10, "t": 10},
    showactive=True,
    x=1.02,
    xanchor="left",
    y=0.75,
    yanchor="top"
    )
                 
fig.update_xaxes(tickformat='%d-%b-%Y')    
fig.update_xaxes(rangeslider_visible=True)

fig.show()

【问题讨论】:

    标签: python set plotly frozenset


    【解决方案1】:

    此错误与 Plotly 无关。你不能有set(字面意思) 作为 dict 中的键,因为 set 不可散列。

    In [1]: {
       ...:     "visible" : ...,
       ...:     {"title"} : "Prévisions des effectifs pour {}".format(...)
       ...: }
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-1-24fea1201dd1> in <module>
          1 {
          2     "visible" : ...,
    ----> 3     {"title"} : "Prévisions des effectifs pour {}".format(...)
          4 }
    
    TypeError: unhashable type: 'set'
    
    In [2]: type({"title"})
    Out[2]: set
    

    而且您不需要在那里设置集合,因此只需将 {"title"} 替换为 "title"(可散列的 str)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      • 2013-10-22
      • 2013-01-23
      • 2018-03-13
      • 1970-01-01
      • 2018-12-26
      • 2020-05-22
      相关资源
      最近更新 更多