对数据进行排序以便在 mplot3d 中绘图

发布于 2024-10-03 09:38:20 字数 765 浏览 1 评论 0原文

我一直在使用 mplot3d (matplotlib 的一部分)进行一些各种 3d 绘图,并且它的工作表现令人钦佩。然而,我遇到了一个新问题。

Mplot3d 期望数据以某种方式排序,以绘制线框。例如,它喜欢这样的东西:

x = array([[1, 2, 3],
          [1, 2, 3],
          [1, 2, 3]])

y = array([[1, 1, 1],
          [2, 2, 2],
          [3, 3, 3])

其中 z 是相同维度的数组,数据对应于空间中的每个位置。

不幸的是,我的数据不是这样格式化的 - 每隔一行都是相反的,因为数据是通过以光栅模式扫描来收集的。

所以我有更多类似的东西:

x = array([[1, 2, 3],
          [3, 2, 1],
          [1, 2, 3]])

我目前的方法是一种非常丑陋的、暴力的“做一个for循环,然后检查你是否在奇数行”,从旧的数组中构建一个新的数组,但我是希望有一种更优雅的方式来做到这一点。棘手的部分是我必须以与 X 和 Y 相同的方式重新排列 Z 数组,以确保保留与每个点对应的数据空间。

理想情况下,我想要一些健壮且专门设计用于对包含任意随机位置点的一组二维数组进行排序的东西,但即使是一种更Pythonic的方式来完成我已经在做的事情也将受到赞赏。如果我能让它变得更加强大,并且不依赖于这种特定的光栅扫描模式,从长远来看,它可能会帮我解决一些麻烦。

I have been using mplot3d (part of matplotlib) for some various 3d plotting, and it has been performing the job admirably. However, I have run into a new problem.

Mplot3d expects data to be sorted in a certain fashion, to plot a wireframe. For example, it likes something like this:

x = array([[1, 2, 3],
          [1, 2, 3],
          [1, 2, 3]])

y = array([[1, 1, 1],
          [2, 2, 2],
          [3, 3, 3])

where z is then an array of the same dimensions, with data corresponding to each of those positions in space.

Unfortunately, my data isn't formatted like this - every other row is reversed, because the data is collected by scanning in a raster pattern.

So I have something more like:

x = array([[1, 2, 3],
          [3, 2, 1],
          [1, 2, 3]])

My current approach is a very ugly, brute-force "do a for loop then check if you're in an odd row or not" that builds a new array out of the old one, but I am hoping there is a more elegant way of doing this. The tricky part is that I have to re-arrange the Z array in the same way I do the X and Y, to ensure that the data corresponding with each point is space is preserved.

Ideally, I'd like something that's robust and specifically designed to sort a set of 2-d arrays that contain arbitrary random position points, but even a more pythonic way of doing what I'm already doing would be appreciated. If I could make it more robust, and not dependent on this specific raster scanning pattern, it would probably save me headaches in the long term.

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

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

发布评论

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

评论(1

尽揽少女心 2024-10-10 09:38:20

如果我理解正确的话,您只想这样做:x[1::2, :] = x[1::2, ::-1]

有一些问题......如果您不制作 x 的中间副本,由于 numpy 中广播的工作方式,它不会完全达到您的预期。

尽管如此,使用基本索引仍然非常简单:

import numpy as np
x = np.array([[1,2,3],[3,2,1],[1,2,3],[3,2,1],[1,2,3]])
x_rev = x.copy()
x_rev[1::2, :] = x[1::2, ::-1]

这会将 this (x):

array([[1, 2, 3],
       [3, 2, 1],
       [1, 2, 3],
       [3, 2, 1],
       [1, 2, 3]])

转换为 this (x_rev):

array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

如果您不熟悉 python 中的切片, x[1::2] 将从第二个项目开始选择 x 的所有其他项目。 (1 是起始索引,2 是增量)相反,x[::-1] 仅指定增量 < code>-1,从而反转数组。在本例中,我们仅将这些切片应用到特定轴,因此我们可以从第二行开始选择并反转每隔一行。

If I understand you correctly, you just want to do this: x[1::2, :] = x[1::2, ::-1].

There are a few kinks... If you don't make an intermediate copy of x it doesn't quite do what you'd expect due to the way broadcasting works in numpy.

Nonetheless, it's still pretty simple to do with basic indexing:

import numpy as np
x = np.array([[1,2,3],[3,2,1],[1,2,3],[3,2,1],[1,2,3]])
x_rev = x.copy()
x_rev[1::2, :] = x[1::2, ::-1]

This converts this (x):

array([[1, 2, 3],
       [3, 2, 1],
       [1, 2, 3],
       [3, 2, 1],
       [1, 2, 3]])

Into this (x_rev):

array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

In case you're not familiar with slicing in python, x[1::2] would select every other item of x, starting with the second item. (1 is the the start index, 2 is the increment) In contrast, x[::-1] just specifies an increment of -1, thus reversing the array. In this case we're only applying these slices to a particular axis, so we can select and reverse every other row, starting with the second row.

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