Home > Back-end >  switch color of one given bar in a bar plot of a pandas dataframe in python
switch color of one given bar in a bar plot of a pandas dataframe in python

Time:01-25

I have the following barh plot of a given dataframe:

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

df = pd.DataFrame({"first":np.arange(1,6), "second":np.arange(2,7)}) 
df.plot(kind="barh", color=tuple(["g", "b"]))

combined bar plot

Comment

Here I split the DataFrame df into two DataFrames df1 and df2, both have the same shape like df.

Then I plot each new DataFrame and combine the two plot.

I don't like the legend but this as close I can get.

CodePudding user response:

The below change (updated code)

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

df = pd.DataFrame({"first":np.arange(1,6), "second":np.arange(2,7)}) 
df.plot(kind="barh", color={"first":["#af12be22"]   4*["g"], "second":5*["b"]})

will give you what I think you are looking for. Hope this helps.

enter image description here

  • Related