matplotlib bar plot labels code example
Example 1: bar labeling in matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('ggplot')
x = ['Nuclear', 'Hydro', 'Gas', 'Oil', 'Coal', 'Biofuel']
energy = [5, 6, 15, 22, 24, 8]
x_pos = [i for i, _ in enumerate(x)]
plt.bar(x_pos, energy, color='green')
plt.xlabel("Energy Source")
plt.ylabel("Energy Output (GJ)")
plt.title("Energy output from various fuel sources")
plt.xticks(x_pos, x)
plt.show()
Example 2: Grouped bar chart with labels
fig, ax = plt.subplots(figsize=(12, 8))
x = np.arange(len(df.job.unique()))
bar_width = 0.4
b1 = ax.bar(x, df.loc[df['sex'] == 'men', 'count'],
width=bar_width)
b2 = ax.bar(x + bar_width, df.loc[df['sex'] == 'women', 'count'],
width=bar_width)