如何使用 matplotlib 索引 n 组 4 列来绘制多个图?
我想知道如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您喜欢 R 的 data.table,那么已经有一些(至少)尝试在 NumPy 中重新创建该功能——通过 NumPy Core 中的附加类和外部 Python 库。我发现最有希望的工作是 dataarray 库费尔南多·佩雷斯。这是它的工作原理。
通过调用构造函数并传入一个普通的 NumPy 数组和一个元组列表(每个轴一个元组)来实例化 DataArray 实例,并且由于这里 ndim = 2,列表中有两个元组,每个元组由轴标签(str)和该轴的标签序列(列表)组成。
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.
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).
您可以将数据放入每个钻孔的
dict
中,以钻孔 ID 为键,将值作为字典,以标题作为键。大致如下:使用这些方法从文件中读取可能会有点麻烦,但是为了绘图,你可以这样做
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:Probably reading from files will be a little bit more cumbersome with these approach, but for plotting you could do something like
以下是命名行和列的习惯用法:
另请参阅 namedtuple:
dataarray
(感谢 Doug)当然更通用。Here are idioms for naming rows and columns:
See also namedtuple:
datarray
(thanks Doug) is certainly more general.