Home > Blockchain >  How to Plot from a dictionary of lists?
How to Plot from a dictionary of lists?

Time:07-01

I have a dictionary similar to d = {'1':['5','6','7'], '2':['1','2','3'], '3':['9','8','7']} and a list like both = [0.5, 1.0, 1.5] and I would like to make a plot that has the list both as the x values and different lines (y value groups) being each of the keys in the dictionary so that the values of '1' is their own line s well as the values of '2' make up another. How can I use a for loop to separate the y values for the plot?

CodePudding user response:

I think this is similar to Iterating over dictionaries using 'for' loops and plot separate plots using dictionary of lists

both = [0.5,1.0,1.5]
d = {'1':['5','6','7'], '2':['1','2','3'], '3':['9','8','7']}
traces = []
counter = 0
for key, value in d.items():
     for index, num in enumerate(both): 
          traces[counter][num] = value[index]
     counter = counter  1

This would give you a list of dicitionaries where the keys in the dicitionaries are the x indices and the y values are the values. This would be super easy to plot with matplotlib

The traces list would be like:

traces = [
     { 0.5 : '5',1.0:'6',1.5:'7'},
     { 0.5 : '1',1.0:'2',1.5:'3'},
     { 0.5 : '9',1.0:'8',1.5:'7'}
]
  • Related