使用一系列值及其频率作为字典绘制直方图 [英] Plotting a histogram using a range of values and their frequency as a dictionary

查看:68
本文介绍了使用一系列值及其频率作为字典绘制直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下字典:

scenario_summary = {'Day1': {'22459-22585': 0.0, '22585-22711': 0.0, '22711-22837': 0.0, '22837-22963': 0.0, '22963-23089': 0.0, '23089-23215': 0.0, '23215-23341': 0.0, '23341-23467': 0.0, '23467-23593': 0.0, '23593-23719': 0.0, '23719-23845': 0.0, '23845-23971': 0.0, '23971-24097': 0.0, '24097-24223': 0.0, '24223-24349': 0.0, '24349-24475': 0.0, '24475-24601': 0.0, '24601-24727': 0.0, '24727-24853': 0.0, '24853-24979': 0.0, '24979-25105': 0.0, '25105-25231': 0.0, '25231-25357': 0.0, '25357-25483': 0.0, '25483-25609': 0.0, '25609-25735': 0.0, '25735-25861': 0.0, '25861-25987': 0.0, '25987-26113': 1.0, '26113-26239': 1.0, '26239-26365': 0.0, '26365-26491': 2.0, '26491-26617': 5.0, '26617-26743': 5.0, '26743-26869': 5.0, '26869-26995': 12.0, '26995-27121': 19.0, '27121-27247': 7.000000000000001, '27247-27373': 11.0, '27373-27499': 15.0, '27499-27625': 7.000000000000001, '27625-27751': 4.0, '27751-27877': 4.0, '27877-28003': 2.0, '28003-28129': 0.0, '28129-28255': 0.0, '28255-28381': 0.0, '28381-28507': 0.0, '28507-28633': 0.0, '28633-28759': 0.0, '28759-28885': 0.0, '28885-29011': 0.0, '29011-29137': 0.0, '29137-29263': 0.0, '29263-29389': 0.0, '29389-29515': 0.0, '29515-29641': 0.0, '29641-29767': 0.0, '29767-29893': 0.0, '29893-30019': 0.0, '30019-30145': 0.0, '30145-30271': 0.0, '30271-30397': 0.0, '30397-30523': 0.0, '30523-30649': 0.0, '30649-30775': 0.0, '30775-30901': 0.0, '30901-31027': 0.0, '31027-31153': 0.0, '31153-31279': 0.0, '31279-31405': 0.0, '31405-31531': 0.0, '31531-31657': 0.0, '31657-31783': 0.0, '31783-31909': 0.0, '31909-32035': 0.0, '32035-32161': 0.0, '32161-32287': 0.0, '32287-32413': 0.0, '32413-32539': 0.0, '32539-32665': 0.0, '32665-32791': 0.0, '32791-32917': 0.0, '32917-33043': 0.0, '33043-33169': 0.0, '33169-33295': 0.0, '33295-33421': 0.0, '33421-33547': 0.0, '33547-33673': 0.0, '33673-33799': 0.0, '33799-33925': 0.0, '33925-34051': 0.0, '34051-34177': 0.0, '34177-34303': 0.0, '34303-34429': 0.0, '34429-34555': 0.0, '34555-34681': 0.0, '34681-34807': 0.0}}

如您所见,字典由一系列字符串值及其频率组成.我想将其绘制为直方图,但是我不知道如何将字符串转换为大熊猫或情节理解的形式.您的方法是什么?还是有比硬编码事情更简单的方法呢?还是,另一个模块会更容易选择吗?

As you can see, the dictionary consists of a range of values in string and their frequency. I would like to plot this as a histogram, but I don't know how I would be able to transform the string into a form that pandas or plotly would understand. What would your approach be? Or is there an easier way to do it, instead of hardcoding things? Or, would another module be easier option in doing so?

谢谢!

推荐答案

直方图基本上是一个简单的条形图,其中每个条形图代表一个bin(通常以范围的形式)和落入该元素的频率那个垃圾箱.

