0

I have a dic of list type. like this :

{'key': '3d animator',
  'Results': [{'freelance artist': 1}, {'3d artist': 2}]},
 {'key': '3d artist',
  'Results': [{'sous chef': 1},
   {'3d animator': 2},
   {'art director': 1},
   {'artist': 1}]},
 {'key': '3d designer',
  'Results': [{'network administrator': 1}, {'None': 1}]}

can I plot a graph for each key, and show by hist the frequency of the values ('Results) ? for example for key= 3d animator, a hist chart where in the horizontal axis I have 'freelance artist' and 3d artist and in the vertical axis their frequency 1 and 2?

1 Answers1

0

First suggestion is to reformat your data:

my_list = [{'key': '3d animator',
  'Results': [{'freelance artist': 1}, {'3d artist': 2}]},
 {'key': '3d artist',
  'Results': [{'sous chef': 1},
   {'3d animator': 2},
   {'art director': 1},
   {'artist': 1}]},
 {'key': '3d designer',
  'Results': [{'network administrator': 1}, {'None': 1}]}]

Assuming each key in subdict lists is unique:

final_dict = {elem["key"]:{k: v for d in elem["Results"] for k, v in d.items()} for elem in my_list}

Then plot each value (dict) of the outer dict (dataset):

import matplotlib.pyplot as plt
for k,v in final_dict.items():
    plt.figure()
    plt.title(k)
    plt.bar(list(v.keys()), v.values())
plt.show()

This plots a graph for each key of the dataset (if that's your need).

plots

frab
  • 1,162
  • 1
  • 4
  • 14
  • 1
    double list comprehension (final_dict): for each element of my list take key and as value: merge of each entry of the lists "element["Results"]" – frab Jan 24 '21 at 08:26
  • could you please tell me where in the code you put titles? (`keys`) –  Jan 24 '21 at 08:31
  • 1
    plt.title(k) in the for loop – frab Jan 24 '21 at 08:32
  • is there anyway to sort each graph with respect of the frequency? –  Jan 24 '21 at 08:35
  • Easiest way is to sort data before plotting, check https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value – frab Jan 24 '21 at 08:42