On Wed, Mar 23, 2011 at 8:43 AM, Sean Lake <odysseus9...@gmail.com> wrote:

> Hello all,
>
> I need to draw different hatch styles on the different groups of a stacked
> bar chart in order to make a figure that works properly in black and white.
> The devil is, I can't figure out how to do it. When I pass the hatch option
> a list of values, I get an error.
>



Looks like the 'hatch' keyword must be a single string, not a list.  You can
set the hatch style of the patches in the plot after calling hist():

-----
from numpy import *
import matplotlib.pyplot as plt


hatches = ['/', '+', '*', '\\', 'x', '.', '-', 'o']

result = plt.hist( [ linspace(1, 10, 20), linspace(1, 10, 40)],
bins=len(hatches), histtype="barstacked", rwidth=1.0, label=["a", "b"],
       color=["red", "green"])

plist1 = result[2][0]
plist2 = result[2][1]

for h, p1, p2 in zip(hatches, plist1, plist2):
    p1.set_hatch(h)
    p2.set_hatch(h)

plt.show()

-----

Warren



> A simple example:
> from numpy import *
> import matplotlib.pyplot as plt
>
> plt.hist( [ linspace(1, 10, 20), linspace(1, 10, 40)], bins=range(10),
> histtype="barstacked", rwidth=1.0, label=["a", "b"],
>        color=["red", "green"], hatch=["/", "x"] )
> plt.show()
>
> The resulting errors/outputs are at the end of the email.
>
> Thanks in advance for any help,
> Sean Lake
>
> In [4]: plt.hist( [ linspace(1, 10, 20), linspace(1, 10, 40)],
> bins=range(10), histtype
> ="barstacked", rwidth=1.0, label=["a", "b"],
>   ...:         color=["red", "green"], hatch=["/", "x"] )
> Out[4]:
> ([array([0, 3, 2, 2, 2, 2, 2, 2, 2]), array([0, 5, 4, 4, 5, 4, 4, 5, 4])],
>  array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
>  <a list of 2 Lists of Patches objects>)
>
> In [5]: plt.show()
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
>
> /sw/lib/python2.6/site-packages/matplotlib/backends/backend_gtk.pyc in
> expose_event(self, widget, event)
>    391                 x, y, w, h = self.allocation
>    392                 self._pixmap_prepare (w, h)
> --> 393                 self._render_figure(self._pixmap, w, h)
>    394                 self._need_redraw = False
>    395
>
> /sw/lib/python2.6/site-packages/matplotlib/backends/backend_gtkagg.pyc in
> _render_figure(self, pixmap, width, height)
>     73     def _render_figure(self, pixmap, width, height):
>     74         if DEBUG: print 'FigureCanvasGTKAgg.render_figure'
> ---> 75         FigureCanvasAgg.draw(self)
>     76         if DEBUG: print 'FigureCanvasGTKAgg.render_figure pixmap',
> pixmap
>     77         #agg_to_gtk_drawable(pixmap, self.renderer._renderer, None)
>
>
> /sw/lib/python2.6/site-packages/matplotlib/backends/backend_agg.pyc in
> draw(self)
>    392
>    393         self.renderer = self.get_renderer()
> --> 394         self.figure.draw(self.renderer)
>    395
>    396     def get_renderer(self):
>
> /sw/lib/python2.6/site-packages/matplotlib/artist.pyc in
> draw_wrapper(artist, renderer, *args, **kwargs)
>     53     def draw_wrapper(artist, renderer, *args, **kwargs):
>     54         before(artist, renderer)
> ---> 55         draw(artist, renderer, *args, **kwargs)
>     56         after(artist, renderer)
>     57
>
> /sw/lib/python2.6/site-packages/matplotlib/figure.pyc in draw(self,
> renderer)
>    796         dsu.sort(key=itemgetter(0))
>    797         for zorder, func, args in dsu:
> --> 798             func(*args)
>    799
>    800         renderer.close_group('figure')
>
> /sw/lib/python2.6/site-packages/matplotlib/artist.pyc in
> draw_wrapper(artist, renderer, *args, **kwargs)
>     53     def draw_wrapper(artist, renderer, *args, **kwargs):
>     54         before(artist, renderer)
> ---> 55         draw(artist, renderer, *args, **kwargs)
>     56         after(artist, renderer)
>     57
>
> /sw/lib/python2.6/site-packages/matplotlib/axes.pyc in draw(self, renderer,
> inframe)
>   1932
>   1933         for zorder, a in dsu:
> -> 1934             a.draw(renderer)
>   1935
>   1936         renderer.close_group('axes')
>
> /sw/lib/python2.6/site-packages/matplotlib/artist.pyc in
> draw_wrapper(artist, renderer, *args, **kwargs)
>     53     def draw_wrapper(artist, renderer, *args, **kwargs):
>     54         before(artist, renderer)
> ---> 55         draw(artist, renderer, *args, **kwargs)
>     56         after(artist, renderer)
>     57
>
> /sw/lib/python2.6/site-packages/matplotlib/patches.pyc in draw(self,
> renderer)
>    381                 path_effect.draw_path(renderer, gc, tpath, affine,
> rgbFace)
>    382         else:
> --> 383             renderer.draw_path(gc, tpath, affine, rgbFace)
>    384
>    385         gc.restore()
>
> /sw/lib/python2.6/site-packages/matplotlib/backends/backend_agg.pyc in
> draw_path(self, gc, path, transform, rgbFace)
>    115                 self._renderer.draw_path(gc, p, transform, rgbFace)
>    116         else:
> --> 117             self._renderer.draw_path(gc, path, transform, rgbFace)
>    118
>    119     def draw_mathtext(self, gc, x, y, s, prop, angle):
>
> /sw/lib/python2.6/site-packages/matplotlib/backend_bases.pyc in
> get_hatch_path(self, density)
>    865         if self._hatch is None:
>    866             return None
> --> 867         return Path.hatch(self._hatch, density)
>    868
>    869
>
> /sw/lib/python2.6/site-packages/matplotlib/path.pyc in hatch(cls,
> hatchpattern, density)
>    662             return None
>    663
> --> 664         hatch_path = cls._hatch_dict.get((hatchpattern, density))
>    665         if hatch_path is not None:
>    666             return hatch_path
>
> TypeError: unhashable type: 'list'
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
>
> /sw/lib/python2.6/site-packages/matplotlib/backends/backend_gtk.pyc in
> expose_event(self, widget, event)
>    391                 x, y, w, h = self.allocation
>    392                 self._pixmap_prepare (w, h)
> --> 393                 self._render_figure(self._pixmap, w, h)
>    394                 self._need_redraw = False
>    395
>
> /sw/lib/python2.6/site-packages/matplotlib/backends/backend_gtkagg.pyc in
> _render_figure(self, pixmap, width, height)
>     73     def _render_figure(self, pixmap, width, height):
>     74         if DEBUG: print 'FigureCanvasGTKAgg.render_figure'
> ---> 75         FigureCanvasAgg.draw(self)
>     76         if DEBUG: print 'FigureCanvasGTKAgg.render_figure pixmap',
> pixmap
>     77         #agg_to_gtk_drawable(pixmap, self.renderer._renderer, None)
>
>
> /sw/lib/python2.6/site-packages/matplotlib/backends/backend_agg.pyc in
> draw(self)
>    392
>    393         self.renderer = self.get_renderer()
> --> 394         self.figure.draw(self.renderer)
>    395
>    396     def get_renderer(self):
>
> /sw/lib/python2.6/site-packages/matplotlib/artist.pyc in
> draw_wrapper(artist, renderer, *args, **kwargs)
>     53     def draw_wrapper(artist, renderer, *args, **kwargs):
>     54         before(artist, renderer)
> ---> 55         draw(artist, renderer, *args, **kwargs)
>     56         after(artist, renderer)
>     57
>
> /sw/lib/python2.6/site-packages/matplotlib/figure.pyc in draw(self,
> renderer)
>    796         dsu.sort(key=itemgetter(0))
>    797         for zorder, func, args in dsu:
> --> 798             func(*args)
>    799
>    800         renderer.close_group('figure')
>
> /sw/lib/python2.6/site-packages/matplotlib/artist.pyc in
> draw_wrapper(artist, renderer, *args, **kwargs)
>     53     def draw_wrapper(artist, renderer, *args, **kwargs):
>     54         before(artist, renderer)
> ---> 55         draw(artist, renderer, *args, **kwargs)
>     56         after(artist, renderer)
>     57
>
> /sw/lib/python2.6/site-packages/matplotlib/axes.pyc in draw(self, renderer,
> inframe)
>   1932
>   1933         for zorder, a in dsu:
> -> 1934             a.draw(renderer)
>   1935
>   1936         renderer.close_group('axes')
>
> /sw/lib/python2.6/site-packages/matplotlib/artist.pyc in
> draw_wrapper(artist, renderer, *args, **kwargs)
>     53     def draw_wrapper(artist, renderer, *args, **kwargs):
>     54         before(artist, renderer)
> ---> 55         draw(artist, renderer, *args, **kwargs)
>     56         after(artist, renderer)
>     57
>
> /sw/lib/python2.6/site-packages/matplotlib/patches.pyc in draw(self,
> renderer)
>    381                 path_effect.draw_path(renderer, gc, tpath, affine,
> rgbFace)
>    382         else:
> --> 383             renderer.draw_path(gc, tpath, affine, rgbFace)
>    384
>    385         gc.restore()
>
> /sw/lib/python2.6/site-packages/matplotlib/backends/backend_agg.pyc in
> draw_path(self, gc, path, transform, rgbFace)
>    115                 self._renderer.draw_path(gc, p, transform, rgbFace)
>    116         else:
> --> 117             self._renderer.draw_path(gc, path, transform, rgbFace)
>    118
>    119     def draw_mathtext(self, gc, x, y, s, prop, angle):
>
> /sw/lib/python2.6/site-packages/matplotlib/backend_bases.pyc in
> get_hatch_path(self, density)
>    865         if self._hatch is None:
>    866             return None
> --> 867         return Path.hatch(self._hatch, density)
>    868
>    869
>
> /sw/lib/python2.6/site-packages/matplotlib/path.pyc in hatch(cls,
> hatchpattern, density)
>    662             return None
>    663
> --> 664         hatch_path = cls._hatch_dict.get((hatchpattern, density))
>    665         if hatch_path is not None:
>    666             return hatch_path
>
> TypeError: unhashable type: 'list'
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
>
> /sw/lib/python2.6/site-packages/matplotlib/backends/backend_gtk.pyc in
> expose_event(self, widget, event)
>    391                 x, y, w, h = self.allocation
>    392                 self._pixmap_prepare (w, h)
> --> 393                 self._render_figure(self._pixmap, w, h)
>    394                 self._need_redraw = False
>    395
>
> /sw/lib/python2.6/site-packages/matplotlib/backends/backend_gtkagg.pyc in
> _render_figure(self, pixmap, width, height)
>     73     def _render_figure(self, pixmap, width, height):
>     74         if DEBUG: print 'FigureCanvasGTKAgg.render_figure'
> ---> 75         FigureCanvasAgg.draw(self)
>     76         if DEBUG: print 'FigureCanvasGTKAgg.render_figure pixmap',
> pixmap
>     77         #agg_to_gtk_drawable(pixmap, self.renderer._renderer, None)
>
>
> /sw/lib/python2.6/site-packages/matplotlib/backends/backend_agg.pyc in
> draw(self)
>    392
>    393         self.renderer = self.get_renderer()
> --> 394         self.figure.draw(self.renderer)
>    395
>    396     def get_renderer(self):
>
> /sw/lib/python2.6/site-packages/matplotlib/artist.pyc in
> draw_wrapper(artist, renderer, *args, **kwargs)
>     53     def draw_wrapper(artist, renderer, *args, **kwargs):
>     54         before(artist, renderer)
> ---> 55         draw(artist, renderer, *args, **kwargs)
>     56         after(artist, renderer)
>     57
>
> /sw/lib/python2.6/site-packages/matplotlib/figure.pyc in draw(self,
> renderer)
>    796         dsu.sort(key=itemgetter(0))
>    797         for zorder, func, args in dsu:
> --> 798             func(*args)
>    799
>    800         renderer.close_group('figure')
>
> /sw/lib/python2.6/site-packages/matplotlib/artist.pyc in
> draw_wrapper(artist, renderer, *args, **kwargs)
>     53     def draw_wrapper(artist, renderer, *args, **kwargs):
>     54         before(artist, renderer)
> ---> 55         draw(artist, renderer, *args, **kwargs)
>     56         after(artist, renderer)
>     57
>
> /sw/lib/python2.6/site-packages/matplotlib/axes.pyc in draw(self, renderer,
> inframe)
>   1932
>   1933         for zorder, a in dsu:
> -> 1934             a.draw(renderer)
>   1935
>   1936         renderer.close_group('axes')
>
> /sw/lib/python2.6/site-packages/matplotlib/artist.pyc in
> draw_wrapper(artist, renderer, *args, **kwargs)
>     53     def draw_wrapper(artist, renderer, *args, **kwargs):
>     54         before(artist, renderer)
> ---> 55         draw(artist, renderer, *args, **kwargs)
>     56         after(artist, renderer)
>     57
>
> /sw/lib/python2.6/site-packages/matplotlib/patches.pyc in draw(self,
> renderer)
>    381                 path_effect.draw_path(renderer, gc, tpath, affine,
> rgbFace)
>    382         else:
> --> 383             renderer.draw_path(gc, tpath, affine, rgbFace)
>    384
>    385         gc.restore()
>
> /sw/lib/python2.6/site-packages/matplotlib/backends/backend_agg.pyc in
> draw_path(self, gc, path, transform, rgbFace)
>    115                 self._renderer.draw_path(gc, p, transform, rgbFace)
>    116         else:
> --> 117             self._renderer.draw_path(gc, path, transform, rgbFace)
>    118
>    119     def draw_mathtext(self, gc, x, y, s, prop, angle):
>
> /sw/lib/python2.6/site-packages/matplotlib/backend_bases.pyc in
> get_hatch_path(self, density)
>    865         if self._hatch is None:
>    866             return None
> --> 867         return Path.hatch(self._hatch, density)
>    868
>    869
>
> /sw/lib/python2.6/site-packages/matplotlib/path.pyc in hatch(cls,
> hatchpattern, density)
>    662             return None
>    663
> --> 664         hatch_path = cls._hatch_dict.get((hatchpattern, density))
>    665         if hatch_path is not None:
>    666             return hatch_path
>
> TypeError: unhashable type: 'list'
>
>
>
> ------------------------------------------------------------------------------
> Enable your software for Intel(R) Active Management Technology to meet the
> growing manageability and security demands of your customers. Businesses
> are taking advantage of Intel(R) vPro (TM) technology - will your software
> be a part of the solution? Download the Intel(R) Manageability Checker
> today! http://p.sf.net/sfu/intel-dev2devmar
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
------------------------------------------------------------------------------
Enable your software for Intel(R) Active Management Technology to meet the
growing manageability and security demands of your customers. Businesses
are taking advantage of Intel(R) vPro (TM) technology - will your software 
be a part of the solution? Download the Intel(R) Manageability Checker 
today! http://p.sf.net/sfu/intel-dev2devmar
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to