BhishanPoudel
BhishanPoudel

Reputation: 17164

How to plot pandas bar plot with sequential colormap?

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

Upvotes: 2

Views: 1741

Answers (2)

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

Upvotes: 1

Yosi Hammer
Yosi Hammer

Reputation: 588

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

enter image description here

Upvotes: 1

Related Questions