A histogram is basically a simple bar chart, where each bar represents a bin (usually in the form of a range) and a frequency of the elements that fall into that bin.

这正是您已经拥有的数据.因此,不要使用直方图的计算值(就像使用

This is exactly the data that you already have. So instead of computing values for a histogram (as it would be done with plt.hist), you can simply pass your data to plt.bar, as it is. The result would then be this:

包含数据的代码,作为 MCVE :

The code with your data, as a MCVE :

import matplotlib.pyplot as plt

scenario_summary = { 'Day1': {
    '22459-22585': 0.0, '22585-22711': 0.0, '22711-22837': 0.0,
    '22837-22963': 0.0, '22963-23089': 0.0, '23089-23215': 0.0,
    '23215-23341': 0.0, '23341-23467': 0.0, '23467-23593': 0.0,
    '23593-23719': 0.0, '23719-23845': 0.0, '23845-23971': 0.0,
    '23971-24097': 0.0, '24097-24223': 0.0, '24223-24349': 0.0,
    '24349-24475': 0.0, '24475-24601': 0.0, '24601-24727': 0.0,
    '24727-24853': 0.0, '24853-24979': 0.0, '24979-25105': 0.0,
    '25105-25231': 0.0, '25231-25357': 0.0, '25357-25483': 0.0,
    '25483-25609': 0.0, '25609-25735': 0.0, '25735-25861': 0.0,
    '25861-25987': 0.0, '25987-26113': 1.0, '26113-26239': 1.0,
    '26239-26365': 0.0, '26365-26491': 2.0, '26491-26617': 5.0,
    '26617-26743': 5.0, '26743-26869': 5.0, '26869-26995': 12.0,
    '26995-27121': 19.0, '27121-27247': 7.0, '27247-27373': 11.0,
    '27373-27499': 15.0, '27499-27625': 7.0, '27625-27751': 4.0,
    '27751-27877': 4.0, '27877-28003': 2.0, '28003-28129': 0.0,
    '28129-28255': 0.0, '28255-28381': 0.0, '28381-28507': 0.0,
    '28507-28633': 0.0, '28633-28759': 0.0, '28759-28885': 0.0,
    '28885-29011': 0.0, '29011-29137': 0.0, '29137-29263': 0.0,
    '29263-29389': 0.0, '29389-29515': 0.0, '29515-29641': 0.0,
    '29641-29767': 0.0, '29767-29893': 0.0, '29893-30019': 0.0,
    '30019-30145': 0.0, '30145-30271': 0.0, '30271-30397': 0.0,
    '30397-30523': 0.0, '30523-30649': 0.0, '30649-30775': 0.0,
    '30775-30901': 0.0, '30901-31027': 0.0, '31027-31153': 0.0,
    '31153-31279': 0.0, '31279-31405': 0.0, '31405-31531': 0.0,
    '31531-31657': 0.0, '31657-31783': 0.0, '31783-31909': 0.0,
    '31909-32035': 0.0, '32035-32161': 0.0, '32161-32287': 0.0,
    '32287-32413': 0.0, '32413-32539': 0.0, '32539-32665': 0.0,
    '32665-32791': 0.0, '32791-32917': 0.0, '32917-33043': 0.0,
    '33043-33169': 0.0, '33169-33295': 0.0, '33295-33421': 0.0,
    '33421-33547': 0.0, '33547-33673': 0.0, '33673-33799': 0.0,
    '33799-33925': 0.0, '33925-34051': 0.0, '34051-34177': 0.0,
    '34177-34303': 0.0, '34303-34429': 0.0, '34429-34555': 0.0,
    '34555-34681': 0.0, '34681-34807': 0.0}}

data = scenario_summary['Day1']

x = range(len(data))
y = list(data.values())

plt.figure(figsize=(16, 9))
plt.bar(x, y)
plt.subplots_adjust(bottom=0.2)
plt.xticks(x, data.keys(), rotation='vertical')
plt.show()

这篇关于使用一系列值及其频率作为字典绘制直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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