【问题标题】:How to plot Counter object in horizontal bar chart?如何在水平条形图中绘制计数器对象?
【发布时间】:2014-04-08 23:25:30
【问题描述】:

我有一个 Counter 对象。我想绘制这样的水平条形图http://matplotlib.org/examples/lines_bars_and_markers/barh_demo.html

我该怎么做?

【问题讨论】:

    标签: python python-3.x matplotlib counter


    【解决方案1】:

    这是您提到的页面中修改为使用计数器对象的示例:

     """
    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()
    

    【讨论】:

    • 如果您希望键按值排序怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多