Skip to content

Instantly share code, notes, and snippets.

@kwcooper
Created April 23, 2020 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwcooper/dc9e67707b00bdc3a2f21379863f8df0 to your computer and use it in GitHub Desktop.
Save kwcooper/dc9e67707b00bdc3a2f21379863f8df0 to your computer and use it in GitHub Desktop.
Create a simple bar plot so I don't have to keep writing it
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
# Create test data
a, b = np.random.randint(10, 25, 30), np.random.randint(0, 8, 30)
a_mu, b_mu = np.mean(a), np.mean(b)
a_std, b_std = np.std(a), np.std(b)
# Plot it
names = ['Name1', 'Name2']
x_pos = np.arange(len(names))
mus = [a_mu, b_mu]
error = [a_std, b_std]
fig, ax = plt.subplots(figsize=(5,5))
ax.bar(x_pos, mus, yerr=error, align='center', alpha=0.5,
ecolor='black', capsize=13, color=['red', 'blue'])
ax.set_ylabel('Y axis amount')
ax.set_xticks(x_pos)
ax.set_xticklabels(names)
ax.set_title('Bar plot with error bars')
ax.yaxis.grid(True)
plt.rcParams["font.size"] = "15"
plt.tight_layout()
#plt.savefig('bar_plot_with_error_bars.png')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment