2

There are many bar plot questions of similar question here in stackoverflow, but none of them completely answer the question.

I have a pandas dataframe and want to use pandas bar plot with increasing hue of given color (e.g. Blue, Red).

Here is my code:

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

df = pd.DataFrame({'count': {0: 3372, 1: 68855, 2: 17948, 3: 708, 4: 9117}})

norm = plt.Normalize(0, df["count"].values.max())
colors = plt.cm.Blues(norm(df["count"].values))

df.plot(kind='bar', color=colors);

Required
Slightly blue color to lowest value.
Darkest blue color to maximum value.

Related questions: How to give a pandas/matplotlib bar graph custom colors
Change colours of Pandas bar chart

light picture

BhishanPoudel
  • 15,974
  • 21
  • 108
  • 169

2 Answers2

1

Try changing the last line to df['count'].plot.bar(color=colors)

enter image description here

Yosi Hammer
  • 588
  • 2
  • 8
1

The problem here is you are calling .plot() on df, which is a pd.DataFrame, and not on a pd.Series. Try this:

df['count'].plot(kind='bar', color=colors)

enter image description here