Graphing A Dictionary With Strange Formatting In Python

- 1 answer

I have a dictionary in python like so:


What I'd like to do with this dictionary is make 5 overlayed line plots; one for each fg value, graphing the array of values in the corresponding item in the 'values' key over the number of units in the each of the 'values' arrays.

So, the first line graph would be for 'fg':'1', and would graph 5,6,7,8 as y values over 1,2,3,4 (the number of units in the array) as x values. The second line graph would be for 'fg':'2', and would graph 2,3,4,5 as y values over, again, 1,2,3,4 as x values. Please let me know if I can further clarify.

I'm not well versed in plotting dictionaries with this formatting so I don't even really know where to start. Any advice would be greatly appreciated!



It's pretty straight forward. We can just loop over the values field in your dictionary and plot each of those entries:

# import libraries
import numpy as np
import matplotlib.pyplot as plt

# define your data

for values in test['values']: # iterate over all "values" entries
    x = np.arange(1, values.shape[0]+1)  # set up x-vector
    plt.plot(x, values, '-')  # plot
plt.legend(test['fg'])  # optionally add legend  # create plo  # create plot