在matplotlib中绘制字符串列表

时间:2013-11-26 21:18:26

标签: string list matplotlib

我有一个大文件(称为Data),它是一个字符串列表,它的175693行,但我只想使用第8到151799行。该文件的缩写版本如下:

Name                 Group          Measured         Modelled         Residual         Weight
 pdwl1                pdwls          2083.620         2089.673        -6.052805        9.4067000E-04
 pdwl2                pdwls          2186.748         2199.771        -13.02284        8.9630800E-04
 pdwl3                pdwls          2150.983         2160.259        -9.275730        9.1121100E-04
 pdwl4                pdwls          2133.283         2142.970        -9.686504        9.1877100E-04
 pdwl5                pdwls          2241.741         1769.331         472.4097        8.7432100E-04
 pst_1                devwls         2191.200         2094.658         96.54200         1.000000    
 pst_2                devwls         2194.160         2094.070         100.0900         1.000000    
 pst_3                devwls         2190.790         2093.375         97.41500         1.000000    
 pst_4                devwls         2191.700         2092.671         99.02900         1.000000    
 pst_5                devwls         2188.260         2092.739         95.52100         1.000000   
 devfl1               devflux       1.2788475E+07    1.2199410E+07     589064.6        1.4030900E-06
 devfl2               devflux       1.2208086E+07    1.2044727E+07     163359.4        1.4030900E-06
 devfl3               devflux       1.3559062E+07    1.1423958E+07     2135104.        1.4030900E-06
 devfl4               devflux       1.2419465E+07    1.1141419E+07     1278046.        1.4030900E-06
 devfl5               devflux       1.2070242E+07    1.0925833E+07     1144409.        1.4030900E-06

我需要绘制测量值与建模值之间的关系,我想要一个用于测量v的图,用于Group == pdwls,另一个用于测量v残差的图= = pdwls,然后用于测量来自Group == devwls和对于devwls的测量v残差的图

这是我的

import numpy as np
import matplotlib.pyplot as plt

data = np.genfromtxt('elm3_1-4 - Copy.rei', dtype=None, names=True)
#data = np.genfromtxt('elm3_1-4-pdwls.rei', dtype=None, names=True)
#data = np.genfromtxt('elm3_1-4-devwls.rei', dtype=None, names=True)

for data[6:1643] in data:
    plt.subplot(2,2,1)
    plt.scatter(data['Measured'], data['Modelled'])
    plt.xlabel('Measured (ft)')
    plt.ylabel('Modelled (ft)')
    plt.title('ELM3_1-4 Pre-Development WLs') 
    plt.xlim(1000,4000)
    plt.ylim(-2000,4000)
    plt.scatter(data['Measured'], data['Residual'])
    plt.xlabel('Measured (ft)')
    plt.ylabel('Residual (Meas - Model) (ft)')
    plt.title('ELM3_1-4 Pre-Development: Measured WLs v Resduals') 
    plt.xlim(1000,4000)
    plt.ylim(-1000,1000)
    plt.subplot(2,2,2)
    plt.show()
for data[1644:151798] in data:
    plt.subplot(2,2,3)
    plt.scatter(data['Measured'], data['Modelled'])
    plt.xlabel('Measured (ft)')
    plt.ylabel('Modelled (ft)')
    plt.title('ELM3_1-4 Development WLs') 
    plt.xlim(1000,4000)
    plt.ylim(1000,4000)
    plt.scatter(data['Measured'], data['Residual'])
    plt.xlabel('Measured (ft)')
    plt.ylabel('Residual (Meas - Model) (ft)')
    plt.title('ELM3_1-4 Development: Measured WLs v Resduals') 
    plt.xlim(1000,4000)
    plt.ylim(-1000,1000)
    plt.subplot(2,2,4)
    plt.show() 

代码运行但不生成图表。我进入命令窗口的所有内容都是:

Line #175688 (got 6 columns instead of 9).

消息中涉及多行,而不仅仅是175688。 我编辑了这个问题,为新的示例数据集输入了for循环。

由于

2 个答案:

答案 0 :(得分:2)

如果您的数据文件实际上是这样,那么您可以使用:

data = np.genfromtxt('elm3_1-4 - Copy.rei', dtype=None, names=True)

dtype=None表示它会自动确定每列的最佳类型,而names=True表示它会创建一个结构化数组,其中包含文件第一行名称的字段。你的看起来像这样:

