Skip to content

Commit

Permalink
Add colormapsupport for barh and update the doc for own colormaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Lettau committed Mar 16, 2014
1 parent 972e481 commit 3a5db97
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
20 changes: 16 additions & 4 deletions docs/examples_with_code.md
Expand Up @@ -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')
```

Expand Down
Binary file modified ipython_notebooks/bar_prettyplotlib_colormap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 11 additions & 2 deletions 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

Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand Down

0 comments on commit 3a5db97

Please sign in to comment.