Skip to content

Instantly share code, notes, and snippets.

@H4kor
Created June 19, 2018 19:23
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save H4kor/d3f619ab974d74dfb85fcdd816877a13 to your computer and use it in GitHub Desktop.
Full Code Plotting Examples
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
N = 100
q1 = np.random.choice(7, N, p=[0.5, 0.3, 0.1, 0.05, 0.05, 0, 0]) + 1 # Mostly at 1
q2 = np.random.choice(7, N, p=[0, 0, 0, 0.1, 0.2, 0.2, 0.5]) + 1 # Mostly at 7
q3 = np.random.choice(7, N, p=[0, 0.1, 0.2, 0.4, 0.2, 0.1, 0]) + 1 # Around center
q4 = np.random.choice(7, N, p=[1/7]*7) + 1 # Uniform
q5 = np.random.choice(7, N, p=[0.4, 0.1, 0, 0, 0, 0.1, 0.4]) + 1 # At extrema
data = np.array([q1, q2, q3, q4, q5])
print(data.shape)
index = np.arange(1, 6)
x_labels = ["Q1", "Q2", "Q3", "Q4", "Q5"]
y = [np.mean(q1), np.mean(q2), np.mean(q3), np.mean(q4), np.mean(q5)]
y_sem = [scipy.stats.sem(q1), scipy.stats.sem(q2), scipy.stats.sem(q3), scipy.stats.sem(q4), scipy.stats.sem(q5)]
y_std = [np.std(q1), np.std(q2), np.std(q3), np.std(q4), np.std(q5)]
colors = [u'#1f77b4', u'#ff7f0e', u'#2ca02c', u'#d62728', u'#9467bd', u'#8c564b', u'#e377c2', u'#7f7f7f', u'#bcbd22', u'#17becf']
fig, ax = plt.subplots()
ax.bar(index, y, color=colors)
ax.set_title('Mean of Answers')
ax.set_xticks(index)
ax.set_xticklabels(x_labels)
ax.set_ylabel('Scores')
ax.set_yticks(range(1,8))
plt.savefig("lik/mean.png")
plt.close()
fig, ax = plt.subplots()
ax.bar(index, y, yerr=y_std, color=colors)
ax.set_title('Mean+Std of Answers')
ax.set_xticks(index)
ax.set_xticklabels(x_labels)
ax.set_ylabel('Scores')
ax.set_yticks(range(1,8))
plt.savefig("lik/mean_std.png")
plt.close()
fig, ax = plt.subplots()
vps = ax.violinplot([q1, q2, q3, q4, q5])
vps["cbars"].set_color("grey")
vps["cmins"].set_color("grey")
vps["cmaxes"].set_color("grey")
for i, vp in enumerate(vps["bodies"]):
vp.set_facecolor(colors[i])
vp.set_edgecolor(colors[i])
vp.set_color(colors[i])
ax.set_title('Violinplot')
ax.set_xticks(index)
ax.set_xticklabels(x_labels)
ax.set_ylabel('Scores')
ax.set_yticks(range(1,8))
plt.savefig("lik/violin.png")
plt.close()
fig, ax = plt.subplots()
ax.boxplot([q1, q2, q3, q4, q5])
ax.set_title('Boxplot')
ax.set_xticks(index)
ax.set_xticklabels(x_labels)
ax.set_ylabel('Scores')
ax.set_yticks(range(1,8))
plt.savefig("lik/boxplot.png")
plt.close()
fig, ax = plt.subplots()
scatter_x = []
scatter_y = []
scatter_c = []
for x, d in enumerate(data):
for y in d:
scatter_x.append(x + 1 + np.random.normal(0, 0.075))
scatter_y.append(y + np.random.normal(0, 0.075))
scatter_c.append(colors[x])
ax.scatter(scatter_x, scatter_y, s=16, color=scatter_c)
ax.set_title('Scattering')
ax.set_xticks(index)
ax.set_xticklabels(x_labels)
ax.set_ylabel('Scores')
ax.set_yticks(range(1,8))
plt.savefig("lik/scatter.png")
plt.close()
fig, ax = plt.subplots()
scatter_x = []
scatter_y = []
scatter_s = []
scatter_c = []
for x in range(1, 6):
for y in range(1, 8):
scatter_x.append(x)
scatter_y.append(y)
scatter_s.append(len([1 for s in data[x-1] if s == y])/len(data[x-1])*1024)
scatter_c.append(colors[x-1])
ax.scatter(scatter_x, scatter_y, s=scatter_s, color=scatter_c)
ax.set_title('Scaled Dots')
ax.set_xticks(index)
ax.set_xticklabels(x_labels)
ax.set_ylabel('Scores')
ax.set_yticks(range(1,8))
plt.savefig("lik/scaled_dots.png")
plt.close()
fig, ax = plt.subplots()
for i, d in enumerate(data):
i += 1
w = []
for k in range(1,8):
w.append(len([1 for s in d if s == k])/len(d))
ax.barh(range(1,8), w, left=i-np.array(w)/2, height=1, align="center")
ax.set_title('Histograms')
ax.set_xticks(index)
ax.set_xticklabels(x_labels)
ax.set_ylabel('Scores')
ax.set_yticks(range(1,8))
plt.savefig("lik/hists.png")
plt.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment