绘制连音符列表的直方图 matplotlib [英] Plotting histogram of list of tuplets matplotlib

查看:61
本文介绍了绘制连音符列表的直方图 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)]

我想对每个连音的频率做一个简单的直方图.怎么做呢?

I want to make a simple histogram of the frequency of each tuplet. How would one go about doing that?

标准的 plt.dist 似乎不太有效,也没有将tuplet映射到单个变量.

Standard plt.dist don't seem to quite work, nor does remapping the tuplets to a single variables.

推荐答案

直方图( numpy.hist plt.hist )通常位于连续数据上,即可以很容易地在垃圾箱中分开.

A histogram (numpy.hist, plt.hist) is usually on continuous data, that you can easily separate in bins.

您要在此处计算相同的元组:您可以使用 collection.Counter

Here you want to count the identical tuples: you can use 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 以直方图的方式绘制每个元组的计数.

After some formatting, you can use plt.bar to plot the count of each tuple, in a histogram fashion.

# 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)

这篇关于绘制连音符列表的直方图 matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