On Fri, Jun 20, 2014 at 4:10 AM, Jamie Mitchell <jamiemitchell1...@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:

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


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


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


Jason M. Swails
Rutgers University
Postdoctoral Researcher

Reply via email to