Python 如何使用matplotlib制作显示x轴上的项目数的柱状图?

Python 如何使用matplotlib制作显示x轴上的项目数的柱状图?,python,matplotlib,Python,Matplotlib,我有以下代码,可以在拼字词典中找到每个单词的所有字谜。我想用matplotlib制作一个直方图来显示x轴上的字谜图的大小,但我真的不知道从哪里开始。我认为我需要将字符串值转换为整数,但我不确定如何将字典字符串值转换为整数 谢谢 # import libraries import urllib2 from collections import defaultdict #function for inputing words def load_dictionary(word_dictionary)

我有以下代码,可以在拼字词典中找到每个单词的所有字谜。我想用matplotlib制作一个直方图来显示x轴上的字谜图的大小,但我真的不知道从哪里开始。我认为我需要将字符串值转换为整数,但我不确定如何将字典字符串值转换为整数

谢谢

# import libraries
import urllib2
from collections import defaultdict

#function for inputing words
def load_dictionary(word_dictionary):
    for word in word_dictionary:
        yield word.strip()

#function for making anagrams
def make_anagrams(source):
    d = defaultdict(list)
    for word in source:
        key = "".join(sorted(word))
        d[key].append(word)
    return d

#function for printing anagrams
def print_anagrams(word_input):
    count=0
    n=0
    k=''
    d = make_anagrams(word_input)
    for key, anagrams in d.iteritems():
        if len(anagrams) > 1:
            count+= 1
            if n < len(anagrams):
                n=len(anagrams) 
                k=key
    print 'The number of unique anagrams is: %d:' % count
    print 'The largest group of anagrams for a word is: %d; and the original word is: %s' % ( n , k)
    print 'The anagrams for the largest anagram word is: %s ' % d[k]


#input dictionary from puzzlers.org               
word_dictionary = urllib2.urlopen('http://www.puzzlers.org/pub/wordlists/ospd.txt')
word_input = load_dictionary(word_dictionary)
#print answer
print_anagrams(word_input)
#导入库
导入urllib2
从集合导入defaultdict
#用于输入单词的函数
def load_字典(word_字典):
对于word_字典中的单词:
让出word.strip()
#造字谜的功能
def生成语法(来源):
d=默认DICT(列表)
对于来源中的单词:
关键字=“连接(已排序(word))
d[关键字]。追加(word)
返回d
#用于打印字谜的函数
def打印字符(word输入):
计数=0
n=0
k=''
d=生成字谜(单词输入)
对于键,d.iteritems()中的字谜:
如果len(字谜)>1:
计数+=1
如果n
您可以使用采集模块中的计数器。以下是用于绘制直方图的代码的修改版本:

# import libraries    
import urllib2    
from collections import defaultdict    
from collections import Counter    
from matplotlib import pylab as plt    

#function for inputing words    
def load_dictionary(word_dictionary):    
    for word in word_dictionary:    
        yield word.strip()    

#function for making anagrams    
def make_anagrams(source):    
    d = defaultdict(list)    
    for word in source:    
        key = "".join(sorted(word))    
        d[key].append(word)    
    return d    

#function for printing anagrams    
def print_anagrams(word_input):    
    count=0    
    n=0    
    k=''    
    d = make_anagrams(word_input)    
    anagramLens =Counter()    
    for key, anagrams in d.iteritems():    
        anagramLens[len(anagrams)] += 1    
        if len(anagrams) > 1:    
            count+= 1    
            if n < len(anagrams):    
                n=len(anagrams)    
                k=key    
    print 'The number of unique anagrams is: %d:' % count    
    print 'The largest group of anagrams for a word is: %d; and the original word is: %s' % ( n , k)    
    print 'The anagrams for the largest anagram word is: %s ' % d[k]    
    #print list(anagramLens.elements())    
    plt.figure()    
    n, bins, patches = plt.hist(list(anagramLens.elements()), histtype='bar', rwidth=0.8)    
    plt.show()    
#input dictionary from puzzlers.org                   
word_dictionary = urllib2.urlopen('http://www.puzzlers.org/pub/wordlists/ospd.txt')    
word_input = load_dictionary(word_dictionary)    
#print answer    
print_anagrams(word_input)
#导入库
导入urllib2
从集合导入defaultdict
从收款进口柜台
从matplotlib导入pylab作为plt
#用于输入单词的函数
def load_字典(word_字典):
对于word_字典中的单词:
让出word.strip()
#造字谜的功能
def生成语法(来源):
d=默认DICT(列表)
对于来源中的单词:
关键字=“连接(已排序(word))
d[关键字]。追加(word)
返回d
#用于打印字谜的函数
def打印字符(word输入):
计数=0
n=0
k=''
d=生成字谜(单词输入)
anagramLens=计数器()
对于键,d.iteritems()中的字谜:
anagramLens[len(anagrams)]+=1
如果len(字谜)>1:
计数+=1
如果n