绘制连音符列表的直方图 matplotlib

2024-01-29

我有一个连音列表

k = [(8, 8),(10, 10),(8, 8),
 (8, 8),(12, 12),(7, 7),(8, 8),
 (9, 9),(10, 10),(10, 10),(8, 8),(9, 9),(13, 13),
 (10, 10),(8, 8),(8, 8),(7, 7)]

我想制作每个连音频率的简单直方图。一个人会怎样做呢?

标准plt.dist似乎不太有效,将连音重新映射到单个变量也不起作用。


直方图(numpy.hist, plt.hist)通常针对连续数据,您可以轻松地将其分入箱中。

这里你想要计算相同的元组:你可以使用collection.Counter

from collections import Counter
k = [(8, 8),(10, 10),(8, 8),
 (8, 8),(12, 12),(7, 7),(8, 8),
 (9, 9),(10, 10),(10, 10),(8, 8),(9, 9),(13, 13),
 (10, 10),(8, 8),(8, 8),(7, 7)]

c=Counter(k)
>>> Counter({(8, 8): 7, (10, 10): 4, (9, 9): 2, (7, 7): 2, (13, 13): 1, (12, 12): 1})

经过一些格式化后,您可以使用plt.bar以直方图的方式绘制每个元组的计数。

# x axis: one point per key in the Counter (=unique tuple)
x=range(len(c))
# y axis: count for each tuple, sorted by tuple value
y=[c[key] for key in sorted(c)]
# labels for x axis: tuple as strings
xlabels=[str(t) for t in sorted(c)]

# plot
plt.bar(x,y,width=1)
# set the labels at the middle of the bars
plt.xticks([x+0.5 for x in x],xlabels)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

绘制连音符列表的直方图 matplotlib 的相关文章

随机推荐