0

I am working with matplotlib and below you can see my data and my plot.

data = {
         'type_sale': ['g_1','g_2','g_3','g_4','g_5','g_6','g_7','g_8','g_9','g_10'],
         'open':[70,20,24,150,80,90,60,90,20,20],
        }

df = pd.DataFrame(data, columns = ['type_sale',
                                   'open',
                                   ])

df.plot(x='type_sale', kind='bar', title='Bar Graph')

enter image description here

So now I want to put a different color (color = 'red') on the fourth bar. I tryed but I colored all instead only one. So can anybody help me how to solve this ?

silent_hunter
  • 2,224
  • 1
  • 12
  • 30
  • 2
    Does this answer your question? [vary the color of each bar in bargraph using particular value](https://stackoverflow.com/questions/18903750/vary-the-color-of-each-bar-in-bargraph-using-particular-value) – BigBen Jul 11 '22 at 13:46
  • 2
    Also https://stackoverflow.com/questions/18973404/setting-different-bar-color-in-matplotlib-python – BigBen Jul 11 '22 at 13:46

2 Answers2

1

You can try this solution

# libraries
import numpy as np
import matplotlib.pyplot as plt
 
# create a dataset
height = [3, 12, 5, 18, 45]
bars = ('A', 'B', 'C', 'D', 'E')
x_pos = np.arange(len(bars))

# Create bars with different colors
plt.bar(x_pos, height, color=['black', 'red', 'green', 'blue', 'cyan'])

# Create names on the x-axis
plt.xticks(x_pos, bars)

# Show graph
plt.show()

Here is the documentation link Link

1

The ax.bar() method returns a list of bars that you can then manipulate, in your case with .set_color():

import matplotlib.pyplot as plt
 
f=plt.figure()
ax=f.add_subplot(1,1,1)

## bar() will return a list of bars
barlist = ax.bar([1,2,3,4], [1,2,3,4])
barlist[3].set_color('r')

plt.show()
lkavenagh
  • 68
  • 6