【发布时间】:2016-02-11 08:24:24
【问题描述】:
我有一些 JSON 数据,我想分析它们的速度和相应的时间戳,我已经做到了这一点。我已经对其进行了反向排序,并使用 matplotlib 绘制了一个图表。现在我想输出具有相应值的键,但我只能打印速度键而不是时间戳键。
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
import pandas as pd
#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)
for e in sorted_waypoints:
for k, v in e.items():
if k == 'Speed' and v >= 6:
print k, v
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
}]
【问题讨论】:
标签: python json data-analysis