4

In python 2.7, I have a dictionary whose size can vary. For each key, Q1,Q2,Q3.... I need to plot the dictionary values, which are lists containing dates and integers.

{'Q1': [['20.03.2016', 25], ['21.03.2016', 35], ['22.03.2016', 22], ['23.03.2016
', 50], ['24.03.2016', 50], ['30.03.2016', 50], ['30.03.2016', 50]], 'Q3': [['20
.03.2016', 23], ['21.03.2016', 21], ['22.03.2016', 27], ['23.03.2016', 20], ['24
.03.2016', 20], ['30.03.2016', 20], ['30.03.2016', 20]], 'Q2': [['20.03.2016', 1
5], ['21.03.2016', 12], ['22.03.2016', 15], ['23.03.2016', 15], ['24.03.2016', 1
5], ['30.03.2016', 15], ['30.03.2016', 15]], 'Q5': [['20.03.2016', 320], ['21.03
.2016', 30], ['22.03.2016', 30], ['23.03.2016', 30], ['24.03.2016', 30], ['30.03
.2016', 30], ['30.03.2016', 30]], 'Q4': [['20.03.2016', 130], ['21.03.2016', 10]
, ['22.03.2016', 10], ['23.03.2016', 10], ['24.03.2016', 10], ['30.03.2016', 10]
, ['30.03.2016', 10]], 'Q7': [['23.03.2016', 5], ['24.03.2016', 5], ['30.03.2016
', 5], ['30.03.2016', 5]], 'Q9': [['30.03.2016', 17]], 'Q8': [['24.03.2016', 15]
, ['30.03.2016', 15], ['30.03.2016', 15]]}

How can I draw all the keys in one plot window containing lines for each key and labelled as per key names itself? Each line should be color coded and show all the integer vs date data points for each key. So Q1 line will plot second colomn(integers) vs first colomn (dates)

['20.03.2016', 25], 
['21.03.2016', 35],
['22.03.2016', 22],
['23.03.2016', 50],
['24.03.2016', 50], 
['30.03.2016', 50],
['30.03.2016', 50]

Each line can be color coded and should be have label of the key in the legend. I am using numpy and matplotlib libraries in python.

5
  • could you rephrase your question a bit? I was thinking, will it work for you if you create a date by number plot where each key is represented by a color? Commented Mar 30, 2016 at 15:46
  • Using which plotting library? Matplotlib? Commented Mar 30, 2016 at 16:03
  • @John, Yes, you got it right. Each key should be color coded. So for each key, it should plot list[1] vs list[0] (the two colomns in the list) Commented Mar 30, 2016 at 16:08
  • @Hannes Yes, Matplotlib Commented Mar 30, 2016 at 16:08
  • Mirror question: Plot data stored in list of dicts using list comprehension Commented Aug 19, 2020 at 12:38

1 Answer 1

1

We can simply iterate over the dictionary items, and then use zip() to unpack the values. The label= keyword argument to plot() will make the legend() call create the legend automatically.

for key, data_list in data_dict.items():
    dates_str, values = zip(*data_list)  # Unpack
    dates = convert_str_to_dates(dates_str)  # Not handled in example
    plt.plot(dates, values, label=key)
plt.legend()

To be solved is how to go from a list of strings to some sort of date object that matplotlib understands. This should be fairly easy to find in the documentation though.

Edit: The trick with using zip() to unpack a list-of-lists was one of those "Aha!"-moments for me :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Thats perfect! I used dates = [dt.datetime.strptime(d,'%d.%m.%Y').date() for d in dates_str] for handling dates in strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.