2017-09-16 382 views
0

目前,我有一個腳本這使得下面的柱狀圖最高值:排序x軸值matplotlib直方圖從最低到使用python

enter image description here

基於此數據:

{"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"} 

這是呈現數據的直方圖的代碼:

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) 

它被描繪這樣的:

# Transpose the data to get the x and y values 
labels, values = zip(*counts[0]) 

indexes = np.arange(len(labels)) 
width = 1 

plt.bar(indexes, values, width) 
plt.xticks(indexes + width * 0.5, labels) 
plt.show() 

的問題是,如何重新安排x軸,這樣他們才能從低到高,即

0, 1, 3, 4 

回答

1

我想既然你已經使用matplotlib,在pandas中更好地進行數據處理。

In [101]: JSON = '''[{"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"}] 
    .....: ''' 

In [102]: df = pd.read_json(JSON) 

In [103]: df 
Out[103]: 
    first second third 
0  A  1  2 
1  B  1  2 
2  C  2  2 
3  D  3  2 
4  E  3  2 
5  F  3  2 
6  G  3  2 
7  H  4  2 
8  I  4  2 
9  J  0  2 
10  K  0  2 
11  L  0  2 
12  M  0  2 
13  N  0  2 

In [104]: df.groupby('second').size().plot(kind='bar') 
Out[104]: <matplotlib.axes._subplots.AxesSubplot at 0x1104eac10> 

enter image description here

條形圖把你的類別以正確的順序。

但如果你只需要一個通用的方法把你的酒吧訂單,你可能只是建立一個臨時的數據幀,排序,然後劇情:

In [109]: pd.DataFrame({'Labels': labels, 
         'Values': values}).sort_values(['Labels']).plot(kind='bar', 
            x='Labels', 
            y='Values', 
            width=1.0) 

enter image description here

+0

,但是,從實際數據集預處理很重要 - 因爲它比玩具例子更大更復雜,所以 - 之後它不再是JSON格式。有數據通過預處理流水線後通過數據實現這一點嗎? –

+0

在這種情況下,您可以考慮構建一個臨時數據框,按標籤排序然後繪圖。請參閱編輯。 –