matplotlib - barchar with blurry effect
<p>I'm working with bar charts in matplotlib, and I'm focusing on colors. I have the following simple cose:</p>

<pre><code>import matplotlib.pyplot as plt

A = [5, 30, 45, 80]
X = range(4)
col = ['r','orange','y','g']
plt.bar(X, A, color = col)
plt.show()
</code></pre>

<p>What I would like to achieve is a blurry color effect on the bar depending on values, starting from red until green. I'm wondering if I can realize something like this (see only bars):</p>

<p><img src="http://i.stack.imgur.com/fyukL.png" alt="bar charts blurry effect"> </p>

<p>I tried to follow this <a href="http://matplotlib.org/examples/color/colormaps_reference.html" rel="nofollow">guide</a>, but I didn't reach any result.
May you please help me? <br></p>

<p>You have a pretty good example from matplotlib documentation on this <a href="http://matplotlib.org/examples/pylab_examples/gradient_bar.html" rel="nofollow">link</a>. Adapting this to your example you would obtain something like this:</p>

<pre><code>import matplotlib.pyplot as plt
from matplotlib.pyplot import figure

def gbar(ax, x, y, width=0.5, bottom=0):
    X = [[.6, .6], [.7, .7]]
    for left, top in zip(x, y):
        right = left + width
        ax.imshow(X, interpolation='bicubic', cmap="copper",
                  extent=(left, right, bottom, top), alpha=1)

A = [5, 30, 45, 80]
x = [i + 0.5 for i in range(4)]

fig = figure()

xmin, xmax = xlim = 0.25, 4.5
ymin, ymax = ylim = 0, 100
ax = fig.add_subplot(111, xlim=xlim, ylim=ylim,
                     autoscale_on=False)

gbar(ax, x, A, width=0.7)
ax.set_aspect('auto')
plt.show()
</code></pre>

<p>, which results in the following plot:</p>

<p><a href="http://i.stack.imgur.com/IXHZu.png" rel="nofollow"><img src="http://i.stack.imgur.com/IXHZu.png" alt="Bar plot with gradients on the bars in matplotlib"></a></p>

<p><strong>EDIT:</strong> The following adaption from the example is more flexible for working with the imshow. As so the example:</p>

<pre><code>import matplotlib.pyplot as plt
from matplotlib.pyplot import figure

def gbar(ax, x, y, width=0.5, bottom=0):
    X = np.arange(100)[:, np.newaxis]
    for left, top in zip(x, y):
        right = left + width
        mask = X &gt; top
        ax.imshow(np.ma.masked_array(X, mask), origin="lower", interpolation='nearest', cmap="RdYlGn", vmin=0, vmax=100,
                  extent=(left, right, bottom, 100), alpha=1)

A = [5, 30, 45, 80]
x = [i + 0.5 for i in range(4)]

fig = figure()

xmin, xmax = xlim = 0.25, 4.5
ymin, ymax = ylim = 0, 100
ax = fig.add_subplot(111, xlim=xlim, ylim=ylim,
                     autoscale_on=False)

gbar(ax, x, A, width=0.7)
ax.set_aspect('auto')
ax.set_yticks([i for i in range(0, 100, 10)])
ax.set_yticklabels([str(i) + " %" for i in range(0, 100, 10)])
plt.show()
</code></pre>

<p>, results in this:</p>

<p><a href="http://i.stack.imgur.com/aPiY4.png" rel="nofollow"><img src="http://i.stack.imgur.com/aPiY4.png" alt="Another implementation of imshow as a barplot"></a></p>

<p>You will notice that I'm building an array of 100 elements (monotonic strictly increasing: 0 to 100 with step 1).</p>

<pre><code>X = np.arange(100)[:, np.newaxis]
</code></pre>

<p>And masking everything above the top of the respective bar:</p>

<pre><code>mask = X &gt; top
np.ma.masked_array(X, mask)
</code></pre>

<p>Also I'm stating <code>vmin</code> and <code>vmax</code> as 0 and 100, respectively. This will always make the total green appear at position 100 and total red at position 0. If this is the effect you are looking for you can use as it is. </p>

<p>The easiest way to me to achieve more customization by bar is just playing with the values inside X (since they are the ones being colormapped). But obviously you'll need some kind of rule for it. In your example the orange sometimes appears at 40 %, at others 30 % and 20 % (it seems related with the size of the bar but I don't know the relation). One of the bars is completely green which is yet another rule.</p>

<p>So you'll have to make exceptions inside your code. For example when the top is over 90 % use a greens colormap (or whatever fits).</p>

python matplotlib colors bar-chart