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


Groups > comp.lang.python > #73457

Re: Matplotlib Colouring outline of histogram

References <ae0491e6-70de-4cc3-aea4-8922f72e17b8@googlegroups.com>
Date 2014-06-20 09:47 -0400
Subject Re: Matplotlib Colouring outline of histogram
From Jason Swails <jason.swails@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.11167.1403272031.18130.python-list@python.org> (permalink)

Show all headers | View raw


[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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web