Buy Me a Coffee? Your support is much appreciated!
demo.py
import numpy as np
import matplotlib.pyplot as plt
# prepare the data
x_data = (20, 35, 30, 25, -28)
y_data = (25, 32, 35, 20, -22)
n = len(x_data)
x_location = np.arange(n)
bar_width = 0.35
fig, ax = plt.subplots(figsize=(7, 7))
# plot the bar graph
x_bar = ax.bar(x_location, x_data, bar_width, label='X')
y_bar = ax.bar(x_location, y_data, bar_width, bottom=x_data, label='Y')
# add a horizontal line across x-axis where y = 0
ax.axhline(0, color='red', linewidth=1.0)
# add y axis label title
ax.set_ylabel('Scores')
# add x axis labebls
ax.set_xticks(x_location)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
# insert legend
ax.legend(loc='upper right')
# insert data labels
ax.bar_label(x_bar, label_type='center', color='white')
ax.bar_label(y_bar, label_type='center', color='white')
ax.bar_label(y_bar)
plt.show()