Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73448 > unrolled thread

Matplotlib Colouring outline of histogram

Started byJamie Mitchell <jamiemitchell1604@gmail.com>
First post2014-06-20 01:10 -0700
Last post2014-06-20 08:10 -0700
Articles 5 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Matplotlib Colouring outline of histogram Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-06-20 01:10 -0700
    Re: Matplotlib Colouring outline of histogram Jason Swails <jason.swails@gmail.com> - 2014-06-20 09:47 -0400
      Re: Matplotlib Colouring outline of histogram Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-06-20 07:27 -0700
        Re: Matplotlib Colouring outline of histogram Jason Swails <jason.swails@gmail.com> - 2014-06-20 10:52 -0400
    Re: Matplotlib Colouring outline of histogram Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-06-20 08:10 -0700

#73448 — Matplotlib Colouring outline of histogram

FromJamie Mitchell <jamiemitchell1604@gmail.com>
Date2014-06-20 01:10 -0700
SubjectMatplotlib Colouring outline of histogram
Message-ID<ae0491e6-70de-4cc3-aea4-8922f72e17b8@googlegroups.com>
Hi folks,

Instead of colouring the entire bar of a histogram i.e. filling it, I would like to colour just the outline of the histogram. Does anyone know how to do this?
Version - Python2.7

Cheers,
Jamie

[toc] | [next] | [standalone]


#73457

FromJason Swails <jason.swails@gmail.com>
Date2014-06-20 09:47 -0400
Message-ID<mailman.11167.1403272031.18130.python-list@python.org>
In reply to#73448

[Multipart message — attachments visible in raw view] — view raw

On Fri, Jun 20, 2014 at 4:10 AM, Jamie Mitchell <jamiemitchell1604@gmail.com
> wrote:

> Hi folks,
>
> Instead of colouring the entire bar of a histogram i.e. filling it, I
> would like to colour just the outline of the histogram. Does anyone know
> how to do this?
> Version - Python2.7
>

Look at the matplotlib.pyplot.hist function documentation:
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hist

In addition to the listed parameters, you'll see the "Other Parameters"
taken are those that can be applied to the created Patch objects (which are
the actual rectangles).  For the Patch keywords, see the API documentation
on the Patch object (
http://matplotlib.org/api/artist_api.html#matplotlib.patches.Patch). So you
can do one of two things:

1) Pass the necessary Patch keywords to effect what you want

e.g. (untested):
import matplotlib.pyplot as plt

plt.hist(dataset, bins=10, range=(-5, 5), normed=True,
         edgecolor='b', linewidth=2, facecolor='none', # Patch options
)

plt.show()

2) Iterate over the Patch instances returned by plt.hist() and set the
properties you want.

e.g. (untested):
import matplotlib.pyplot as plt

n, bins, patches = plt.hist(dataset, bins=10, range=(-5, 5), normed=True)
for patch in patches:
    patch.set_edgecolor('b') # color of the lines around each bin
    patch.set_linewidth(2) # Set width of bin edge
    patch.set_facecolor('none') # set no fill
    # Anything else you want to do

plt.show()

Approach (1) is the "easy" way, and is there to satisfy the majority of use
cases.  However, approach (2) is _much_ more flexible.  Suppose you wanted
to highlight a particular region of your data with a specific facecolor or
edgecolor -- you can apply the features you want to individual patches
using approach (2).  Or if you wanted to highlight a specific bin with
thicker lines.

This is a common theme in matplotlib -- you can use keywords to apply the
same features to every part of a plot or you can iterate over the drawn
objects and customize them individually.  This is a large part of what
makes matplotlib nice to me -- it has a "simple" mode as well as a
predictable API for customizing a plot in almost any way you could possibly
want.

HTH,
Jason

-- 
Jason M. Swails
BioMaPS,
Rutgers University
Postdoctoral Researcher

[toc] | [prev] | [next] | [standalone]


#73460

FromJamie Mitchell <jamiemitchell1604@gmail.com>
Date2014-06-20 07:27 -0700
Message-ID<83defcb1-70ef-437f-87df-9f8c8164fa92@googlegroups.com>
In reply to#73457
On Friday, June 20, 2014 2:47:03 PM UTC+1, Jason Swails wrote:
> On Fri, Jun 20, 2014 at 4:10 AM, Jamie Mitchell <jamiemit...@gmail.com> wrote:
> 
> Hi folks,
> 
> 
> 
> Instead of colouring the entire bar of a histogram i.e. filling it, I would like to colour just the outline of the histogram. Does anyone know how to do this?
> 
> Version - Python2.7
> 
> 
> 
> Look at the matplotlib.pyplot.hist function documentation: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hist
> 
> 
> 
> In addition to the listed parameters, you'll see the "Other Parameters" taken are those that can be applied to the created Patch objects (which are the actual rectangles).  For the Patch keywords, see the API documentation on the Patch object (http://matplotlib.org/api/artist_api.html#matplotlib.patches.Patch). So you can do one of two things:
> 
> 
> 
> 1) Pass the necessary Patch keywords to effect what you want
> 
> 
> 
> e.g. (untested):
> import matplotlib.pyplot as plt
> 
> 
> 
> plt.hist(dataset, bins=10, range=(-5, 5), normed=True,
>          edgecolor='b', linewidth=2, facecolor='none', # Patch options
> 
> )
> 
> 
> plt.show()
> 
> 
> 
> 2) Iterate over the Patch instances returned by plt.hist() and set the properties you want.
> 
> 
> 
> e.g. (untested):
> import matplotlib.pyplot as plt
> 
> 
> 
> n, bins, patches = plt.hist(dataset, bins=10, range=(-5, 5), normed=True)
> for patch in patches:
> 
>     patch.set_edgecolor('b') # color of the lines around each bin
>     patch.set_linewidth(2) # Set width of bin edge
> 
>     patch.set_facecolor('none') # set no fill
>     # Anything else you want to do
> 
> 
> 
> plt.show()
> 
> 
> Approach (1) is the "easy" way, and is there to satisfy the majority of use cases.  However, approach (2) is _much_ more flexible.  Suppose you wanted to highlight a particular region of your data with a specific facecolor or edgecolor -- you can apply the features you want to individual patches using approach (2).  Or if you wanted to highlight a specific bin with thicker lines.
> 
> 
> 
> This is a common theme in matplotlib -- you can use keywords to apply the same features to every part of a plot or you can iterate over the drawn objects and customize them individually.  This is a large part of what makes matplotlib nice to me -- it has a "simple" mode as well as a predictable API for customizing a plot in almost any way you could possibly want.
> 
> 
> 
> HTH,
> Jason
> 
> 
> -- 
> 
> Jason M. Swails
> BioMaPS,
> Rutgers University
> Postdoctoral Researcher

