2017-09-16 29 views
0

我与编码像这样的数据集工作:使用Python把一个元组列表成柱状图/条形图

[ 
    [ 
     (u'90000', 100318), 
     (u'21000', 58094), 
     (u'50000', 14695), 
     (u'250000', 8190), 
     (u'100000', 5718), 
     (u'40000', 4276) 
    ] 
] 

我想它transmogrify成柱状图/条形图。

我一直在寻找XXX,迄今为止我试图像这样:

fig, ax = plt.subplots() 
ax.yaxis.set_major_formatter(formatter) 
plt.bar(x, counts) 
plt.xticks(counts[0], counts[1]) 

plt.xticks(rotation=70) 
plt.show() 

然而,所产生的错误:

NameError: name 'formatter' is not defined 

用于产生数据结构的代码看起来是这样的:

with open('toy_two.json', 'rb') as inpt: 

    dict_hash_gas = list() 
    for line in inpt: 
     resource = json.loads(line) 
     dict_hash_gas.append({resource['first']:resource['second']}) 

# Count up the values 
counts = collections.Counter(v for d in dict_hash_gas for v in d.values()) 

counts = counts.most_common() 

# Apply a threshold 
threshold = 4275 
counts = [list(group) for val, group in itertools.groupby(counts, lambda x: x[1] > threshold) if val] 

print(counts) 

而且这样的数据:

{"first":"A","second":"1","third":"2"} 
{"first":"B","second":"1","third":"2"} 
{"first":"C","second":"2","third":"2"} 
{"first":"D","second":"3","third":"2"} 
{"first":"E","second":"3","third":"2"} 
{"first":"F","second":"3","third":"2"} 
{"first":"G","second":"3","third":"2"} 
{"first":"H","second":"4","third":"2"} 
{"first":"I","second":"4","third":"2"} 
{"first":"J","second":"0","third":"2"} 
{"first":"K","second":"0","third":"2"} 
{"first":"L","second":"0","third":"2"} 
{"first":"M","second":"0","third":"2"} 
{"first":"N","second":"0","third":"2"} 

问题:

要清楚的问题是:如何使这个帖子开头的数据,即

[ 
    [ 
     (u'90000', 100318), 
     (u'21000', 58094), 
     (u'50000', 14695), 
     (u'250000', 8190), 
     (u'100000', 5718), 
     (u'40000', 4276) 
    ] 
] 

为直方图?

x轴将是u'90000',u'21000',...,u'40000'

y轴将是100318,58094,...,4276

+0

什么问题? – wwii

+0

问题是 - 如何将该数据呈现为直方图 –

+0

什么是'plt'?和'无花果'和'斧头'? – wwii

回答

2
data = [ 
    [ 
     (u'90000', 100318), 
     (u'21000', 58094), 
     (u'50000', 14695), 
     (u'250000', 8190), 
     (u'100000', 5718), 
     (u'40000', 4276) 
    ] 
] 

移调数据,以获得x和y的值

#data = data[0] 
#x, y = zip(*data) 
x, y = zip(*data[0]) 

压缩的y值,使他们适合在屏幕上

import math 
y = [int(math.log(n, 1.5)) for n in y] 

遍历数据,并创建直方图

for label, value in zip(x, y): 
    print('{:>10}: {}'.format(label, 'x'*value)) 


>>> 
    90000: xxxxxxxxxxxxxxxxxxxxxxxxxxxx 
    21000: xxxxxxxxxxxxxxxxxxxxxxxxxxx 
    50000: xxxxxxxxxxxxxxxxxxxxxxx 
    250000: xxxxxxxxxxxxxxxxxxxxxx 
    100000: xxxxxxxxxxxxxxxxxxxxx 
    40000: xxxxxxxxxxxxxxxxxxxx 
>>> 
+1

'x,y = zip(* data [0])'可能更好,因此您不会改变原始数据。 – Alexander

+0

所以 - 这是完全可怕的 - 和超级创意 - 但我更像是想,把它放在mat plot lib或类似的东西里,你知道吗? –

+1

@ s.matthew.english - 我不知道:关于这个问题没有任何问题。 – wwii