简体   繁体   English

多图合二为一 plot

[英]Multiple plots into one plot

Hi I'm using matplotlib and I'm trying to create a mean plot.嗨,我正在使用 matplotlib,我正在尝试创建一个平均 plot。 I have those arrays -我有那些 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我正在使用 pandas \ numpy \ matplotlib

You could use dictionaries do this.你可以使用字典来做到这一点。 Convert your values of A to a dictionary first, the update it accordingly.首先将 A 的值转换为字典,然后相应地更新它。 If your dict already contains the key, update it by the mean formula, else add an entry.如果您的 dict 已经包含密钥,请通过均值公式对其进行更新,否则添加一个条目。 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. rest 只是将其转换为旧格式以获取 x 和 y 的单独列表,但您可以根据您使用的 matplotlib 函数跳过此操作。

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.请注意,这仅在您知道您有两个来源(在您的情况下为 A 和 B)时才有效,因为否则,平均值将是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM