diff --git a/docs/examples_with_code.md b/docs/examples_with_code.md index e705480..cf37204 100644 --- a/docs/examples_with_code.md +++ b/docs/examples_with_code.md @@ -370,23 +370,35 @@ fig.savefig('bar_prettyplotlib_grid_annotated_labeled.png') ![Bar plot with white grid, x-axis labeled and each bar annotated](https://raw.github.com/olgabot/prettyplotlib/master/ipython_notebooks/bar_prettyplotlib_grid_annotated_labeled.png) -## `bar` with colormap +## `bar` and `barh` with colormap -If you want to color the bars according to thier height, you should use `cmap=True`: +If you want to color the bars according to thier height, you should either use `cmap=True` or you specify the colormap on your own `cmap=your_cmap`: ```python import prettyplotlib as ppl -from pylab import savefig +from pylab import savefig, show, figure, subplot from numpy import array, e from scipy import optimize from scipy.misc import factorial # prettyplotlib imports +import brewer2mpl func = lambda p, x: p[0]*pow(p[1],x)*pow(e,-p[1])/factorial(x) N = range(40) -ppl.bar(N, func(array([0.9,14.8]),N), cmap=True) +Set2 = brewer2mpl.get_map('Set2', 'qualitative', 8).mpl_colormap +figure(1) +subplot(2,2,1) +ppl.bar(N, func(array([0.9,14.8]),N), cmap=True) +subplot(2,2,2) +ppl.barh(N, func(array([0.9,14.8]),N), cmap=True) +subplot(2,2,3) +ppl.bar(N, func(array([0.9,14.8]),N), cmap=Set2) +subplot(2,2,4) +ppl.barh(N, func(array([0.9,14.8]),N), cmap=Set2) + +#show() savefig('bar_prettyplotlib_colormap.png') ``` diff --git a/ipython_notebooks/bar_prettyplotlib_colormap.png b/ipython_notebooks/bar_prettyplotlib_colormap.png index 5771f37..32c0ed7 100644 Binary files a/ipython_notebooks/bar_prettyplotlib_colormap.png and b/ipython_notebooks/bar_prettyplotlib_colormap.png differ diff --git a/prettyplotlib/_barh.py b/prettyplotlib/_barh.py index 9fcd743..dd7729b 100644 --- a/prettyplotlib/_barh.py +++ b/prettyplotlib/_barh.py @@ -1,7 +1,7 @@ __author__ = 'olga' from prettyplotlib.utils import maybe_get_ax, remove_chartjunk -from prettyplotlib.colors import set2, almost_black +from prettyplotlib.colors import set2, almost_black, getcolors import numpy as np import collections @@ -53,6 +53,12 @@ def barh(*args, **kwargs): # If no grid specified, don't draw one. grid = kwargs.pop('grid', None) + cmap = kwargs.pop('cmap', False) + if cmap: + kwargs['edgecolor'] = almost_black + if not stacked: + kwargs['color'] = getcolors(cmap, width, 0) + # Check if stacked and plot data accordingly if stacked: num_stacks, num_data = width.shape @@ -61,7 +67,10 @@ def barh(*args, **kwargs): lst = list(args) lst[1] = width[i] args = tuple(lst) - kwargs['color'] = set2[i] + if cmap: + kwargs['color'] = getcolors(cmap, width[i], i) + else: + kwargs['color'] = set2[i] kwargs['left'] = left rectangles = ax.barh(*args, **kwargs) left += width[i]