How to fill matplotlib bars with a gradient?

11,949

Solution 1

Just as depicted in Pyplot: vertical gradient fill under curve? one may use an image to create a gradient plot.

Since bars are rectangular the extent of the image can be directly set to the bar's position and size. One can loop over all bars and create an image at the respective position. The result is a gradient bar plot.

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

bar = ax.bar([1,2,3,4,5,6],[4,5,6,3,7,5])

def gradientbars(bars):
    grad = np.atleast_2d(np.linspace(0,1,256)).T
    ax = bars[0].axes
    lim = ax.get_xlim()+ax.get_ylim()
    for bar in bars:
        bar.set_zorder(1)
        bar.set_facecolor("none")
        x,y = bar.get_xy()
        w, h = bar.get_width(), bar.get_height()
        ax.imshow(grad, extent=[x,x+w,y,y+h], aspect="auto", zorder=0)
    ax.axis(lim)

gradientbars(bar)

plt.show() 

enter image description here

Solution 2

I adapted @ImportanceOfBeingErnest's answer here using Seaborn instead of Matplotlib.

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

def gradientbars(bars):
    grad = np.atleast_2d(np.linspace(0,1,256)).T # Gradient of your choice

    rectangles = bars.containers[0]
    # ax = bars[0].axes
    fig, ax = plt.subplots()

    xList = []
    yList = []
    for rectangle in rectangles:
        x0 = rectangle._x0
        x1 = rectangle._x1
        y0 = rectangle._y0
        y1 = rectangle._y1

        xList.extend([x0,x1])
        yList.extend([y0,y1])

        ax.imshow(grad, extent=[x0,x1,y0,y1], aspect="auto", zorder=0)

    ax.axis([min(xList), max(xList), min(yList), max(yList)*1.1]) # *1.1 to add some buffer to top of plot

    return fig,ax


sns.set(style="whitegrid", color_codes=True)
np.random.seed(sum(map(ord, "categorical")))

# Load dataset
titanic = sns.load_dataset("titanic")

# Make Seaborn countplot
seabornAxHandle = sns.countplot(x="deck", data=titanic, palette="Greens_d")
plt.show() # Vertical bars with horizontal gradient

# Call gradientbars to make vertical gradient barplot using Seaborn ax
figVerticalGradient, axVerticalGradient = gradientbars(seabornAxHandle)

# Styling using the returned ax
axVerticalGradient.xaxis.grid(False)
axVerticalGradient.yaxis.grid(True)

# Labeling plot to match Seaborn
labels=titanic['deck'].dropna().unique().to_list() # Chaining to get tick labels as a list
labels.sort()
plt.ylabel('count')
plt.xlabel('deck')
plt.xticks(range(0,len(labels)), labels)  # Set locations and labels

plt.show() # Vertical bars with vertical gradient

Output from Seaborn countplot: Output from Seaborn countplot

Output with vertical gradient bars: Output with gradient bars

Solution 3

I am using seaborn barplot with the palette option. Imagine you have a simple dataframe like:

df = pd.DataFrame({'a':[1,2,3,4,5], 'b':[10,5,2,4,5]})

using seaborn:

sns.barplot(df['a'], df['b'], palette='Blues_d')

you can obtain something like:

enter image description here

then you can also play with the palette option and colormap adding a gradient according to some data like:

sns.barplot(df['a'], df['b'], palette=cm.Blues(df['b']*10)

obtaining:

enter image description here

Hope that helps.

Share:
11,949

Related videos on Youtube

cattt84
Author by

cattt84

Updated on June 04, 2022

Comments

  • cattt84
    cattt84 almost 2 years

    I would be very interested in filling matplotlib/seaborn bars of a barplot with different gradients exactly like done here (not with matplotlib as far as I understood): enter image description here

    I have also checked this related topic Pyplot: vertical gradient fill under curve?.

    Is this only possible via gr-framework: enter image description here or are there alternative strategies?

  • cattt84
    cattt84 over 7 years
    Thanks a lot, but I am looking for the same gradient within every single bar, not a gradient accross different bars!