2017-11-23 3 views
3

このプログラムを実行して、特定のテキストの文字の分布を検索しています。プロットがJ(インデックス10)で停止する理由を説明できます

# this is a paragraph from python documentation :) 
mytext = 'When a letter is first k encountered, it is missing from the mapping, so the default_factory function calls int() to supply a default count of zero. The increment operation then builds up the count for each letter.The function int() which always returns zero is just a special case of constant functions. A faster and more flexible way to create constant functions is to use a lambda function which can supply any constant value (not just zero):' 

d = dict() 
ignorelist = ('(',')',' ', ',', '.', ':', '_') 

for n in mytext: 
    if(n not in ignorelist): 
     n = n.lower() 
     if n in d.keys(): 
      d[n] = d[n] + 1 
     else: 
      d[n] = 1 
xx = list(d.keys()) 
yy = list(d.values()) 

import matplotlib.pyplot as plt 
plt.scatter(xx,yy, marker = '*') 
plt.show() 

リストには25個の要素があります。いくつかの奇妙な理由のために、プロットはこのように来ています。 x軸で 'J'で終わります。 enter image description here

ズームすると右側が表示されますが、ポイントはプロットされません。 enter image description here

+1

私はこれについて[GitHubのトラッカー上の問題](https://github.com/matplotlib/matplotlib/issues/9843)を開きました。 – ImportanceOfBeingErnest

答えて

4

これはmatplotlibのバージョン2.2

のように固定されることに注意してくださいあなたがmatplotlibの2.1の新機能カテゴリにバグを発見したようです。 1文字のカテゴリの場合、その機能は明らかに10項目に制限されます。カテゴリがより多くの文字で構成されている場合、これは起こりません。

いずれの場合でも、解決策は数​​値をプロットすることです(ちょうどmatplotlib 2.1より前に行う必要があったように)。次に、ティックラベルをカテゴリに設定します。

# this is a paragraph from python documentation :) 
mytext = 'When a letter is first k encountered, it is missing from the mapping, so the default_factory function calls int() to supply a default count of zero. The increment operation then builds up the count for each letter.The function int() which always returns zero is just a special case of constant functions. A faster and more flexible way to create constant functions is to use a lambda function which can supply any constant value (not just zero):' 

d = dict() 
ignorelist = ('(',')',' ', ',', '.', ':', '_') 

for n in mytext: 
    if(n not in ignorelist): 
     n = n.lower() 
     if n in d.keys(): 
      d[n] = d[n] + 1 
     else: 
      d[n] = 1 

xx,yy = zip(*d.items()) 

import numpy as np 
import matplotlib.pyplot as plt 

xx_sorted, order = np.unique(xx, return_inverse=True) 

plt.scatter(order,yy, marker="o") 
plt.xticks(range(len(xx)), xx_sorted) 
plt.show() 

enter image description here

関連する問題