colorbar with date-time formatting

Hi,
I've got data in terms of day numbers, and wish to plot it as a 2D
scatter plot, with the color denoting the day.

I'd like the colorbar to be annotated in Apr 7 type format...

I've tried the following code without success.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter,DayLocator
from matplotlib import cm

data = np.arange(24.)+700000.
x = np.random.rand(24)
y = np.random.rand(24)
fig = plt.figure()
ax = fig.add_subplot(111)
smap = ax.scatter(x,y,s=10,c=data,edgecolors='none',marker='o',cmap=cm.jet)
cb = fig.colorbar(smap,orientation='horizontal',shrink=0.7)
cax=cb.ax
cax.xaxis_date()
cax.xaxis.set_major_locator(DayLocator())
cax.xaxis.set_major_formatter(DateFormatter('%b %d'))
plt.show()

Any ideas on how to do this would be appreciated.

George Nurser.

The code fails with the trace
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backends/backend_wx.pyc
in _onPaint(self, evt)
   1154 drawDC = wx.PaintDC(self)
   1155 if not self._isDrawn:
-> 1156 self.draw(drawDC=drawDC)
   1157 else:
   1158 self.gui_repaint(drawDC=drawDC)

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backends/backend_wxagg.pyc
in draw(self, drawDC)
     57 """
     58 DEBUG_MSG("draw()", 1, self)
---> 59 FigureCanvasAgg.draw(self)
     60
     61 self.bitmap =
_convert_agg_to_wx_bitmap(self.get_renderer(), None)

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backends/backend_agg.pyc
in draw(self)
    386
    387 self.renderer = self.get_renderer()
--> 388 self.figure.draw(self.renderer)
    389
    390 def get_renderer(self):

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/artist.pyc
in draw_wrapper(artist, renderer, *kl)
     53 def draw_wrapper(artist, renderer, *kl):
     54 before(artist, renderer)
---> 55 draw(artist, renderer, *kl)
     56 after(artist, renderer)
     57

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/figure.pyc
in draw(self, renderer)
    770
    771 # render the axes
--> 772 for a in self.axes: a.draw(renderer)
    773
    774 # render the figure text

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/artist.pyc
in draw_wrapper(artist, renderer, *kl)
     53 def draw_wrapper(artist, renderer, *kl):
     54 before(artist, renderer)
---> 55 draw(artist, renderer, *kl)
     56 after(artist, renderer)
     57

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axes.pyc
in draw(self, renderer, inframe)
   1759
   1760 for zorder, i, a in dsu:
-> 1761 a.draw(renderer)
   1762
   1763 renderer.close_group('axes')

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/artist.pyc
in draw_wrapper(artist, renderer, *kl)
     53 def draw_wrapper(artist, renderer, *kl):
     54 before(artist, renderer)
---> 55 draw(artist, renderer, *kl)
     56 after(artist, renderer)
     57

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axis.pyc
in draw(self, renderer, *args, **kwargs)
    747 renderer.open_group(__name__)
    748 interval = self.get_view_interval()
--> 749 for tick, loc, label in self.iter_ticks():
    750 if tick is None: continue
    751 if not mtransforms.interval_contains(interval,
loc): continue

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axis.pyc
in iter_ticks(self)
    688 Iterate through all of the major and minor ticks.
    689 """
--> 690 majorLocs = self.major.locator()
    691 majorTicks = self.get_major_ticks(len(majorLocs))
    692 self.major.formatter.set_locs(majorLocs)

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/dates.pyc
in __call__(self)
    530 self.rule.set(dtstart=start, until=stop)
    531 dates = self.rule.between(dmin, dmax, True)
--> 532 return self.raise_if_exceeds(date2num(dates))
    533
    534 def _get_unit(self):

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/ticker.pyc
in raise_if_exceeds(self, locs)
    680 'raise a RuntimeError if Locator attempts to create
more than MAXTICKS locs'
    681 if len(locs)>=self.MAXTICKS:
--> 682 raise RuntimeError('Locator attempting to generate
%d ticks from %s to %s: exceeds Locator.MAXTICKS'%(len(locs), locs[0],
locs[-1]))
    683
    684 return locs

RuntimeError: Locator attempting to generate 3654 ticks from 730120.0
to 733773.0: exceeds Locator.MAXTICKS

Colorbar axes is a rather special and things need to be set during the
initialization.

Here is a slightly modified version of your script.

While it does not produces error, I'm not sure if the result is correct.

-JJ

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter,DayLocator
from matplotlib import cm

