user8864088
user8864088

Reputation:

Pandas plot bar with different colors not working as expected

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'

Upvotes: 5

Views: 1647

Answers (2)

Veera Samantula
Veera Samantula

Reputation: 165

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

Upvotes: 0

Lucas
Lucas

Reputation: 7341

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

Upvotes: 7

Related Questions