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