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_Arrays_Matplotlib - Fatal编程技术网

Python 来自数组的直方图matplotlib

Python 来自数组的直方图matplotlib,python,arrays,matplotlib,Python,Arrays,Matplotlib,我有两个阵列: 例如: a=[1, 2, 3, 4] b=[10, 20, 30, 40] 根据这些数字,我需要绘制直方图,我知道用坐标(1,10),(2,20)等绘制曲线很容易,但是如何从数组绘制直方图。目前我只能选择绘制直方图。任何建议都可以 import matplotlib.pyplot as plt import numpy as np a = [([97, 99, 99, 96, 97, 98, 99, 97, 99, 99, 96, 97, 99, 99, 95,

我有两个阵列:

例如:

a=[1, 2, 3, 4]

b=[10, 20, 30, 40]
根据这些数字,我需要绘制直方图,我知道用坐标(1,10),(2,20)等绘制曲线很容易,但是如何从数组绘制直方图。目前我只能选择绘制直方图。任何建议都可以

import matplotlib.pyplot as plt
import numpy as np

a = [([97, 99, 99, 96, 97, 98, 99, 97, 99, 99, 96, 97, 99, 99, 95,
       98, 99, 97, 97, 98, 97, 96, 98, 98, 98, 98, 98, 98, 96, 98, 98, 98, 98,
       98, 98, 96, 97, 97, 97, 97, 97, 96, 96, 97, 97, 96, 95, 97, 96, 97, 96, 97,
       96, 95, 96, 97, 95, 95, 93, 93, 92, 93, 93, 95, 95, 94, 93, 94, 94, 95, 95, 95,
       95, 96, 96, 95, 96, 96, 96, 96, 94, 95, 90, 95, 95, 95, 95,
       95, 88, 94, 94, 93, 95, 95, 94, 95, 95, 95, 95, 95, 93],)]

for item in a[0]:
    s = item

lengths = len(s)
s2 = [s[x:x+9] for x in xrange(0, len(s), 9)]
print s2.index(min(s2))

test = 2400+int(lengths)
xaxis=range(2400,test)
yaxis=s
下面是图像示例,x轴是2400-2500之间的值,y轴是某种数组中的值


您可以使用在matplotlib中创建直方图。考虑到问题中的代码,如果希望
a
中的值为2400到2500中的值的频率,这可以简单地执行以下操作:

plt.hist(range(2400, 2501), weights=a[0][0])
plt.show()
这样做会从
a
生成一个直方图,默认为10个箱子,如下所示

然而,这里有一点奇怪,因为
a
有101个值(这意味着绘制的范围是2400到2500,包括2400到2500),所以最后一个存储单元获取11个值的频率计数,而其他存储单元获取10个值的频率计数。您可以使用以下内容为每个值指定其自己的bin

plt.hist(range(2400, 2501), weights=a[0][0], bins=101)
plt.show()
这将生成下面的图像


它显示了我需要的东西,但不是真正需要的。我需要x轴是从2400到2500的值,y轴(直方图的高度是从数组a开始的)。增加了图像的外观。