That's great Jason thanks for the detailed response, I went with the easier option 1!

I am also trying to put hatches on my histograms like so:

plt.hist(dataset,bins=10,hatch=['*'])

When it comes to plt.show() I get the following error message:
File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backends/backend_gtk.py", line 435, in expose_event
    self._render_figure(self._pixmap, w, h)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backends/backend_gtkagg.py", line 84, in _render_figure
    FigureCanvasAgg.draw(self)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backends/backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/figure.py", line 1034, in draw
    func(*args)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/axes.py", line 2086, in draw
    a.draw(renderer)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/patches.py", line 429, in draw
    renderer.draw_path(gc, tpath, affine, rgbFace)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backends/backend_agg.py", line 145, in draw_path
    self._renderer.draw_path(gc, path, transform, rgbFace)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backend_bases.py", line 1010, in get_hatch_path
    return Path.hatch(self._hatch, density)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/path.py", line 888, in hatch
    hatch_path = cls._hatch_dict.get((hatchpattern, density))
TypeError: unhashable type: 'list'
Traceback (most recent call last):
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backends/backend_gtk.py", line 435, in expose_event
    self._render_figure(self._pixmap, w, h)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backends/backend_gtkagg.py", line 84, in _render_figure
    FigureCanvasAgg.draw(self)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backends/backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/figure.py", line 1034, in draw
    func(*args)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/axes.py", line 2086, in draw
    a.draw(renderer)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/patches.py", line 429, in draw
    renderer.draw_path(gc, tpath, affine, rgbFace)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backends/backend_agg.py", line 145, in draw_path
    self._renderer.draw_path(gc, path, transform, rgbFace)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backend_bases.py", line 1010, in get_hatch_path
    return Path.hatch(self._hatch, density)
  File "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/path.py", line 888, in hatch
    hatch_path = cls._hatch_dict.get((hatchpattern, density))
TypeError: unhashable type: 'list'

Do you have any idea why this is happening?

Cheers,

Jamie

[toc] | [prev] | [next] | [standalone]


#73462

FromJason Swails <jason.swails@gmail.com>
Date2014-06-20 10:52 -0400
Message-ID<mailman.11169.1403276280.18130.python-list@python.org>
In reply to#73460

[Multipart message — attachments visible in raw view] — view raw

On Fri, Jun 20, 2014 at 10:27 AM, Jamie Mitchell <
jamiemitchell1604@gmail.com> wrote:

>
> That's great Jason thanks for the detailed response, I went with the
> easier option 1!
>
> I am also trying to put hatches on my histograms like so:
>
> plt.hist(dataset,bins=10,hatch=['*'])
>
> When it comes to plt.show() I get the following error message:
> ​[snip]
>
>   File
> "/usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/path.py",
> line 888, in hatch
>     hatch_path = cls._hatch_dict.get((hatchpattern, density))
> TypeError: unhashable type: 'list'
>
> Do you have any idea why this is happening?
>

lists are mutable types, so they are not hashable (and therefore cannot be
used as dictionary keywords).​  You need an immutable type (which _is_
hashable) to act as a dictionary key.  Like strings, tuples, and basic
number types (int, float, etc.).

The hatch should be a string (allowable symbols are given in the API
documentation).  So try

plt.hist(dataset, bins, hatch='*')

HTH,
Jason

-- 
Jason M. Swails
BioMaPS,
Rutgers University
Postdoctoral Researcher

[toc] | [prev] | [next] | [standalone]


#73463

FromJamie Mitchell <jamiemitchell1604@gmail.com>
Date2014-06-20 08:10 -0700
Message-ID<6c273842-d8de-4b27-8cc0-b1b7f06d8340@googlegroups.com>
In reply to#73448
On Friday, June 20, 2014 9:10:58 AM UTC+1, Jamie Mitchell wrote:
> Hi folks,
> 
> 
> 
> Instead of colouring the entire bar of a histogram i.e. filling it, I would like to colour just the outline of the histogram. Does anyone know how to do this?
> 
> Version - Python2.7
> 
> 
> 
> Cheers,
> 
> Jamie

Great thanks again Jason.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web