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?
Asked
Active
Viewed 54 times
-1

birbarchimedes
- 3
- 3
-
Wait so you want a different trace on the plot for each key in the dictionary? Or do you want the ith index of the list to be its own plot while the keys are the x values? – Shavk with a Hoon Jun 30 '22 at 13:59
-
My apologies. I am wanting a different trace for each key where the y-values are the values of that key. The x values will come from the list 'both'. So for this plot there would be 3 trace lines since there are 3 keys. – birbarchimedes Jun 30 '22 at 14:03
-
hm ok. So for example, the first trace would have points (0,5,5),(1.0,6),(1.5,7)? – Shavk with a Hoon Jun 30 '22 at 14:08
-
Yes! Exactly. That way the different traces have the same x-values for comparison reasons. – birbarchimedes Jun 30 '22 at 14:13
-
Ok, you should be able to do it directly from those lists but if you want to break it out into some other data format check my answer. What format do you want it in because you could just iterate over the dictionary and make the traces that way. – Shavk with a Hoon Jun 30 '22 at 14:16
-
Does this answer your question? [Iterating over dictionaries using 'for' loops](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops) Once you know how to get each individual item in the dictionary, it is trivial to use that item as the y-value for your plot. Looping over the contents of a dictionary is usually covered in most decent python tutorials, and your question suggests that you might find it useful to look through such a tutorial. – Pranav Hosangadi Jun 30 '22 at 14:58
-
presumably the dict contains numbers in the list values and not strings: `d = {'1':[5,6,7], '2':[1,2,3], '3':[9,8,7]}`. can you confirm this ? – D.L Jun 30 '22 at 15:10
1 Answers
-1
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'}
]

Shavk with a Hoon
- 568
- 4
- 21
-
So if there is an unknown/variable amount of keys and values, I can just use another 'counter' like item in place for the 0-2? – birbarchimedes Jun 30 '22 at 14:46
-