Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I recently wrote something similar to the following to help make some task at work quicker:

import argparse
import json
import matplotlib.pyplot as plt

def main():
    '''Plot an arbitrary number of graphs, given input json files'''
    parser = argparse.ArgumentParser()
    parser.add_argument('files', nargs='+')
    args = parser.parse_args()
    for filename in args.files:
        x_axis = list()
        y_axis = list()
        with open(filename) as infile:
            for line in infile:
                json_line = json.loads(line)
                x_axis.append(json_line['time'])
                y_axis.append(json_line['some_magnitude'])
        plt.plot(x_axis, y_axis, label=filename.strip('.json'))
    plt.xlabel('time (s)')
    plt.ylabel('some_magnitude')
    plt.grid(True)
    plt.legend()
    plt.show()

if __name__ == '__main__':
    main()

As can be seen, each line on the log file is a json object. It has a bunch of fields, but for graphing I'm only interested in 2, time and some_magnitude.

Running this would be something like:

python script.py logfile-1.json logfile-2.json
share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.