data = np.arange(24.)+700000.
x = np.random.rand(24)
y = np.random.rand(24)
fig = plt.figure()
ax = fig.add_subplot(111)
smap = ax.scatter(x,y,s=10,c=data,edgecolors='none',marker='o',cmap=cm.jet)

cb = fig.colorbar(smap,orientation='horizontal',shrink=0.7,
                  ticks=DayLocator(interval=5),
                  format=DateFormatter('%b %d'))
plt.show()

···

On Thu, Jan 14, 2010 at 3:04 PM, George Nurser <gnurser@...982...> wrote:

Hi,
I've got data in terms of day numbers, and wish to plot it as a 2D
scatter plot, with the color denoting the day.

I'd like the colorbar to be annotated in Apr 7 type format...

I've tried the following code without success.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter,DayLocator
from matplotlib import cm

data = np.arange(24.)+700000.
x = np.random.rand(24)
y = np.random.rand(24)
fig = plt.figure()
ax = fig.add_subplot(111)
smap = ax.scatter(x,y,s=10,c=data,edgecolors='none',marker='o',cmap=cm.jet)
cb = fig.colorbar(smap,orientation='horizontal',shrink=0.7)
cax=cb.ax
cax.xaxis_date()
cax.xaxis.set_major_locator(DayLocator())
cax.xaxis.set_major_formatter(DateFormatter('%b %d'))
plt.show()

Any ideas on how to do this would be appreciated.

George Nurser.

The code fails with the trace
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backends/backend_wx.pyc
in _onPaint(self, evt)
1154 drawDC = wx.PaintDC(self)
1155 if not self._isDrawn:
-> 1156 self.draw(drawDC=drawDC)
1157 else:
1158 self.gui_repaint(drawDC=drawDC)

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backends/backend_wxagg.pyc
in draw(self, drawDC)
57 """
58 DEBUG_MSG("draw()", 1, self)
---> 59 FigureCanvasAgg.draw(self)
60
61 self.bitmap =
_convert_agg_to_wx_bitmap(self.get_renderer(), None)

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backends/backend_agg.pyc
in draw(self)
386
387 self.renderer = self.get_renderer()
--> 388 self.figure.draw(self.renderer)
389
390 def get_renderer(self):

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/artist.pyc
in draw_wrapper(artist, renderer, *kl)
53 def draw_wrapper(artist, renderer, *kl):
54 before(artist, renderer)
---> 55 draw(artist, renderer, *kl)
56 after(artist, renderer)
57

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/figure.pyc
in draw(self, renderer)
770
771 # render the axes
--> 772 for a in self.axes: a.draw(renderer)
773
774 # render the figure text

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/artist.pyc
in draw_wrapper(artist, renderer, *kl)
53 def draw_wrapper(artist, renderer, *kl):
54 before(artist, renderer)
---> 55 draw(artist, renderer, *kl)
56 after(artist, renderer)
57

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axes.pyc
in draw(self, renderer, inframe)
1759
1760 for zorder, i, a in dsu:
-> 1761 a.draw(renderer)
1762
1763 renderer.close_group('axes')

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/artist.pyc
in draw_wrapper(artist, renderer, *kl)
53 def draw_wrapper(artist, renderer, *kl):
54 before(artist, renderer)
---> 55 draw(artist, renderer, *kl)
56 after(artist, renderer)
57

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axis.pyc
in draw(self, renderer, *args, **kwargs)
747 renderer.open_group(__name__)
748 interval = self.get_view_interval()
--> 749 for tick, loc, label in self.iter_ticks():
750 if tick is None: continue
751 if not mtransforms.interval_contains(interval,
loc): continue

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axis.pyc
in iter_ticks(self)
688 Iterate through all of the major and minor ticks.
689 """
--> 690 majorLocs = self.major.locator()
691 majorTicks = self.get_major_ticks(len(majorLocs))
692 self.major.formatter.set_locs(majorLocs)

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/dates.pyc
in __call__(self)
530 self.rule.set(dtstart=start, until=stop)
531 dates = self.rule.between(dmin, dmax, True)
--> 532 return self.raise_if_exceeds(date2num(dates))
533
534 def _get_unit(self):

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/ticker.pyc
in raise_if_exceeds(self, locs)
680 'raise a RuntimeError if Locator attempts to create
more than MAXTICKS locs'
681 if len(locs)>=self.MAXTICKS:
--> 682 raise RuntimeError('Locator attempting to generate
%d ticks from %s to %s: exceeds Locator.MAXTICKS'%(len(locs), locs[0],
locs[-1]))
683
684 return locs

RuntimeError: Locator attempting to generate 3654 ticks from 730120.0
to 733773.0: exceeds Locator.MAXTICKS

------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users