Issue
Hi I'm using matplotlib and I'm trying to create a mean plot. I have those arrays -
A_x = [1,3,4]
A_y = [1,2,3]
B_x = [2,3,5]
B_y = [2,3,4]
And I would like to create this array (mean array)
AB_x = [1,2,3,4,5]
AB_y = [1,2,(3+2)/2,3,4]
I Am using pandas \ numpy \ matplotlib
Solution
You could use dictionaries do this. Convert your values of A to a dictionary first, the update it accordingly. If your dict already contains the key, update it by the mean formula, else add an entry. The rest is just converting it to your old format to get separate lists for x and y, but you can probably skip this depending on the matplotlib functions you are using.
The code:
A_x = [1,3,4]
A_y = [1,2,3]
B_x = [2,3,5]
B_y = [2,3,4]
AB = {x: y for x, y in zip(A_x, A_y)}
for key, value in zip(B_x, B_y):
if key in AB:
AB[key] = (AB[key] + value) / 2
else:
AB[key] = value
AB_list = [(x, y) for x, y in AB.items()]
AB_list.sort(key=lambda entry: entry[0])
AB_x, AB_y = list(zip(*AB_list))
AB_x = list(AB_x)
AB_y = list(AB_y)
Note that this only works if you know that you have exactly two sources (A and B in your case) because else, the mean is going to be wrong.
Answered By - Brimbo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.