Python 常用字符串前缀的直方图
所以我为自己制作了一本很好的单词前缀词典,但现在我想用matplotlib将其转换成一个好看的直方图。我对整个matplot场景还不熟悉,我没有看到任何其他问题 这是我的字典的一个例子Python 常用字符串前缀的直方图,python,matplotlib,scipy,Python,Matplotlib,Scipy,所以我为自己制作了一本很好的单词前缀词典,但现在我想用matplotlib将其转换成一个好看的直方图。我对整个matplot场景还不熟悉,我没有看到任何其他问题 这是我的字典的一个例子 {'aa':4, 'ca':6, 'ja':9, 'du':10, ... 'zz':1} 也许这会给你一个开始(在ipython--pylab中制作): [1]中的:来自itertools导入计数 在[2]中:前缀={'aa':4'ca':6'ja':9'du':10'zz':1} 在[3]中:条(*zip(
{'aa':4, 'ca':6, 'ja':9, 'du':10, ... 'zz':1}
也许这会给你一个开始(在
ipython--pylab中制作):
[1]中的:来自itertools导入计数
在[2]中:前缀={'aa':4'ca':6'ja':9'du':10'zz':1}
在[3]中:条(*zip(*zip(count(),前缀.values()))
出[3]:
在[4]中:xticks(*zip(*zip(计数(0.4),前缀)))
相关文件:
我会使用pandas,因为它内置了:
熊猫使用了numpy
和matplotlib
,但使一些事情更方便
您可以这样简单地绘制结果:
In [26]: s = pd.Series({'aa':4, 'ca':6, 'ja':9, 'du':10, 'zz':1})
In [27]: s.plot(kind='bar', rot=0)
Out[27]: <matplotlib.axes.AxesSubplot at 0x5720150>
[26]中的s=pd.Series({'aa':4,'ca':6,'ja':9,'du':10,'zz':1})
在[27]中:s.plot(kind='bar',rot=0)
出[27]:
# create some example data
In [266]: words = np.asarray(['aafrica', 'Aasia', 'canada', 'Camerun', 'jameica',
'java', 'duesseldorf', 'dumont', 'zzenegal', 'zZig'])
In [267]: many_words = words.take(np.random.random_integers(words.size - 1,
size=1000))
# convert to pandas Series
In [268]: s = pd.Series(many_words)
# show which words are in the Series
In [269]: s.value_counts()
Out[269]:
zZig 127
Camerun 127
Aasia 116
canada 115
dumont 110
jameica 109
zzenegal 108
java 105
duesseldorf 83
# using vectorized string methods to count all words with same first two
# lower case strings as an example
In [270]: s.str.lower().str[:2].value_counts()
Out[270]:
ca 242
zz 235
ja 214
du 193
aa 116
In [26]: s = pd.Series({'aa':4, 'ca':6, 'ja':9, 'du':10, 'zz':1})
In [27]: s.plot(kind='bar', rot=0)
Out[27]: <matplotlib.axes.AxesSubplot at 0x5720150>