So I have a dictionary (dictionary2) where every value is a list. I need to make a function that shows this data on a plot, with the keys on the x-axis, which I managed. I want to create a separate list (count_list) that contains counts for how often each number repeats itself within the list belonging to a given key (only 1 separate list for all of the values of the dictionary together). The end goal is to create a scatterplot where overlapping markers are bigger, which I could do by ascribing this separate list to the 's' argument in the scatterplot call. (Note that different color or something would work fine too but that would still require the count_list anyway)
I have 1 working version that does not account for overlap (this might give some context, see below).
I'm currently trying to make the count_list with the following code (I took it outside of the function for experimenting convenience):
count_list=[]
for key in dictionary2:
for value in dictionary2:
for M in value:
count= value.count(M)
count_list.append(count)
This returns a count_list with the same sequence of numbers being repeated for each key. I realize my code is probably way too simplistic, so I am not surprised it didn't work. However, I'm not sure where to go from here and don't understand why the output looks like that.
The current plot looks like this:
def plot_dict(dataset):
#transforming data to be read correctly into the plot
d = dataset
x= []
y= []
for k, v in d.items():
x.extend(list(itertools.repeat(k, len(v))))
y.extend(v)
plt.figure(figsize=(30,30))
plt.plot(x,y, '.')
plt.title('FASTA plot', fontsize=45)
plt.xlabel('Sequence IDs', fontsize=30)
plt.ylabel('Molecular weight', fontsize=30)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plot_dict(dictionary2)
(I'm using jupyterlab 3.0.14.)
This is my first time posting a question in stack overflow, so if I broke any kind of etiquette or if anything in my explanation of the problem was unclear, please let me know!
CodePudding user response:
I'm not sure if I understood what you need correctly, but is it something like this?
from typing import Dict
dictionary = {
"key1": [1, 2, 3, 4, 4, 1],
"key2": [1, 2, 2, 2, 1, 5],
"key3": [100, 3, 100, 9],
}
occurrences_dict: Dict[str, Dict[int, int]] = {key: {} for key in dictionary}
for key, numbers_list in dictionary.items():
for number in numbers_list:
occurrences = numbers_list.count(number)
occurrences_dict[key].update({number: occurrences})
print(occurrences_dict)
The output is the following:
{
"key1": {1: 2, 2: 1, 3: 1, 4: 2},
"key2": {1: 2, 2: 3, 5: 1},
"key3": {100: 2, 3: 1, 9: 1},
}
You get a dictionary with the same keys as the original and within each key, you have a dictionary with the number of occurrences of each number on the corresponding list