如何在水平条形图中绘制Counter对象?

3 投票
1 回答
4823 浏览
提问于 2025-04-17 21:06

我有一个计数器对象。我想画一个像这个链接里那样的横向条形图 http://matplotlib.org/examples/lines_bars_and_markers/barh_demo.html

我该怎么做呢?

1 个回答

3

这是你提到的页面上的一个例子,经过修改后使用了一个计数器对象:

 """
Simple demo of a horizontal bar chart.
"""
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt


# Counter data, counter is your counter object
keys = counter.keys()
y_pos = np.arange(len(keys))
# get the counts for each key, assuming the values are numerical
performance = [counter[k] for k in keys]
# not sure if you want this :S
error = np.random.rand(len(keys))

plt.barh(y_pos, performance, xerr=error, align='center', alpha=0.4)
plt.yticks(y_pos, keys)
plt.xlabel('Counts per key')
plt.title('How fast do you want to go today?')

plt.show()

撰写回答