1

I did like display height number of the bar on the output of this bar graphs and also be able to include missing values on the bar graphs to be represented by zero. This my dataset:

# intialise data of lists. 
data = {'Hospital_name':['Jootrh Hospital', 'Jootrh Hospital', 'Embu Hospital', 'Embu Hospital','Bungoma Hospital', 'Bungoma Hospital', 'Keru Hospital', 'Keru Hospital'], 
        'periodname':["18-Jul", "18-Aug", "18-Jul", "18-Aug","18-Jul", "18-Aug", "18-Jul", "18-Aug"], 'normal deliveries':[452, 458, "NAN", 45,498, 466, "NAN", 450],
       'caesarian sections':[67.0, 99.0, 13.0, 13.0,60.0, 19.0, 73.0, "NAN"], 'breach delivery':[10.0, "NAN", 13.0, 137.0,100.0, "NAN", "NAN" ,197.0],
       'assisted vd':["NAN", "NAN", 1.0, 37.0,1.0, "NAN", 1.0, 37.0]}


# Create DataFrame 
df = pd.DataFrame(data) 
df

Here is my code that displays the bar graphs but I did like to include missing values and bar height value.

import numpy as np
import matplotlib.pyplot as plt

df.set_index('periodname', inplace=True)
grouped = df.groupby('Hospital_name')

ncols=2
nrows = int(np.ceil(grouped.ngroups/ncols))

fig, axes = plt.subplots(nrows=nrows, ncols=ncols,figsize=(20,50),  constrained_layout=True)

for (key, ax) in zip(grouped.groups.keys(), axes.flatten()):
    grouped.get_group(key).plot(kind='bar',ax=ax, title=key)

ax.legend()
plt.show()

How can be able to include bar height value and also include missing values represented by zero on bar graphs?

EXPECTED OUTPUT is something like this : enter image description here

LivingstoneM
  • 1,088
  • 10
  • 28

1 Answers1

0

You probably could just replace all your "NAN" values with 0 before plotting.

for (key, ax) in zip(grouped.groups.keys(), axes.flatten()):
    grouped.get_group(key).replace("NAN",0).plot(kind='bar',ax=ax, title=key)

This should result in 4 bars for each period.

Update

For the height / values for each bar refer to answer.

Very crude and with positioning problems this could look something like this in your code:

fig, axes = plt.subplots(nrows=nrows, ncols=ncols,figsize=(20,50),  constrained_layout=True)

x_offset = 0.02
y_offset = 0.02

for (key, ax) in zip(grouped.groups.keys(), axes.flatten()):

    temp = grouped.get_group(key).replace("NAN",0).plot(kind='bar',ax=ax, title=key)
    for bar in temp.patches:
        b = bar.get_bbox()
        val = "{:+.2f}".format(b.y1 + b.y0)        
        ax.annotate(val, ((b.x0 + b.x1)/2 + x_offset, b.y1 + y_offset))


ax.legend()
plt.show()
user10455554
  • 403
  • 7
  • 14
  • and the bar height number? – LivingstoneM Oct 30 '19 at 13:18
  • Sorry, I missed that. Maybe an answer of [so1](https://stackoverflow.com/questions/51919113/how-can-i-display-the-bar-value-on-top-of-each-bar-on-matplotlib) or [so2](https://stackoverflow.com/questions/30228069/how-to-display-the-value-of-the-bar-on-each-bar-with-pyplot-barh) can be adapted. The matplotlib homepage also includes an [example](https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/barchart.html) – user10455554 Oct 30 '19 at 13:36
  • I have tried that matplotlib example multiple times but now I dont get any plot at all, I did like if you can try replicating it on the code I have shared, seems I am missing something this side – LivingstoneM Oct 30 '19 at 13:42
  • See Update in answer – user10455554 Oct 30 '19 at 14:22