Python Forum
bar chart and custom colormap
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bar chart and custom colormap
#1
Dear Python Experts,
I created a custom colormap but noticed that my bars all got the same color.
Why is that?
Anything that helps my understanding would be much appreciated!

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

np.random.seed(12345)

df = pd.DataFrame([np.random.normal(33500,150000,3650), 
                   np.random.normal(41000,90000,3650), 
                   np.random.normal(41000,120000,3650), 
                   np.random.normal(48000,55000,3650)], 
                  index=[1992,1993,1994,1995])

numpyMatrix = df.as_matrix()

#red,white,blue
cmap = clr.LinearSegmentedColormap.from_list('custom red', ['#ff0000','#000000','#0000ff'], N=16)

%matplotlib notebook

def plot():
    return  df.mean(axis=1).plot.bar(colormap=cmap, width=0.9, rot=0)
plot()
Reply
#2
df.mean() is pandas serie and uses single color of colormap. If you had dataframe with multiple columns, it would be colored according to the  given colormap. You can use
pd.DataFrame(np.diag(df.mean(axis=1))).plot.bar(colormap=cmap)
to transform your serie to a diagonal of a dataframe and plot it, but probably most straightforward way is to use color parameter with explictly stated colors
df.mean(axis=1).plot.bar(color=['r', 'g', 'b', 'y'])
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020