array([('pdwl1', 'pdwls', 2083.62, 2089.673, -6.052805, 0.00094067),
       ('pdwl2', 'pdwls', 2186.748, 2199.771, -13.02284, 0.000896308),
       ('pdwl3', 'pdwls', 2150.983, 2160.259, -9.27573, 0.000911211),
       ('pdwl4', 'pdwls', 2133.283, 2142.97, -9.686504, 0.000918771)], 
      dtype=[('Name', 'S5'), ('Group', 'S5'), ('Measured', '<f8'), ('Modelled', '<f8'), ('Residual', '<f8'), ('Weight', '<f8')])

要绘制,例如'Measured''Modelled',请使用:

plt.plot(data['Modelled'], data['Measured'])

MvM

要明确的是,您上面发布的所有内容都可以简化为:

import numpy as np
import matplotlib.pyplot as plt

data = np.genfromtxt('elm3_1-4 - Copy.rei', dtype=None, names=True)

plt.plot(data['Modelled'], data['Measured'])
plt.ylabel('Measured')
plt.xlabel('Modelled')
plt.title('Title')                 
plt.show()

答案 1 :(得分:0)

此代码获取我想要的内容

import numpy as np
import matplotlib.pyplot as plt

data = np.genfromtxt('elm3_1-4 - Copy.rei', dtype=None, names=True, skip_header=6)

font = {'size'   : 10,}

#-----PreDevelopment plots__________________________________
plt.rc('axes', color_cycle=['r'])
plt.subplot(2,3,1)
plt.scatter(data[7:1643]['Measured'], data[7:1643]['Modelled'])
plt.plot([0,4000],[0,4000])
plt.xlabel('Measured (ft)', fontdict=font)
plt.ylabel('Modelled (ft)', fontdict=font)
plt.title('ELM3_1-4 Pre-Development WLs', fontdict=font) 
plt.xlim(1000,4000)
plt.ylim(-2000,4000)

plt.rc('axes', color_cycle=['r'])
plt.subplot(2,3,2)
plt.scatter(data[7:1643]['Measured'], data[7:1643]['Residual'])
plt.plot([0,4000],[4000,4000])
plt.xlabel('Measured (ft)', fontdict=font)
plt.ylabel('Residual (Meas - Model) (ft)', fontdict=font)
plt.title('ELM3_1-4 Pre-Development: \n Measured WLs v Residual', fontdict=font) 
plt.xlim(1000,4000)
plt.ylim(-1000,1000)
plt.show()

plt.rc('axes', color_cycle=['r'])
plt.subplot(2,3,3)
plt.scatter(data[151761:151798]['Measured'], data[151761:151798]['Modelled'])
plt.plot([0,4000],[0,4000])
plt.xlabel('Measured (ft)', fontdict=font)
plt.ylabel('Modelled (ft)', fontdict=font)
plt.title('ELM3_1-4 Pre-Development \n Measured BFs v Modelled BFs', fontdict=font) 
#plt.xlim(1000,4000)
#plt.ylim(-2000,4000)
plt.show()

#-----Development plots__________________________________
plt.subplot(2,3,4)
plt.scatter(data[1644:151760]['Measured'], data[1644:151760]['Modelled'])
plt.plot([0,4000],[0,4000])
plt.xlabel('Measured (ft)', fontdict=font)
plt.ylabel('Modelled (ft)', fontdict=font)
plt.title('ELM3_1-4 Development WLs', fontdict=font) 
plt.xlim(1000,4000)
plt.ylim(1000,4000)

plt.subplot(2,3,5)
plt.scatter(data[1644:151760]['Measured'], data[1644:151760]['Residual'])
plt.xlabel('Measured (ft)', fontdict=font)
plt.ylabel('Residual (Meas - Model) (ft)', fontdict=font)
plt.title('ELM3_1-4 Development: \n Measured WLs v Resduals', fontdict=font)
plt.plot([0,4000],[4000,4000])
plt.xlim(1000,4000)
plt.ylim(-1000,1000)
plt.show() 
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)

plt.subplot(2,3,6)
plt.scatter(data[151799:175132]['Measured'], data[151799:175132]['Residual'])
plt.xlabel('Measured (ft)', fontdict=font)
plt.ylabel('Residual (Meas - Model) (ft)', fontdict=font)
plt.title('ELM3_1-4 Development: \n Measured BFs v Modelled BFs', fontdict=font)
plt.plot([0,4000],[4000,4000])
#plt.xlim(1000,4000)
#plt.ylim(-1000,1000)
plt.show() 
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)