colorbar with date

Hi all,

try to make a colobar with date type data is causing a type error. Is
there a solution?

Any help is wellcome
Elmar

Python 3
np_version : 1.9.2
pd version : 0.16.2
mpl.version: 1.4.3

···

###########################
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

N = 50
df = pd.DataFrame({
         'x': np.random.randn(N),
         'y': np.random.randn(N),
         'z': pd.date_range(start='2015-01-01', periods=N, freq='D')})

fig, ax = plt.subplots()
smap = ax.scatter(df.x, df.y, s=50, c=df.z)
cb = plt.colorbar(smap)
plt.show()

###########################

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-75-8c2abb81cdb2> in <module>()
       1 fig, ax = plt.subplots()
       2 smap = ax.scatter(df2.x, df2.y, s=50, c=df2.z)
----> 3 cb = plt.colorbar(smap)

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/pyplot.py
in colorbar(mappable, cax, ax, **kw)
    2157 ax = gca()
    2158
-> 2159 ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
    2160 draw_if_interactive()
    2161 return ret

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/figure.py
in colorbar(self, mappable, cax, ax, use_gridspec, **kw)
    1504 cax, kw = cbar.make_axes(ax, **kw)
    1505 cax.hold(True)
-> 1506 cb = cbar.colorbar_factory(cax, mappable, **kw)
    1507
    1508 self.sca(current_ax)

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/colorbar.py
in colorbar_factory(cax, mappable, **kwargs)
    1316 cb = ColorbarPatch(cax, mappable, **kwargs)
    1317 else:
-> 1318 cb = Colorbar(cax, mappable, **kwargs)
    1319
    1320 cid = mappable.callbacksSM.connect('changed',
cb.on_mappable_changed)

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/colorbar.py
in __init__(self, ax, mappable, **kw)
     892 kw['alpha'] = mappable.get_alpha()
     893
--> 894 ColorbarBase.__init__(self, ax, **kw)
     895
     896 def on_mappable_changed(self, mappable):

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/colorbar.py
in __init__(self, ax, cmap, norm, alpha, values, boundaries,
orientation, ticklocation, extend, spacing, ticks, format, drawedges,
filled, extendfrac, extendrect, label)
     319 # The rest is in a method so we can recalculate when
clim changes.
     320 self.config_axis()
--> 321 self.draw_all()
     322
     323 def _extend_lower(self):

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/colorbar.py
in draw_all(self)
     340 and do all the drawing.
     341 '''
--> 342 self._process_values()
     343 self._find_range()
     344 X, Y = self._mesh()

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/colorbar.py
in _process_values(self, b)
     660 self.norm.vmin, self.norm.vmax =
mtrans.nonsingular(self.norm.vmin,
     661
  self.norm.vmax,
--> 662
expander=0.1)
     663
     664 b = self.norm.inverse(self._uniform_y(self.cmap.N + 1))

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/transforms.py
in nonsingular(vmin, vmax, expander, tiny, increasing)
    2681 returns -*expander*, *expander*.
    2682 '''
-> 2683 if (not np.isfinite(vmin)) or (not np.isfinite(vmax)):
    2684 return -expander, expander
    2685 swapped = False

TypeError: ufunc 'isfinite' not supported for the input types, and the
inputs could not be safely coerced to any supported types according to
the casting rule ''safe''

found a solution

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, date2num

N = 50
df = pd.DataFrame({
           'x': np.random.randn(N),
           'y': np.random.randn(N),
           'z': pd.date_range(start='2015-01-01', periods=N, freq='D')})

fig, ax = plt.subplots()

smap = ax.scatter(df2.x, df2.y, s=50,
                   c=[date2num(i.date()) for i in df2.z]) # <==

cb = fig.colorbar(smap, orientation='vertical', shrink=0.9,
                   format=DateFormatter('%d %b %y')) # <==

plt.show()

···

On 06.09.2015 17:55, elmar werling wrote:

Hi all,

try to make a colobar with date type data is causing a type error. Is
there a solution?

Any help is wellcome
Elmar

Python 3
np_version : 1.9.2
pd version : 0.16.2
mpl.version: 1.4.3

###########################
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

N = 50
df = pd.DataFrame({
         'x': np.random.randn(N),
         'y': np.random.randn(N),
         'z': pd.date_range(start='2015-01-01', periods=N, freq='D')})

fig, ax = plt.subplots()
smap = ax.scatter(df.x, df.y, s=50, c=df.z)
cb = plt.colorbar(smap)
plt.show()

###########################

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-75-8c2abb81cdb2> in <module>()
       1 fig, ax = plt.subplots()
       2 smap = ax.scatter(df2.x, df2.y, s=50, c=df2.z)
----> 3 cb = plt.colorbar(smap)

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/pyplot.py
in colorbar(mappable, cax, ax, **kw)
    2157 ax = gca()
    2158
-> 2159 ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
    2160 draw_if_interactive()
    2161 return ret

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/figure.py
in colorbar(self, mappable, cax, ax, use_gridspec, **kw)
    1504 cax, kw = cbar.make_axes(ax, **kw)
    1505 cax.hold(True)
-> 1506 cb = cbar.colorbar_factory(cax, mappable, **kw)
    1507
    1508 self.sca(current_ax)

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/colorbar.py
in colorbar_factory(cax, mappable, **kwargs)
    1316 cb = ColorbarPatch(cax, mappable, **kwargs)
    1317 else:
-> 1318 cb = Colorbar(cax, mappable, **kwargs)
    1319
    1320 cid = mappable.callbacksSM.connect('changed',
cb.on_mappable_changed)

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/colorbar.py
in __init__(self, ax, mappable, **kw)
     892 kw['alpha'] = mappable.get_alpha()
     893
--> 894 ColorbarBase.__init__(self, ax, **kw)
     895
     896 def on_mappable_changed(self, mappable):

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/colorbar.py
in __init__(self, ax, cmap, norm, alpha, values, boundaries,
orientation, ticklocation, extend, spacing, ticks, format, drawedges,
filled, extendfrac, extendrect, label)
     319 # The rest is in a method so we can recalculate when
clim changes.
     320 self.config_axis()
--> 321 self.draw_all()
     322
     323 def _extend_lower(self):

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/colorbar.py
in draw_all(self)
     340 and do all the drawing.
     341 '''
--> 342 self._process_values()
     343 self._find_range()
     344 X, Y = self._mesh()

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/colorbar.py
in _process_values(self, b)
     660 self.norm.vmin, self.norm.vmax =
mtrans.nonsingular(self.norm.vmin,
     661 self.norm.vmax,
--> 662 expander=0.1)
     663
     664 b = self.norm.inverse(self._uniform_y(self.cmap.N +
1))

/home/elmar/anaconda3/envs/py3/lib/python3.4/site-packages/matplotlib/transforms.py
in nonsingular(vmin, vmax, expander, tiny, increasing)
    2681 returns -*expander*, *expander*.
    2682 '''
-> 2683 if (not np.isfinite(vmin)) or (not np.isfinite(vmax)):
    2684 return -expander, expander
    2685 swapped = False

TypeError: ufunc 'isfinite' not supported for the input types, and the
inputs could not be safely coerced to any supported types according to
the casting rule ''safe''