pandas line plot dictionary 1

pandas line plot dictionary

### credit to StackOverflow user in source link

import matplotlib.pylab as plt

lists = sorted(d.items()) # sorted by key, return a list of tuples

x, y = zip(*lists) # unpack a list of pairs into two tuples

plt.plot(x, y)

Here is what the above code is Doing:
1. sorted(d.items()) returns a list of tuples sorted by key.
2. zip(*lists) with *lists is the same as zip((1, 4, 5), (2, 6, 7), (3, 8, 9))
3. zip(*lists) returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
4. x, y = zip(*lists) is the same as x, y = (1, 4, 5), (2, 6, 7), (3, 8, 9)
5. x, y = zip(*lists) returns two tuples, where x contains the first elements of each tuple, and y contains the second elements of each tuple.
6. plt.plot(x, y) plots x against y.

Similar Posts