Matplotlib histograms (basic questions)

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
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)

I get a histogram that looks like this.

How do I do the following?

  1. Change the x-axis so that the last bar counts the number for x = 10. If I simply change to bins=np.arange(0,11,1) this cuts off the value for x = 10.
  2. Label every point in the x-axis
  3. Move the x-axis labels to be under the middle of the bars and not at the start of them as they are now.


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.


fig, ax = plt.subplots(1,1)

ax.hist(hamdists, bins=bins, align='left')

Need Your Help

What happens when a code signing certificate expires?

certificate code-signing xbap

I am considering purchasing a code signing certificate from VeriSign or Thawte to sign an XBAP with. My question is this: What happens when that certificate expires? $299 and $599 are pretty hefty ...

How to SELECT INTO tableB which name is @tableB * FROM @tableA?

sql sql-server-2008

I am creating a stored procedure which has to create a table which name depends on input variable. For temporary results I have declared a table variable:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.