4

I have the following json file and I would like to make bar graphs based on bytes and timestamp. Noticed timestamps provided on the file are not in order, and they need to .

{
    "success":true,
        "data":[
            {

                "record_id":258585618,
                "timestamp":"2018-01-21 22:34:34",
                "bytes":29466,

            }
            ,
            {
                "record_id":258585604,
                "timestamp":"2018-01-21 22:33:14",
                "bytes":37892,
            }
            ,
            {
                "record_id":258584399,
                "timestamp":"2018-01-21 22:37:40",
                "bytes":36396,
            }
        ]
    }

Thank you very much!!!

0

1 Answer 1

4

Edit: I have now sorted the dates for the bar graph.

You could do something like this:

import json
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
data = json.dumps({
    "success":True,
        "data":[
            {

                "record_id":258585618,
                "timestamp":"2018-01-21 22:34:34",
                "bytes":29466,

            }
            ,
            {
                "record_id":258585604,
                "timestamp":"2018-01-21 22:33:14",
                "bytes":37892,
            }
            ,
            {
                "record_id":258584399,
                "timestamp":"2018-01-21 22:37:40",
                "bytes":36396,
            }
        ]
    })

data = json.loads(data)
dates = [i['timestamp'] for i in data["data"]]
values = [i['bytes'] for i in data['data']]

df = pd.DataFrame({'dates':dates, 'values':values})
df['dates']  = [pd.to_datetime(i) for i in df['dates']]

print(df.sort_values(by='dates'))

                dates  values
1 2018-01-21 22:33:14   37892
0 2018-01-21 22:34:34   29466
2 2018-01-21 22:37:40   36396

plt.bar(dates, values)

enter image description here

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

2 Comments

i just noticed that you need the time stamps to be ordered, should they also be the x-axis labels?
I think it is a cosmetic thing, but it would be appreciated if it can be also implemented.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.