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

标签 python matplotlib

我有一个连音列表

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.histplt.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)

tuple bar plot

关于python - 绘制连音符列表的直方图 matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35084574/

相关文章:

python - MemoryError 与 numpy arange

python-3.x - 在 Cartopy 中使用 Google map 图 block

python - 具有可扩展/可更新 x 轴的 Matplotlib 图

python - inpolygon - matplotlib.path.Path contains_points() 方法的示例?

python - 使嵌套的 'for' 循环更像 pythonic

python - 将 celery 优先级队列与广播任务一起使用

python - 如何在Windows 7中使用Python 3.3.4在Django 1.5.5中运行mysql

python - 将普通代码转换为 3AC - 三地址代码

python - matplotlib 补丁可以与函数一起使用吗?

python - 在 OpenCV 中使用 createTrackbar() 方法制作绘画应用程序时,EVENT_MOUSEMOVE 不起作用?