gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:26:07 24 4
gpt4 key购买 nike

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

# 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com