X Y1 Y2 Y3 ...格式的Matplotlib数据

时间:2017-11-17 14:11:19

标签: python matplotlib multidimensional-array

根据文件data.dat,第一列是X数据,其余列是Y1Y2,数据:

# Lines starting with #
# More lines starting with #
@ Lines starting with @
@ More lines starting with @

  0.00000E+00  0.81837E+02  0.81837E+02  0.14153E+03  0.14153E+03  0.17240E+03  0.17240E+03  0.19575E+03  0.19575E+03  0.21944E+03  0.21944E+03  0.25196E+03  0.25196E+03  0.35173E+03  0.35173E+03  0.35441E+03  0.35441E+03  0.37508E+03  0.37508E+03  0.71611E+03  0.71611E+03  0.72908E+03  0.72908E+03  0.87658E+03  0.87658E+03  0.11174E+04  0.11174E+04  0.15014E+04  0.15014E+04  0.15644E+04  0.15644E+04
  0.39856E+00  0.80923E+02  0.82842E+02  0.13984E+03  0.14327E+03  0.17059E+03  0.17417E+03  0.19258E+03  0.19907E+03  0.21823E+03  0.22072E+03  0.25141E+03  0.25250E+03  0.35083E+03  0.35230E+03  0.35441E+03  0.35467E+03  0.37481E+03  0.37543E+03  0.71596E+03  0.71626E+03  0.72906E+03  0.72913E+03  0.87640E+03  0.87676E+03  0.11173E+04  0.11176E+04  0.15011E+04  0.15016E+04  0.15631E+04  0.15657E+04

在以下代码中,

import os
import numpy as np
import matplotlib.pyplot as plt

path='./'
template = os.path.join(path, '*.dat')

fname = glob.glob(template)
fname = fname[0]

# This is just to skip lines that start with '@' and '#':
gen = (r for r in open(fname) if not r[0] in ('@', '#'))

data = np.genfromtxt(gen)
print 'data = ', data
print np.shape(data)  

就像最后一次印刷中所述,data是一个二维的形状数组:(2,31):

data = [[  0.00000000e+00   8.18370000e+01   8.18370000e+01   1.41530000e+02
    1.41530000e+02   1.72400000e+02   1.72400000e+02   1.95750000e+02
    1.95750000e+02   2.19440000e+02   2.19440000e+02   2.51960000e+02
    2.51960000e+02   3.51730000e+02   3.51730000e+02   3.54410000e+02
    3.54410000e+02   3.75080000e+02   3.75080000e+02   7.16110000e+02
    7.16110000e+02   7.29080000e+02   7.29080000e+02   8.76580000e+02
    8.76580000e+02   1.11740000e+03   1.11740000e+03   1.50140000e+03
    1.50140000e+03   1.56440000e+03   1.56440000e+03]
 [  3.98560000e-01   8.09230000e+01   8.28420000e+01   1.39840000e+02
    1.43270000e+02   1.70590000e+02   1.74170000e+02   1.92580000e+02
    1.99070000e+02   2.18230000e+02   2.20720000e+02   2.51410000e+02
    2.52500000e+02   3.50830000e+02   3.52300000e+02   3.54410000e+02
    3.54670000e+02   3.74810000e+02   3.75430000e+02   7.15960000e+02
    7.16260000e+02   7.29060000e+02   7.29130000e+02   8.76400000e+02
    8.76760000e+02   1.11730000e+03   1.11760000e+03   1.50110000e+03
    1.50160000e+03   1.56310000e+03   1.56570000e+03]]

对于X1 = 0.00000000e+00,有30个Y值。这对应于data.dat的第一行。

对于X2 = 3.98560000e-01,还有其他30个Y值。这对应于data.dat

的第二行

我想matplotlib data 2D数组,即属于Y的{​​{1}}的所有30个值以及X1 = 0.00000000e+00属于Y的所有30个值{1}}

更新

感谢@jakevdp回答,以下两个陈述:

X2 = 3.98560000e-01

打印相同的结果:

print 'data[0][1:] = ', data[0][1:] # Extracts Ys(X1)
print 'data[0, 1:] = ', data[0, 1:] # "      

以下两个陈述也会打印出相同的结果:

data[0][1:] =  [   81.837    81.837   141.53    141.53    172.4     172.4     195.75
   195.75    219.44    219.44    251.96    251.96    351.73    351.73
   354.41    354.41    375.08    375.08    716.11    716.11    729.08
   729.08    876.58    876.58   1117.4    1117.4    1501.4    1501.4
  1564.4    1564.4  ]
data[0, 1:] =  [   81.837    81.837   141.53    141.53    172.4     172.4     195.75
   195.75    219.44    219.44    251.96    251.96    351.73    351.73
   354.41    354.41    375.08    375.08    716.11    716.11    729.08
   729.08    876.58    876.58   1117.4    1117.4    1501.4    1501.4
  1564.4    1564.4  ]

现在,按照相同的逻辑,这两个语句也会打印出相同的结果:

print 'data[1][1:] = ', data[1][1:] # Extracts Ys(X2)
print 'data[1, 1:] = ', data[1, 1:] # " 

data[1][1:] =  [   80.923    82.842   139.84    143.27    170.59    174.17    192.58
   199.07    218.23    220.72    251.41    252.5     350.83    352.3
   354.41    354.67    374.81    375.43    715.96    716.26    729.06
   729.13    876.4     876.76   1117.3    1117.6    1501.1    1501.6
  1563.1    1565.7  ]
data[1, 1:] =  [   80.923    82.842   139.84    143.27    170.59    174.17    192.58
   199.07    218.23    220.72    251.41    252.5     350.83    352.3
   354.41    354.67    374.81    375.43    715.96    716.26    729.06
   729.13    876.4     876.76   1117.3    1117.6    1501.1    1501.6
  1563.1    1565.7  ]

然而,他们没有:

print 'data[:][0] = ' , data[:][0]
print 'data[:, 0] = ' , data[:, 0]

为什么会这样?

1 个答案:

答案 0 :(得分:1)

您对“所需绘图”的描述有点模糊,但如果您想在该行开头的每个Y值上绘制一个点,您可以这样做:

X = data[:, 0]
Ys = data[:, 1:]
plt.plot(X, Ys, '.', color='black')

enter image description here