matplotlib从一维数组创建二维数组 - 有没有更好的方式? [英] matplotlib creating 2D arrays from 1D arrays - is there a nicer way?

查看:133
本文介绍了matplotlib从一维数组创建二维数组 - 有没有更好的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一些可视化的三维数据我一直在使用matplotlibs等高线图,表面图和线框图。

I am trying to visualise some 3d data I have using matplotlibs contour plots, surface plots and wireframe plots.

我的原始数据是在以x,y和z各自为numpy的阵列中的其自己的列(例如)的形式:

my raw data is in the form of a numpy array with x,y and z each in their own column (e.g.):

| XS | YS | ZS |结果
| --- | --- | ---- |结果
| 1 | 1 | 3 |结果
| 2 | 1 | 4 |结果
| 3 | 1 | 2 |结果
| 4 | 1 | 3 |结果
| 5 | 1 | 5 |结果
| 1 | 2 | -1 |结果
| 2 | 2 | -1 |结果
| 3 | 2 | -2 |结果
| 4 | 2 | 2 |结果
| 5 | 2 | 7 |结果
| 1 | 3 | 5 |结果
| 2 | 3 | 2 |结果
| 3 | 3 | 3 |结果
| 4 | 3 | 2 |结果
| 5 | 3 | 3 |结果

| xs | ys | zs |
|---|---|----|
| 1 | 1 | 3 |
| 2 | 1 | 4 |
| 3 | 1 | 2 |
| 4 | 1 | 3 |
| 5 | 1 | 5 |
| 1 | 2 | -1 |
| 2 | 2 | -1 |
| 3 | 2 | -2 |
| 4 | 2 | 2 |
| 5 | 2 | 7 |
| 1 | 3 | 5 |
| 2 | 3 | 2 |
| 3 | 3 | 3 |
| 4 | 3 | 2 |
| 5 | 3 | 3 |

现在一些绘图函数只取数据,一维数组对应我的专栏(XS,YS,ZS)。然而,有些需要一个二维数组(meshgrid)格式。有没有一种简单的方法,从3维数组转换为3二维数组的格式是否正确?我一直在使用numpy.meshgrid尝试和,而这个工程创建X和Y二维数组,我不能制定出一个很好的方式来创建对应的Z个二维数组。我设法通过一个空二维数组和Z的适当的值填充它做到这一点,但是这是不是很漂亮。有没有更好的方式来创建Z轴二维数组?

Now some of the plotting functions just take the data in 1D arrays corresponding to my columns (xs,ys,zs). However, some require a 2D array (meshgrid) format. Is there an easy way to convert from the 3 1D arrays to the correct format of 3 2D arrays? I have tried using numpy.meshgrid and, whilst this works for creating the X and Y 2D arrays, I can't work out a nice way to create the corresponding Z 2D array. I have managed to do it by making a blank 2D array and filling it with the appropriate values for Z, but this isn't very nice. Is there a better way to create the Z 2D array?

下面是我尝试(工作)。有没有一种方法,使Z轴阵列,无需通过X和Y?

Below is my attempt (which works). Is there a way to make the Z array without cycling through both X and Y?

def getMeshGrid(dataArray):
    """get 2d coordinate grid and Z values in meshgrid format. requires values in
    dataArray to have a rectangular region of x-y space covered uniformly"""
    xs = dataArray[:,0]
    ys = dataArray[:,1]
    xmin,xmax = xs.min(), xs.max()
    xstep = xs[xs!=xmin].min()-xmin
    ymin,ymax = ys.min(), ys.max()
    ystep = ys[ys!=ymin].min()-ymin
    X = numpy.arange(xmin, xmax+xstep, xstep)
    Y = numpy.arange(ymin, ymax+ystep, ystep)
    X,Y = numpy.meshgrid(X,Y)
    Z = numpy.zeros(X.shape)
    height, width = X.shape
    for i in range(0, height):
        for j in range(0,width):
            halfway = dataArray[dataArray[:,0]==X[i,j]] # finds all with that value of x
            row = halfway[halfway[:,1]==Y[i,j]] # finds y value 
            Z[i,j] = row[0,6]
    return X,Y,Z

在此先感谢

推荐答案

如果你的数据是像你这样的例子给了一个,你已经有一个目(你有z的数值为每对(X,Y) ),你只需要重塑数组:

If your data is like the one you gave in the example, you already have a mesh (you have a value of z for each pair (x,y)) and you only need to reshape the arrays:

cols = np.unique(xs).shape[0]
X = xs.reshape(-1, cols)
Y = ys.reshape(-1, cols)
Z = zs.reshape(-1, cols)

这篇关于matplotlib从一维数组创建二维数组 - 有没有更好的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