绘制multiindex pandas数据帧时的工件

时间:2014-11-17 02:42:18

标签: python pandas plot

我将数据组织成一个多索引数据帧。例如:

    Sweep  Time   Primary     Secondary  x720nm    x473nm      PMTShutter                                                      
Sweep0001 0.00000 -87.429810  -4.882812  0.000610  0.000305    0.000000
          0.00005 -87.445068  -4.882812  0.000610  0.001221    0.000000
          0.00010 -87.451172  -4.272460  0.000000  0.000916    0.000000
            ...        ...       ...       ...         ...  
Sweep0039 0.68655 -87.261963  -4.272461  0.000305  0.000916    0.000305
          0.68660 -87.258911  -4.272461  0.000305  0.000916    0.000305
          0.68665 -87.252808  -5.493164  0.000000  0.000916    0.000305
          0.68670 -87.261963  -4.272461  0.000305  0.000916    0.000305

绘制任何单次扫描工作正常,但是当我去绘制多次扫描时,我有这些基本上是直线的伪影(见下文)。

这不是matplotlib特有的,因为pyqtgraph也会发生这种情况。

绘制单个迹线时不存在问题:

plt.plot(data.Time['Sweep0001'], data.Primary['Sweep0001'])

enter image description here

一旦绘制多条痕迹就会出现问题:

plt.plot(data.Time['Sweep0001':'Sweep0002'], data.Primary['Sweep0001':'Sweep0002'])

enter image description here

plt.plot(data.Time['Sweep00-1':'Sweep0010'], data.Primary['Sweep0001':'Sweep0010'])

enter image description here

1 个答案:

答案 0 :(得分:1)

data.Time['Sweep001':'Sweep0002']data.Time['Sweep001']data.Time['Sweep002']连接起来。因此,时间值再次从0变为N,然后从0变为N. 因此,plt.plot会从t=N绘制一条回到t=0的行,从而导致该工件。

而是为每一行使用一个plt.plot调用:

for i in range(1, 11):
    col = 'Sweep{:04d}'.format(i)
    plt.plot(data.Time[col], data.Primary[col])