【问题标题】:Plot a histogram from a Dictionary从字典中绘制直方图
【发布时间】:2014-02-07 07:57:01
【问题描述】:

我创建了一个 dictionary 来计算每个键在 list 中的出现次数,现在我想绘制其内容的直方图。

这是我要绘制的字典的内容:

{1: 27, 34: 1, 3: 72, 4: 62, 5: 33, 6: 36, 7: 20, 8: 12, 9: 9, 10: 6, 11: 5, 12: 8, 2: 74, 14: 4, 15: 3, 16: 1, 17: 1, 18: 1, 19: 1, 21: 1, 27: 2}

到目前为止,我写了这个:

import numpy as np
import matplotlib.pyplot as plt

pos = np.arange(len(myDictionary.keys()))
width = 1.0     # gives histogram aspect to the bar diagram

ax = plt.axes()
ax.set_xticks(pos + (width / 2))
ax.set_xticklabels(myDictionary.keys())

plt.bar(myDictionary.keys(), ******, width, color='g')
#                            ^^^^^^ what should I put here?
plt.show()

我只是简单地尝试了

plt.bar(myDictionary.keys(), myDictionary, width, color='g')

但这是结果:

我不知道为什么这 3 个条形图会移动,而且我希望直方图以有序的方式显示。

谁能告诉我怎么做?

【问题讨论】:

    标签: python dictionary matplotlib histogram


    【解决方案1】:

    您可以使用该函数绘制直方图,如下所示:

    a = np.random.random_integers(0,10,20) #example list of values
    plt.hist(a)
    plt.show()
    

    或者你可以像这样使用myDictionary

    plt.bar(myDictionary.keys(), myDictionary.values(), width, color='g')
    

    【讨论】:

      【解决方案2】:

      对于 Python 3,您需要使用 list(your_dict.keys()) 而不是 your_dict.keys()(否则您会得到 TypeError: 'dict_keys' object does not support indexing):

      import matplotlib.pyplot as plt
      
      dictionary = {1: 27, 34: 1, 3: 72, 4: 62, 5: 33, 6: 36, 7: 20, 8: 12, 9: 9, 10: 6, 11: 5, 
                    12: 8, 2: 74, 14: 4, 15: 3, 16: 1, 17: 1, 18: 1, 19: 1, 21: 1, 27: 2}
      plt.bar(list(dictionary.keys()), dictionary.values(), color='g')
      plt.show()
      

      使用 Matplotlib 2.0.0 和 python 3.5 测试。

      仅供参考:Plotting a python dict in order of key values

      【讨论】:

        【解决方案3】:
        values = [] #in same order as traversing keys
        keys = [] #also needed to preserve order
        for key in myDictionary.keys():
          keys.append(key)
          values.append(myDictionary[key])
        

        使用“键”和“值”。这确保了订单被保留。

        【讨论】:

        • 在标准 Python 实现中,您不需要这样做,正如 here 所解释的那样
        • 我认为他的意思是使用x,y = zip(myDictionary.items())
        【解决方案4】:

        如果您真的想使用plt.hist 函数(例如使用 bins 关键字),您可以随时将您的 Counter 转换为列表

        使用您的代码:

        mydict = {1: 27, 34: 1, 3: 72, 4: 62, 5: 33, 6: 36, 7: 20, 8: 12, 9: 9, 10: 6, 11: 5, 12: 8, 2: 74, 14: 4, 15: 3, 16: 1, 17: 1, 18: 1, 19: 1, 21: 1, 27: 2}
        
        mylist = [key for key, val in mydict.items() for _ in range(val)]
        plt.hist(mylist, bins=20)
        

        将输出

        plt.hist(mylist, bins=5)
        

        将输出

        【讨论】:

          【解决方案5】:

          如果myDictionary的key不是统一分布的,使用str的key很有帮助:

          plt.bar([ str(i) for i in myDictionary.keys()], myDictionary.values(), color='g')
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-06-28
            • 1970-01-01
            • 2018-05-22
            • 1970-01-01
            • 2020-10-17
            • 1970-01-01
            • 1970-01-01
            • 2014-08-08
            相关资源
            最近更新 更多