python - 如何使用 matplotlib 索引 n 组 4 列以绘制多个图?

标签 python numpy matplotlib scipy

我想知道我应该如何在 python 中以编程方式索引/访问某些数据。 我有柱状数据:一组钻孔的深度、温度、梯度、 Gamma 。有n个钻孔。我有一个标题,其中列出了钻孔名称和数字 ID。示例:

Bore_name,Bore_ID,,,Bore_name,Bore_ID,,,, ... 
<a row of headers>
depth,temp,gradient,gamma,depth,temp,gradient,gamma ...

除了粗鲁的迭代,我不知道如何索引数据:

with open(filename,'rU') as f:
    bores = f.readline().rstrip().split(',')   
    headers = f.readline().rstrip().split(',')


# load from CSV file, missing values are empty 'cells'
tdata = numpy.genfromtxt(filename, skip_header=2, delimiter=',', missing_values='', filling_values=numpy.nan)

for column in range(0,numpy.shape(tdata)[1],4):  
    # plots temperature on x, depth on y
    pl.plot(tdata[:,column+1],tdata[:,column], label=bores[column])
    # get index at max depth
    depth = numpy.nanargmin(tdata[:,column])
    # plot text label at max depth (y) and temp at that depth (x)
    pl.text(tdata[depth,column+1],tdata[depth,column],bores[column])

这种方式似乎很简单,但我最近一直在使用 R,并且已经有点习惯了他们通过从标题解释的类和子类来引用数据对象的方式。

最佳答案

好吧,如果您喜欢 R 的 data.table,已经有一些(至少)尝试在 NumPy 中重新创建该功能——通过 NumPy Core 中的其他类和通过外部 Python 库。我发现最有希望的努力是 datarray费尔南多佩雷斯图书馆。这是它的工作原理。

>>> # create a NumPy array for use as our data set
>>> import numpy as NP
>>> D = NP.random.randint(0, 10, 40).reshape(8, 5)

>>> # create some generic row and column names to pass to the constructor
>>> row_ids = [ "row{0}".format(c) for c in range(D1.shape[0]) ]
>>> rows = 'rows_id', row_ids

>>> variables = [ "col{0}".format(c) for c in range(D1.shape[1]) ]
>>> cols = 'variable', variables

实例化 DataArray 实例,方法是调用构造函数并传入一个普通的 NumPy 数组和一个元组列表——每个轴一个元组,并且由于 ndim = 2 这里,列表中有两个元组,每个元组由轴标签 (str) 和该轴的标签序列 (list) 组成。

>>> from datarray.datarray import DataArray as DA
>>> D1 = DA(D, [rows, cols])

>>> D1.axes
      (Axis(name='rows', index=0, labels=['row0', 'row1', 'row2', 'row3', 
           'row4', 'row5', 'row6', 'row7']), Axis(name='cols', index=1, 
           labels=['col0', 'col1', 'col2', 'col3', 'col4']))

>>> # now you can use R-like syntax to reference a NumPy data array by column:
>>> D1[:,'col1']
      DataArray([8, 5, 0, 7, 8, 9, 9, 4])
      ('rows',)

关于python - 如何使用 matplotlib 索引 n 组 4 列以绘制多个图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8470539/

相关文章:

python - 加载文本文件 python 无法将字符串转换为 float

python - Sklearn MLPRegressor 中的 RandomGridSearchCV 错误

python - Matplotlib:可以绘制从一组轴到另一组轴的线吗?

python - 如何使用 tvl1 opencv 函数计算光流

python - 删除 jinja 中的重复列表

python - 如何使用 python selenium 将文本添加到页面?

python - Groupby,转置每个组并乘以 pandas 数据框中的原始组

python - 在 Python 中具有共享轴的 GridSpec

python - 使用 matplotlib 的动态图在一段时间后变慢

python - 需要提高速度性能。使用嵌套 for 循环