danjgale / surfplot

A package for publication-ready brain surface figures
https://surfplot.readthedocs.io/en/latest/
Other
50 stars 13 forks source link

TypeError: unhashable type: 'ListedColormap' #29

Open victoris93 opened 11 months ago

victoris93 commented 11 months ago

There seems to be a bug related to the matplotlib listed colormaps. I tried running your tutorial on regions and parcellations and the plotting cell threw this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
[/Users/VictoriaShevchenko/Downloads/plot_tutorial_06.ipynb](https://file+.vscode-resource.vscode-cdn.net/Users/VictoriaShevchenko/Downloads/plot_tutorial_06.ipynb) Cell 12 line 5
      [2](vscode-notebook-cell:/Users/VictoriaShevchenko/Downloads/plot_tutorial_06.ipynb#X13sZmlsZQ%3D%3D?line=1) p.add_layer(regions, cmap=cmap, cbar=False)
      [3](vscode-notebook-cell:/Users/VictoriaShevchenko/Downloads/plot_tutorial_06.ipynb#X13sZmlsZQ%3D%3D?line=2) p.add_layer(regions, cmap='gray', as_outline=True, cbar=False)
----> [5](vscode-notebook-cell:/Users/VictoriaShevchenko/Downloads/plot_tutorial_06.ipynb#X13sZmlsZQ%3D%3D?line=4) fig = p.build()
      [6](vscode-notebook-cell:/Users/VictoriaShevchenko/Downloads/plot_tutorial_06.ipynb#X13sZmlsZQ%3D%3D?line=5) fig.show()

File [~/.local/lib/python3.9/site-packages/surfplot/plotting.py:498](https://file+.vscode-resource.vscode-cdn.net/Users/VictoriaShevchenko/Downloads/~/.local/lib/python3.9/site-packages/surfplot/plotting.py:498), in Plot.build(self, figsize, colorbar, cbar_kws, scale)
    475 def build(self, figsize=None, colorbar=True, cbar_kws=None, scale=(2, 2)):
    476     """Build matplotlib figure of surface plot
    477 
    478     Parameters
   (...)
    496         Surface plot figure
    497     """
--> 498     p = self.render()
    499     p._check_offscreen()
    500     x = p.to_numpy(transparent_bg=True, scale=scale)

File [~/.local/lib/python3.9/site-packages/surfplot/plotting.py:391](https://file+.vscode-resource.vscode-cdn.net/Users/VictoriaShevchenko/Downloads/~/.local/lib/python3.9/site-packages/surfplot/plotting.py:391), in Plot.render(self, offscreen)
    388     cmap = [cmaps]
    389     color_range = [crange]
--> 391 return plot_surf(surfs=self.surfaces, layout=hemi_layout,
    392                  array_name=names, cmap=cmap, color_bar=False,
    393                  color_range=color_range, view=view_layout,
    394                  background=self.background, zoom=self.zoom,
    395                  nan_color=(0, 0, 0, 0), share=True,
    396                  label_text=self.label_text, size=self.size, 
    397                  return_plotter=True, offscreen=offscreen)

File [~/.local/lib/python3.9/site-packages/surfplot/surf.py:431](https://file+.vscode-resource.vscode-cdn.net/Users/VictoriaShevchenko/Downloads/~/.local/lib/python3.9/site-packages/surfplot/surf.py:431), in plot_surf(surfs, layout, array_name, view, color_bar, color_range, share, label_text, cmap, nan_color, zoom, background, size, embed_nb, interactive, scale, transparent_bg, screenshot, filename, return_plotter, **kwargs)
    428 if screenshot or embed_nb:
    429     kwargs.update({'offscreen': True})
--> 431 p = build_plotter(surfs, layout, array_name=array_name, view=view,
    432                   color_bar=color_bar, color_range=color_range,
    433                   share=share, label_text=label_text, cmap=cmap,
    434                   nan_color=nan_color, zoom=zoom, background=background,
    435                   size=size, **kwargs)
    436 if return_plotter:
    437     return p

File [~/.local/lib/python3.9/site-packages/surfplot/surf.py:286](https://file+.vscode-resource.vscode-cdn.net/Users/VictoriaShevchenko/Downloads/~/.local/lib/python3.9/site-packages/surfplot/surf.py:286), in build_plotter(surfs, layout, array_name, view, color_bar, color_range, share, label_text, cmap, nan_color, zoom, background, size, **kwargs)
    284 cm = cmap[i, j][ia]
    285 if cm is not None:
--> 286     if cm in colormaps:
    287         table = colormaps[cm]
    288     else:

TypeError: unhashable type: 'ListedColormap'

Any insight about the possible source of the problem?

danjgale commented 10 months ago

Hi @victoris93,

This seems related to #12. What version of matplotlib are you running? There were some versions that caused this, but IIRC the fix is to either downgrade the matplotlib version to 3.4.2 or update to the most recent matplotlib. Give that a shot, and if the problem persists, let me know.