2016-02-10 74 views
0

我有一個json文件,我正在反向排序使用natsort,然後我想繪製一個「速度」的圖形,但我得到一個錯誤。我現在在這裏也包含了JSON文件。AssertionError:不兼容的尺寸:參數'高度'必須是長度5或標量

"AssertionError: incompatible sizes: argument 'height' must be length 5 or scalar" 

Traceback (most recent call last): File "new.py", line 71, in <module> visualize_type(sorted_waypoints) File "new.py", line 45, in visualize_type plt.bar(xlocations, counter.values()) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matp‌​lotlib/pyplot.py", line 2515, in bar ret = ax.bar(left, height, width=width, bottom=bottom, **kwargs) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matp‌​lotlib/axes.py", line 4999, in bar nbars) 

代碼:

import json 
import datetime 
import pprint 
from operator import itemgetter 
import natsort 
import matplotlib.pyplot as plt 
import numpy as np 
from collections import Counter 



#path to gps data file in json format. 
data_file = "waypoints.json" 


def speed_ans(self, data_file): 
    pass 



def visualize_type(output): 
    """Visualize data by category in a bar graph""" 

    #This returns a dict where it sums the total per Category. 
    counter = Counter(item["Speed"] for item in output) 

    # Set the labels which are based on the keys of our counter. 
    labels = tuple("Speed") 

    # Set where the labels hit the x-axis 
    xlocations = np.arange(len(labels)) + 0.5 

    # Width of each bar 
    width = 0.5 

    # Assign data to a bar plot 
    plt.bar(xlocations, counter.values(), width=width) 

    # Assign labels and tick location to x- and y-axis 
    plt.xticks(xlocations + width/2, labels, rotation=90) 
    plt.yticks(range(0, max(counter.values()), 5)) 

    # Give some more room so the labels aren't cut off in the graph 
    plt.subplots_adjust(bottom=0.4) 

    # Make the overall graph/figure larger 
    plt.rcParams['figure.figsize'] = 12, 8 

    # Save the graph! 
    plt.savefig("Graph.png") 

    plt.clf() 



if __name__ == '__main__': 
    with open(data_file) as f: 
     waypoints = json.load(f) 

    sorted_waypoints = natsort.natsorted(waypoints, key=itemgetter(*['Speed']), reverse = True) 
    pprint.pprint(sorted_waypoints) 

    visualize_type(sorted_waypoints) 

waypoints.json文件:

[{ 
    "Latitude": 1.282143333, 
    "Timestamp": 1434368770, 
    "Speed": 7.696, 
    "Longitude": 103.850785 
}, { 
    "Latitude": 1.282205, 
    "Timestamp": 1434368771, 
    "Speed": 7.233, 
    "Longitude": 103.850806667 
}, { 
    "Latitude": 1.282205, 
    "Timestamp": 1434368772, 
    "Speed": 7.233, 
    "Longitude": 103.850806667 
}, { 
    "Latitude": 1.282205, 
    "Timestamp": 1434368773, 
    "Speed": 7.444, 
    "Longitude": 103.850806667 
}, { 
    "Latitude": 1.282261667, 
    "Timestamp": 1434368774, 
    "Speed": 6.933, 
    "Longitude": 103.850833333 
}] 
+2

您能否提供AssertionError的整個Traceback? – MSeifert

+0

@ MSeifert是的,當然。 回溯(最近通話最後一個): 文件 「new.py」,71行,在 visualize_type(sorted_waypoints) 文件 「new.py」 45行,在visualize_type plt.bar(xlocations,counter.values ()) 文件「/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py」,line 2515,in bar ret = ax.bar(left,height, width = width,bottom = bottom,** kwargs) 文件「/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py」,第4999行,在 nbars ) – Dynamic

+1

請編輯您的答案,添加回溯使其更具可讀性。謝謝。 – lrnzcig

回答

1

我想你做什麼tuple()確實爲字符串的不正確的假設:

tuple("Speed") 
# returns ('S', 'p', 'e', 'e', 'd') 

我想你想要:

labels = counter.keys() 
+0

感謝您糾正我。我更換了線,但我得到了這個錯誤。回溯(最近通話最後一個): 文件 「new.py」,71行,在 visualize_type(sorted_waypoints) 文件 「new.py」 36行,在visualize_type 標籤= x.keys() NameError:全局名稱'x'未定義 – Dynamic

+0

對不起,我一直對變量名稱感到困惑。編輯答案。必須是''counter.keys()''。對不起 – MSeifert

+0

''plt.plot([item] [item]輸出中的項目[「TimeStamp」],[item] [item] []速度]]''但這與這個問題無關。如果您有任何其他問題或相關問題,請打開另一個問題,然後按照[mcve](http://stackoverflow.com/help/mcve)進行操作,然後人們會爲您提供幫助。 – MSeifert

相關問題