0

I have got a bidimensionnal array containing values that I would like to plot on the Y axis, and a bidimensionnal array, of datetime values that I would like to plot in the X axis, and I would like to get two lines of values against date from those arrays. But these structures used as-is don't work as expected, as I have got a different kind of graph than a line graph.

Values for the x axis (dates):

    [[datetime.datetime(2011, 1, 1, 0, 0, 25, 135000), datetime.datetime(2011, 2, 1, 0, 0, 57, 386000), 
datetime.datetime(2011, 3, 1, 0, 0, 59, 579000), datetime.datetime(2011, 4, 1, 0, 0, 27, 676000), 
datetime.datetime(2011, 5, 1, 0, 0, 25, 135000), datetime.datetime(2011, 6, 1, 0, 0, 26, 414000), 
datetime.datetime(2011, 7, 1, 0, 0, 28, 145000), datetime.datetime(2011, 8, 1, 0, 0, 26, 432000), 
datetime.datetime(2011, 9, 1, 0, 0, 27, 301000), datetime.datetime(2011, 10, 1, 0, 0, 27, 643000), 
datetime.datetime(2011, 11, 1, 0, 0, 27, 673000), datetime.datetime(2011, 12, 1, 0, 0, 28, 294000)], 
[datetime.datetime(2011, 1, 1, 0, 0, 25, 135000), datetime.datetime(2011, 2, 1, 0, 0, 57, 386000), 
datetime.datetime(2011, 3, 1, 0, 0, 59, 579000), datetime.datetime(2011, 4, 1, 0, 0, 27, 676000), 
datetime.datetime(2011, 5, 1, 0, 0, 25, 135000), datetime.datetime(2011, 6, 1, 0, 0, 26, 414000), 
datetime.datetime(2011, 7, 1, 0, 0, 28, 145000), datetime.datetime(2011, 8, 1, 0, 0, 26, 432000), 
datetime.datetime(2011, 9, 1, 0, 0, 27, 301000), datetime.datetime(2011, 10, 1, 0, 0, 27, 643000), 
datetime.datetime(2011, 11, 1, 0, 0, 27, 673000), datetime.datetime(2011, 12, 1, 0, 0, 28, 294000)]]

Values for the y axis (min, max):

    [[-8.0, 19.0, 11.0, 6.0, 6.0, 6.0, 6.0, 6.0, 2.0, 2.0, 2.0, 2.0], 
[-12.0, -7.0, -6.0, -6.0, -6.0, -6.0, -6.0, -6.0, -6.0, -6.0, -7.0, -7.0]]

I would like some help on how to transform these structures so that two lines of min and max over date are displayed.

4

1 回答 1

1

x如果您有两个称为and的“二维”数组,y它们的长度相同,请尝试

import numpy as np
import matplotlib.pyplot as plt

# populate x and y arrays.

x = np.array(x)
y = np.array(y)

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(x[:,0], y[:,0], 'g-o')
ax.plot(x[:,1], y[:,1], 'r-x')

一般来说,如果你的数组只有一维,你可以简单地做

ax.plot(x, y)

如果您的数组有两组以上的值,您可以这样做

for i in range(x.shape[-1]):
    ax.plot(x[:,i], y[:,0])

该符号x[:,n]有两个部分;:从每一行获取数据的方法和从每一行获取,nn+10代表第一个)元素的方法。这是有效的,因为您的数据的结构类似于

[[date00, date01],
 [date10, date11],
 [date20, date21],
 ...
 [dateN0, dateN1]]

所以x[:,0]取第一列并x[:,1]取第二列。

如果您有一个包含许多维度的大型数组,您可以像x[dim0, dim1, ..., dimN]. 例如,假设您有一个医疗数据的 3 维数组,其中维度表示患者 ID、样本 ID、病毒载量,您可以通过 do 从第一个患者的所有样本中获取最后一个病毒载量数data[0, :, -1],或者从所有样本中获取所有病毒载量数从那个病人做起data[0, :, :]

于 2013-09-24T16:48:59.447 回答