18

何千もの要素とそれぞれの周波数を含むデータセットがあります。上位 10 個の要素のヒストグラムをプロットする必要があります。
やった:

  top_words = Counter(my_data).most_common()  
  top_words_10 = top_words[:10]  
  plt.hist(top_words_10,label='True')    

このエラーが発生しました:

TypeError                                   
  Traceback (most recent call last) 
<ipython-input-29-ff974b3a2354> in <module>()  
      5  print top_words[:10]  
      6   
----> 7 plt.hist(top_words_10)    
C:\Anaconda\lib\site-packages\numpy\core\_methods.pyc in _amin(a, axis, out, keepdims)  
     12 def _amin(a, axis=None, out=None, keepdims=False):  
     13     return um.minimum.reduce(a, axis=axis,  
---> 14                             out=out, keepdims=keepdims)  
     15   
     16 def _sum(a, axis=None, dtype=None, out=None, keepdims=False):  


TypeError: cannot perform reduce with flexible type

何か案が??私のデータは次のようになります:

[(' whitefield', 65299), (' bellandur', 57061), (' kundalahalli', 51769), (' marathahalli', 50639), (' electronic city', 44041), (' sarjapur road junction', 34164), (' indiranagar 2nd stage', 32459), (' malleswaram', 32171), (' yelahanka main road', 28901), (' domlur', 28869)]
4

2 に答える 2

0

問題は、渡されたデータからヒストグラムを作成plt.histしようとすることです。nmupy.hist

あなたはただ使いたいbar

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)
words, counts = zip(*data32)  # unpack pairs into two lists
ax.bar(range(len(counts)), words, align='center')
ax.set_xticks(range(len(counts))
ax.set_xticklabels(words)  # this is about the _only_ use for set_xticklabels
plt.draw

この例ドキュメントを参照してください。

于 2014-01-31T05:07:52.730 に答える