Pandas plot bar with different colors not working as expected

Viewed 1400

I was trying to plot a bar diagram with first 10 bars green and last 10 bars red colors.

Here is my code:

import pandas as pd
import numpy as np

df = pd.DataFrame({'c0':np.arange(20)})

my_colors = ['g','g','g','g','g',
             'g','g','g','g','g',
             'r','r','r','r','r',
             'r','r','r','r','r']
df.plot(kind='bar', color=my_colors)

enter image description here

How can the problem be solved?

pd.__version__
'0.23.4'
2 Answers

Color has to be a list with list inside. try:

df = pd.DataFrame({'c0':np.arange(20)})

# attention to double "["
my_colors = [['g','g','g','g','g',
              'g','g','g','g','g',
              'r','r','r','r','r',
              'r','r','r','r','r']]

df.plot(kind='bar', color=my_colors)

enter image description here

Also this issue can help

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame({'c0':np.arange(20)})

my_colors = ['g','g','g','g','g',
             'g','g','g','g','g',
             'r','r','r','r','r',
             'r','r','r','r','r']
for i ,row in enumerate(my_colors):
    plt.bar(i,df.loc[i,"c0"],color=row)

enter image description here

Related