Title: Plotting an R-style Histogram in Python Slug: r-histogram-python Summary: A functional replication of R's histogram function in Python Date: 2018-01-23 18:15 Category: Statistics Tags: Basics Authors: Thomas Pinder
In R, a common function I use is the simple histogram plot one-liner. Whilst functionable in MatPlotLib, I do not find it to have the same end appeal in in terms of visual aesthetics. A simple function solves that though...
In [4]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import collections as matcoll
%matplotlib inline
def r_histogram(x_data, y_data, x_label, y_label):
lines = []
for i in range(len(x_data)):
pair=[(x_data[i],0), (x_data[i], y_data[i])]
lines.append(pair)
linecoll = matcoll.LineCollection(lines)
fig, ax = plt.subplots()
ax.add_collection(linecoll)
plt.scatter(x_data, y_data);
plt.xticks(x_data);
plt.ylim(0, np.max(y_data)+0.02);
plt.xlim(0, np.max(x_data)+0.02);
plt.xlabel(x_label)
plt.ylabel(y_label)
We can see an example of this function now using some mock data Bayesian data:
In [5]:
beliefs = np.arange(0.05, 1, 0.1)
priors = np.array([1, 1, 3, 2, 6, 8, 9, 8, 2, 1])
prior = priors/np.sum(priors)
r_histogram(beliefs, prior, "Proportion", "Posterior Probability")