0

I have a basic data frame like this:

    Mean    SEM
A   21.4    2.1
B   33.5    3.4
C   16.5    3.9
D   50.1    6.1

I attempt to plot it with matplotlib like this:

plot1 = df.plot(kind="bar", yerr="SEM", y="Mean", legend=False, cmap="Greys", capsize=4, edgecolor="black")

CMAP does not work correctly. It colorizes each bar by itself, meaning that each bar has a sequential grayscale applied to it.

I want it to colorize across my bars, so that the first bar is white, the second bar is greyish, the third bar is greyish, and the final bar is ultimately black. I can achieve this with manual color filling but that's not ideal.

Here's all my code:

import pandas as pd

%matplotlib inline
# produce vector inline graphics
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('pdf', 'svg')
import matplotlib.pyplot as plt


data = {'Mean':[21.4, 33.5, 16.5, 50.1], 
        'SEM':[2.1, 3.4, 3.9, 6.1]} 
  
# Create DataFrame 
df = pd.DataFrame(data, index=["A", "B", "C", "D"]) 

plot1 = df.plot(kind="bar", yerr="SEM", y="Mean", legend=False, cmap="Greys", capsize=4, edgecolor="black")

The (failed) plot: enter image description here

Kalif Vaughn
  • 353
  • 2
  • 5
  • 10
  • 1
    If you want something automatic, maybe `sns.barplot(x=df.index, y=df["Mean"], yerr=df["SEM"], palette='Greys')` could do? Or `df.plot(..., color=plt.cm.Greys(np.linspace(0, 1, len(df))))` – JohanC Feb 19 '21 at 20:28
  • Have a look at the `color` argument. https://stackoverflow.com/a/11927922/2864250 – dubbbdan Feb 19 '21 at 20:38
  • @JohanC Thank you, the "palette" command from Seaborn solves the issue. – Kalif Vaughn Feb 19 '21 at 21:50

0 Answers0