Skip to content

Exception in Tkinter/to_rgb with new colormaps #6025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ocehugo opened this issue Feb 18, 2016 · 3 comments
Closed

Exception in Tkinter/to_rgb with new colormaps #6025

ocehugo opened this issue Feb 18, 2016 · 3 comments
Assignees
Milestone

Comments

@ocehugo
Copy link

ocehugo commented Feb 18, 2016

python3.5.1
matplotlib 1.5.1
jupyter 4.0.6

matplotlib.pyplot cannot render figures with new colormaps in discretized mode ('viridis' and friends).

with 'jet' and friends exception is not raised

How to reproduce:

import matplotlib.pyplot as plt
import numpy as np
a = np.random.randn(20,20)
cmap = plt.get_cmap('viridis',20)
plt.pcolormesh(a,cmap=cmap)

raises:

<matplotlib.collections.QuadMesh at 0x11a767198>

In [6]: Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/matplotlib/colors.py", line 368, in to_rgba
    'length of rgba sequence should be either 3 or 4')
ValueError: length of rgba sequence should be either 3 or 4

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/matplotlib/colors.py", line 399, in to_rgba_array
    return np.array([self.to_rgba(c, alpha)], dtype=np.float)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/colors.py", line 376, in to_rgba
    'to_rgba: Invalid rgba arg "%s"\n%s' % (str(arg), exc))
ValueError: to_rgba: Invalid rgba arg "['viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis', 'viridis']"
length of rgba sequence should be either 3 or 4

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/matplotlib/colors.py", line 306, in to_rgb
    fl = float(argl)
ValueError: could not convert string to float: 'viridis'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/matplotlib/colors.py", line 370, in to_rgba
    r, g, b = self.to_rgb(arg)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/colors.py", line 328, in to_rgb
    'to_rgb: Invalid rgb arg "%s"\n%s' % (str(arg), exc))
ValueError: to_rgb: Invalid rgb arg "viridis"
could not convert string to float: 'viridis'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1549, in __call__
    return self.func(*args)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 596, in callit
    func(*args)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/backend_tkagg.py", line 373, in idle_draw
    self.draw()
  File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/backend_tkagg.py", line 354, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py", line 474, in draw
    self.figure.draw(self.renderer)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/figure.py", line 1159, in draw
    func(*args)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 2324, in draw
    a.draw(renderer)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/collections.py", line 1847, in draw
    self.update_scalarmappable()
  File "/usr/local/lib/python3.5/site-packages/matplotlib/collections.py", line 713, in update_scalarmappable
    self._facecolors = self.to_rgba(self._A, self._alpha)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/cm.py", line 262, in to_rgba
    x = self.cmap(x, alpha=alpha, bytes=bytes)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/colors.py", line 550, in __call__
    self._init()
  File "/usr/local/lib/python3.5/site-packages/matplotlib/colors.py", line 845, in _init
    rgba = colorConverter.to_rgba_array(self.colors)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/colors.py", line 422, in to_rgba_array
    result[i] = self.to_rgba(cc, alpha)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/colors.py", line 376, in to_rgba
    'to_rgba: Invalid rgba arg "%s"\n%s' % (str(arg), exc))
ValueError: to_rgba: Invalid rgba arg "viridis"
to_rgb: Invalid rgb arg "viridis"
could not convert string to float: 'viridis'

@ocehugo ocehugo changed the title Exception in Tkinter with new colormaps Exception in Tkinter/to_rgb with new colormaps Feb 18, 2016
@tacaswell tacaswell added this to the 1.5.2 (Critical bug fix release) milestone Feb 18, 2016
@tacaswell
Copy link
Member

Attn @efiring I am pretty sure this is a duplicate?

@tacaswell
Copy link
Member

I think the _resample method of ListedColormap is just wrong

The current state came from https://github.com/matplotlib/matplotlib/pull/5283/files but passes arguments that do not make sense to the ListedColormap __init__.

A more minimal example

plt.get_cmap('viridis',20)(.5)

@efiring
Copy link
Member

efiring commented Feb 19, 2016

Yes, _resample is wrong. It should return something like

    ListedColormap(self(np.linspace(0, 1, lutsize)), name=self.name + str(lutsize))`

@efiring efiring self-assigned this Feb 20, 2016
tacaswell added a commit that referenced this issue Feb 21, 2016
BUG: fix ListedColormap._resample, hence plt.get_cmap; closes #6025
tacaswell added a commit that referenced this issue Feb 21, 2016
BUG: fix ListedColormap._resample, hence plt.get_cmap; closes #6025
tacaswell added a commit to tacaswell/matplotlib that referenced this issue May 22, 2016
BUG: fix ListedColormap._resample, hence plt.get_cmap; closes matplotlib#6025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants