我有一個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/matplotlib/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/matplotlib/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
}]
您能否提供AssertionError的整個Traceback? – MSeifert
@ 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
請編輯您的答案,添加回溯使其更具可讀性。謝謝。 – lrnzcig