0
目前,我有一个脚本这使得下面的柱状图最高值:排序x轴值matplotlib直方图从最低到使用python
基于此数据:
{"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
,但是,从实际数据集预处理很重要 - 因为它比玩具例子更大更复杂,所以 - 之后它不再是JSON格式。有数据通过预处理流水线后通过数据实现这一点吗? –
在这种情况下,您可以考虑构建一个临时数据框,按标签排序然后绘图。请参阅编辑。 –