python-3.x - 你能解释一下为什么情节在 J 处停止(在索引 10 处)

标签 python-3.x matplotlib scatter-plot

我正在运行这个程序来查找特定文本中的字符分布。

# 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

最佳答案

请注意,这将从 matplotlib 版本 2.2 开始得到修复

您似乎在 matplotlib 2.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

关于python-3.x - 你能解释一下为什么情节在 J 处停止(在索引 10 处),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47464242/

相关文章:

python - 对对象方法使用 Python 模拟的 autospec 的正确方法是什么?

python - 两条线之间的填充区域(当一条线低于另一条线时)

javascript - d3.缺失数据。仅绘制每组中的初始值

javascript - d3 tickFormat 错误 - 将 v3 转换为 v4

python-3.x - Matplotlib - 散点图,如何填充每个点之间的空间?

python - 将空行添加到 python 数据框中

Python 3,按顺序获取 IP 地址和 Ping 的文本文件

Python如何在不删除已有内容的情况下继续写入文件

python - 绘制二维矩阵中特征的存在情况

python - matplotlib动画电影: quality of movie decreasing with time