I am trying to plot a simple histogram using matplotlib. I have for example (I will in practice be using different distance functions)
import matplotlib.pyplot as plt
import numpy as np
import itertools
def hamdist(str1, str2):
"""Count the # of differences between equal length strings str1 and str2"""
if (len(str1) != len(str2)):
print str1, str2, "Length mismatch bozo!!!!!!"
diffs = 0
for ch1, ch2 in itertools.izip(str1, str2):
if ch1 != ch2:
diffs += 1
return diffs
n = 10
bins=np.arange(0,n+2,1)
hamdists = []
for str1 in itertools.product('01', repeat = n):
for str2 in itertools.product('01', repeat = n):
hamdists.append(hamdist(str1, str2))
plt.hist(hamdists, bins=bins)
plt.show()
I get a histogram that looks like this.
How do I do the following?
bins=np.arange(0,11,1)
this cuts off the value for x = 10. It is a graph showing the number of observations within each given interval.
The matplotlib. pyplot. hist() function is used to compute and create histogram of x.
plt. hist() method is used multiple times to create a figure of three overlapping histograms. we adjust opacity, color, and number of bins as needed. Three different columns from the data frame are taken as data for the histograms.
Your first and third points can be solved by setting the align keyword of the histogram function (which defaults to 'mid', the center of the bin). The second by manually setting the xticks.
See:
fig, ax = plt.subplots(1,1)
ax.hist(hamdists, bins=bins, align='left')
ax.set_xticks(bins[:-1])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With