string - 在 matplotlib 中绘制字符串列表

标签 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

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

这是我所拥有的

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 循环。

谢谢

最佳答案

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

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()

关于string - 在 matplotlib 中绘制字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20228174/

相关文章:

python - 无法连接 'str' 和 'list' 对象列表包含整数

python - 如何解决生成饼图时出现Python错误: ValueError: 'explode' must be of length 'x'

python - 使用 matplotlib 的plot() 中的 'dashes' 参数绘制连续线

python - 在现有图表之上绘图 Python Matplotlib

java - JSONObject 删除空值对

ios - Objective-C 中的正则表达式

string - 如何在 Haskell 中编写纯字符串到字符串函数 FFI 到 C++

c - 在字符串中搜索字符串

python - 从 python 中的字典列表中写出一个 csv 文件

python - .clear() 对于列表不起作用 - python