How to give a pandas/matplotlib bar graph custom colors

How to give a pandas/matplotlib bar graph custom colors

Asked on December 24, 2018 in Pandas.
Add Comment


  • 3 Answer(s)

    The color option as a list directly to the plot function:

    from matplotlib import pyplot as plt
    from itertools import cycle, islice
    import pandas, numpy as np # I find np.random.randint to be better
     
    # Make the data
    x = [{i:np.random.randint(1,5)} for i in range(10)]
    df = pandas.DataFrame(x)
     
    # Make a list by cycling through the colors you care about
    # to match the length of your data.
    my_colors = list(islice(cycle(['b', 'r', 'g', 'y', 'k']), None, len(df)))
     
    # Specify this list of colors as the `color` option to `plot`.
    df.plot(kind='bar', stacked=True, color=my_colors)
    

    The own custom list, you can do a few of the following, or just look up the Matplotlib techniques for defining a color item by its RGB values, etc.

    my_colors = ['g', 'b']*5 # <-- this concatenates the list to itself 5 times.
    my_colors = [(0.5,0.4,0.5), (0.75, 0.75, 0.25)]*5 # <-- make two custom RGBs and repeat/alternate them over all the bar elements.
    my_colors = [(x/10.0, x/20.0, 0.75) for x in range(len(df))] # <-- Quick gradient example along the Red/Green dimensions.
    

    The big piece of advice is to just use the functions from Matplotlib directly. Calling them from Pandas is OK, but I find better options and performance calling them straight from Matplotlib.

    Answered on December 24, 2018.
    Add Comment

    The simplest way is to use the colormap parameter in .plot() with one of the preset color gradients.

    df.plot(kind='bar', stacked=True, colormap='Paired')
    
    
    Answered on December 24, 2018.
    Add Comment

    The list of colors and pass them to the color parameter. All the colormaps are in the cm matplotlib module. Let’s get a list of 30 RGB (plus alpha) color values from the reversed inferno colormap. first get the colormap and the pass it a sequence of values between 0 and 1. Here, using np.linspace to create 30 equally-spaced values between .4 and .8 that represent that portion of the colormap.

    from matplotlib import cm
    color = cm.inferno_r(np.linspace(.4,.8, 30))
    color
     
    array([[ 0.865006, 0.316822,  0.226055,  1. ],
          [ 0.851384 ,  0.30226 , 0.239636,  1. ],
          [ 0.832299,  0.283913 , 0.257383,  1. ],
          [ 0.817341,  0.270954,  0.27039 ,  1. ],
          [ 0.796607,  0.254728,  0.287264,  1. ],
          [ 0.775059,  0.239667,  0.303526,  1. ],
          [ 0.758422,  0.229097,  0.315266,  1. ],
          [ 0.735683,  0.215906,  0.330245,  1. ],
    .....
    

    The plot using data from the original post:

    import random
    x = [{i:random.randint(1,5)} for i in range(30)]
    df = pd.DataFrame(x)
    df.plot(kind='bar', stacked=True, color=color, legend=False, figsize=(12,4))
    
    Answered on December 24, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.