Python Matplotlib:创建直方图

2024-05-17 12:37:26 发布

您现在位置:Python中文网/ 问答频道 /正文

编写代码,读取测试文件“T1”的内容,并用10000个随机字典的随机字典记录婴儿床和“T1”翻译之间的最小汉明距离。使用matplotlib.pyplot.hist文件绘制结果的直方图。你知道吗

我已经有了记录最小汉明距离、翻译“T1”以及生成随机字典的函数,但我不知道如何使用matplotlib为10000个字典生成直方图。你知道吗

我在想

plt.hist(min_ham_dist("crib",trans_cipher(T1,rand_dict())))

应该用在某个地方,因为这些是我的函数名。你知道吗


Tags: 文件函数代码距离内容字典matplotlib记录
1条回答
网友
1楼 · 发布于 2024-05-17 12:37:26

您必须生成最小汉明距离值的数组或列表。例如(在Jupyter中测试):

import random
import matplotlib.pyplot as plt

##### This block is only dummy methods so that is compatible with your example
def rand_dict(): return None
def trans_cipher(a, b): return None
def min_ham_dist(a, b): return random.randint(0, 1000)
T1 = None
#####

arr = [min_ham_dist("crib",trans_cipher(T1,rand_dict())) for i in range(10000)] # This is the important point
plt.hist(arr, bins=20)
plt.show()

在本例中,我使用列表理解(PEP 202)快速构建随机值列表。结果应该是这样的:

dummy histogram using matplotlib hist method

相关问题 更多 >