1

At the moment I have a script which renders the following histogram:

enter image description here

Based on this data:

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

This is the code that renders the data for the histogram:

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)

It's being plotted like this:

# 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()

The question is, how to reorganize the x-axis so that they're order from lowest to highest, i.e.

0, 1, 3, 4

1 Answer 1

1

I think since you are already using matplotlib, it makes better sense to do the data wrangling in pandas as well.

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

The bar graph put your category in the right order.

But if you just need a general method to put your bars in order, you might just construct a temporary dataframe, sort it, and then plot:

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

Sign up to request clarification or add additional context in comments.

2 Comments

but on the real dataset the preprocessing is important- because it's much larger and more complicated than the toy example, and so- afterwards it's no longer in JSON format. Is there some way to achieve this with the data after it's passed through that preproessing pipeline?
In that case you might consider building a temporary dataframe, sort by labels and then plot. See edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.