2

我想用 pyplot 做以下事情,但找不到办法,也许不可能:

给定一组字符串 S 和自然数 N。给定一组项目 I,它们附加一组 (字符串 € S,number € N) 对 p。给定一个坐标系,S 在 y 轴上,N 在 x 轴上。

现在我想在这个坐标系中映射每一对 p ,以便特定项目的所有相应点都通过线连接。

然后对于每个项目,我在这个坐标系中都有一个图表。

一个问题是,如果有一个字符串 s € S 不包含在附加到某个项目的对中,那么我有某种 NULL 值。这不应被解释为 (s,0) 而只是被忽略...

比如说,我有 (1) 使用的所有字符串的列表 L1,(2) 一个项目 I1 和 (3) 一个项目 I2:

L1 = [London, Paris, Tokio, Berlin, Barcelona, Reykjavik]
I1 = [[London,0],[Paris,10],[Barcelona,23]]
I2 = [Paris,10],[Tokio,19],[Berlin,21],[Reykjavik,23]]

现在我希望将 I1 和 I2 映射到在 y 轴上具有 L1 并且在 x 轴上具有自然数的坐标系。每个项目的“点”应该结合起来。

我希望我的意思很清楚。

4

1 回答 1

1

这是你想要的吗? 在此处输入图像描述

from pylab import *

L1 = ['London', 'Paris', 'Tokio', 'Berlin', 'Barcelona', 'Reykjavik']
pos = arange(len(L1))
yticks(pos, L1)

I1 = [['London',0],['Paris',10],['Barcelona',23]]
I2 = [['Paris',10],['Tokio',19],['Berlin',21],['Reykjavik',23]]

I1_y = [L1.index(el[0]) for el in I1]
I1_x = [el[1] for el in I1]

I2_y = [L1.index(el[0]) for el in I2]
I2_x = [el[1] for el in I2]

plot(I1_x, I1_y, 'o-')
plot(I2_x, I2_y, 'x-')
grid(True)
savefig('countries.png')
show()
于 2012-05-03T08:35:01.597 回答