Last active
March 29, 2021 14:16
-
-
Save mjm522/2abb11935b90c53648a52f2a72322b81 to your computer and use it in GitHub Desktop.
Python normalised histogram with just outline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
x_min = 0.51 | |
x_max = 0.54 | |
num_bins = 20 | |
num_data=7000 | |
x_mean = 0.53 | |
plt.style.use("ggplot") | |
#generate data | |
data1 = np.random.normal(loc=x_mean, size=num_data) | |
data2 = np.random.normal(loc=x_mean, size=num_data) | |
#create histogram | |
d1_bin_height, d1_bin_boundary = np.histogram(data1,bins=np.linspace(x_min, x_max, num_bins)) | |
d2_bin_height, d2_bin_boundary = np.histogram(data2, bins=np.linspace(x_min, x_max, num_bins)) | |
#find the width of the bin | |
d1_width = d1_bin_boundary[1]-d1_bin_boundary[0] | |
d2_width = d2_bin_boundary[1]-d2_bin_boundary[0] | |
#standardize data in each column by dividing with the maximum height | |
d1_bin_height = d1_bin_height/float(max(d1_bin_height)) | |
d2_bin_height = d2_bin_height/float(max(d2_bin_height)) | |
#plot the data in two subplots | |
fig=plt.figure(figsize=(15,9)) | |
ax0 = fig.add_subplot(131) | |
ax0.bar(d1_bin_boundary[:-1], d1_bin_height, width=d1_width, alpha=0.5, color=(0.6,0.7,0), label='data1') | |
ax0.step(d1_bin_boundary[:-1], d1_bin_height, where='mid') | |
ax0.legend(fontsize=20) | |
for tick in ax0.xaxis.get_major_ticks(): | |
tick.label.set_fontsize(14) | |
for tick in ax0.yaxis.get_major_ticks(): | |
tick.label.set_fontsize(14) | |
ax0.set_xticks(np.arange(x_min, x_max, 0.01), minor=False) | |
ticks = ax0.get_xticks()*10**2 | |
ax0.set_xticklabels(ticks) | |
ax1 = fig.add_subplot(132) | |
ax1.bar(d2_bin_boundary[:-1], d2_bin_height, width=f_width, alpha=0.5, color=(0.,0.7,0.6), label='data2') | |
ax1.step(d2_bin_boundary[:-1], d2_bin_height, where='mid', color='k') | |
ax1.legend(fontsize=20) | |
ax1.set_xticks(np.arange(x_min, x_max, 0.01), minor=False) | |
#increase the font size | |
for tick in ax1.xaxis.get_major_ticks(): | |
tick.label.set_fontsize(14) | |
for tick in ax1.yaxis.get_major_ticks(): | |
tick.label.set_fontsize(14) | |
#how to scale the ticks by a factor of 100 | |
ticks = ax1.get_xticks()*10**2 | |
ax1.set_xticklabels(ticks) | |
#combine the data in a single subplot | |
ax2 = fig.add_subplot(133) | |
plt.bar(d1_bin_boundary[:-1], d1_bin_height, width=r_width, alpha=0.5, color=(0.6,0.7,0), label='data1') | |
plt.step(d1_bin_boundary[:-1], d1_bin_height, where='mid') | |
plt.legend(fontsize=20) | |
plt.yticks(fontsize=20) | |
plt.xticks(fontsize=20) | |
plt.bar(d1_bin_boundary[:-1], d2_bin_height, width=f_width, alpha=0.5, color=(0.,0.7,0.6), label='data2') | |
plt.step(d2_bin_boundary[:-1], d2_bin_height, where='mid', color='k') | |
plt.legend(fontsize=20) | |
plt.yticks(fontsize=20) | |
#how to scale the tick labels | |
ticks = ax0.get_xticks()*10**2 | |
ax0.set_xticklabels(ticks) | |
#plot the mean | |
plt.vlines(x_mean, 0, 1, color='b', linestyle='--', linewidth=6) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment