total_group_sum.unstack().plot(kind='bar')

I am able to plot my bar graph, however now I want to draw a horizontal mean line over it. I try using axhline() but I am getting an error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

How do I plot a horizontal mean line over my already existing bar graph?

total_group_sum.unstack().plot(kind='bar')

<IMAGE OF BAR GRAPH (I AM NOT ABLE TO POST PHOTO HERE)>

mean = total_group_sum.mean()
plt = total_group_sum.unstack().plot(kind='bar')
plt.axhline(mean)

ValueError: The truth value of a Series is ambiguous. Use a.empty, 
a.bool(), a.item(), a.any() or a.all().

PS - I am using jupyter notebook

1 Answers

0
warped On Best Solutions

So, I tried recreating what I think is your problem, using random data:

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


np.random.seed(123)
df = pd.DataFrame(
    {
        'a':np.random.randint(0,10,100),
        'b':np.random.randint(0,10,100),
        'c':np.random.randint(0,10,100),}
)

df.groupby('a').sum().unstack().plot(kind='bar')

barplot

I think your problem is, that this line:

mean = df.groupby('a').sum().mean()

returns the mean for every group:

b    48.7
c    42.1
dtype: float64

Then, you call

plt.axhline(mean)

Which wants to plot exactly one line, but you give it several. You can do two things:

  1. either, plot the mean for every group:

    for m in mean:
        plt.axhline(m)
    
  2. or, calculate the mean of the means, and plot that:

    plt.axhline(np.mean(mean))