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

发布于 2024-12-20 15:01:04 字数 1032 浏览 0 评论 0原文

我想知道如何在 python 中以编程方式索引/访问一些数据。 我有一组钻孔的柱状数据:深度、温度、梯度、伽马值。有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,并且已经有点习惯了他们通过类和子类引用数据对象的方式从标题解释。

I want to know how I should index / access some data programmatically in python.
I have columnar data: depth, temperature, gradient, gamma, for a set of boreholes. There are n boreholes. I have a header, which lists the borehole name and numeric ID. Example:

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

I don't know how to index the data, apart from rude iteration:

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

It seems easy enough this way, but I've been using R recently and have got a bit used to their way of referencing data objects via classes and subclasses interpreted from headers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一江春梦 2024-12-27 15:01:04

如果您喜欢 R 的 data.table,那么已经有一些(至少)尝试在 NumPy 中重新创建该功能——通过 NumPy Core 中的附加类和外部 Python 库。我发现最有希望的工作是 dataarray 库费尔南多·佩雷斯。这是它的工作原理。

>>> # 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

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

>>> 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',)

Well if you like R's data.table, there have been a few (at least) attempts to re-create that functionality in NumPy--through additional classes in NumPy Core and through external Python libraries. The effort i find most promising is the datarray library by Fernando Perez. Here's how it works.

>>> # 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

Instantiate the DataArray instance, by calling the constructor and passing in an ordinary NumPy array and a list of tuples--one tuple for each axis, and since ndim = 2 here, there are two tuples in the list each tuple is comprised of axis label (str) and a sequence of labels for that axes (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',)
苄①跕圉湢 2024-12-27 15:01:04

您可以将数据放入每个钻孔的 dict 中,以钻孔 ID 为键,将值作为字典,以标题作为键。大致如下:

data = {boreid1:{"temp":temparray, ...}, boreid2:{"temp":temparray}}

使用这些方法从文件中读取可能会有点麻烦,但是为了绘图,你可以这样做

pl.plot(data[boreid]["temperature"], data[boreid]["depth"])

You could put your data into a dict for each borehole, keyed by the borehole id, and values as dicts with headers as keys. Roughly like this:

data = {boreid1:{"temp":temparray, ...}, boreid2:{"temp":temparray}}

Probably reading from files will be a little bit more cumbersome with these approach, but for plotting you could do something like

pl.plot(data[boreid]["temperature"], data[boreid]["depth"])
り繁华旳梦境 2024-12-27 15:01:04

以下是命名行和列的习惯用法:

row0, row1 = np.ones((2,5))

for col in range(0, tdata.shape[1], 4):
   depth,temp,gradient,gamma = tdata[:, col:col+4] .T
   pl.plot( temp, depth )

另请参阅 namedtuple

from collections import namedtuple
Rec = namedtuple( "Rec", "depth temp gradient gamma" )
r = Rec( *tdata[:, col:col+4].T )
print r.temp, r.depth

dataarray(感谢 Doug)当然更通用。

Here are idioms for naming rows and columns:

row0, row1 = np.ones((2,5))

for col in range(0, tdata.shape[1], 4):
   depth,temp,gradient,gamma = tdata[:, col:col+4] .T
   pl.plot( temp, depth )

See also namedtuple:

from collections import namedtuple
Rec = namedtuple( "Rec", "depth temp gradient gamma" )
r = Rec( *tdata[:, col:col+4].T )
print r.temp, r.depth

datarray (thanks Doug) is certainly more general.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文