Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib:创建直方图_Python_Matplotlib - Fatal编程技术网

Python Matplotlib:创建直方图

Python Matplotlib:创建直方图,python,matplotlib,Python,Matplotlib,编写代码,读取测试文件T1的内容,并使用10000个随机字典的随机字典记录婴儿床和T1翻译之间的最小汉明距离。使用matplotlib.pyplot.hist绘制结果的直方图 我已经有了记录最小汉明距离、翻译T1和生成随机字典的功能,但我不知道如何使用matplotlib为10000个字典生成直方图 我在想 plt.hist(min_ham_dist("crib",trans_cipher(T1,rand_dict()))) 应该在某个地方使用,因为这些是我函数的名称 您必须生成最小汉明距离值

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

我已经有了记录最小汉明距离、翻译T1和生成随机字典的功能,但我不知道如何使用matplotlib为10000个字典生成直方图

我在想

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

应该在某个地方使用,因为这些是我函数的名称

您必须生成最小汉明距离值的数组或列表。例如,在:

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


我可以添加标签,但只做plt.xlabel‘最小汉明距离’和plt.ylabel‘模拟次数’?是的,但这比在这里提问更快。此外,出于某种原因,直方图上有两个不同的彩色图,有没有办法把它变成一个呢?这可能是因为你的输入是一个由两个列表组成的列表或者类似的东西。将列表打印到屏幕上,并检查您试图绘制的内容,使列表包含10个元素,而不是10000个元素,以便于调试。
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()