简体   繁体   English

TypeError: unhashable type: 'set' in dropdown plotly python

[英]TypeError: unhashable type: 'set' in dropdown plotly python

The following code works by itself but I have a "TypeError: unhashable type: 'set'" when it's in my plotly graph.以下代码本身可以工作,但当它在我的 plotly 图中时,我有一个“TypeError:unhashable type:'set'”。 Can you help me out with this?你能帮我解决这个问题吗? I never worked with set or frozenset.我从未使用过 set 或 freezeset。

Thanks a lot !非常感谢 !

code that works be itself:有效的代码本身:

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)

code inside my plotly graph:我的 plotly 图中的代码:

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

This error has nothing to do with Plotly.此错误与 Plotly 无关。 You can't have a set (literal) as a key in a dict because a set isn't hashable.您不能将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

And you don't need a set there, so just replace {"title"} with "title" (a hashable str ).而且你不需要在那里设置,所以只需将{"title"}替换为"title" (一个可散列的str )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM