Can you explain why the plot is stopping at J (at index 10)

烈酒焚心 提交于 2019-11-28 05:08:45

问题


I am running this program to find the distribution of characters in a specific text.

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

Both the list has 25 elements. For some strange reason the plot is coming like this. It ends in 'J' in x axis.

If I zoom it the right side gets visible but there is no points plotted.


回答1:


Note that this will be fixed as of matplotlib version 2.2

It seems you have found a bug in the new categorical feature of matplotlib 2.1. For single letter categories it will apparently limit its functionality to 10 items. If categories consist of more letters, this does not happen.

In any case, a solution is to plot numerical values (just as one would have needed to do prior to matplotlib 2.1 anyways). Then set the ticklabels to the categories.

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



来源:https://stackoverflow.com/questions/47464242/can-you-explain-why-the-plot-is-stopping-at-j-at-index-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!