V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
nccers
V2EX  ›  Python

谁知道怎么用 python 画等值面图啊?

  •  
  •   nccers · 2016-07-13 17:06:06 +08:00 · 5237 次点击
    这是一个创建于 2851 天前的主题,其中的信息可能已经有所发展或是发生改变。

    老师给了我一组数据, 让我把图画出来 效果是这样的 http://v1.freep.cn/3tb_160713170646njy5512293.jpg 数据在这里 http://yun.baidu.com/share/link?shareid=3048138222&uk=637680708 啊 我快要崩溃了, python 到底要怎样才能生成三维网格? 到底要怎样才能三维插值? 谁能帮帮我啊!

    9 条回复    2016-07-14 02:21:43 +08:00
    nccers
        2
    nccers  
    OP
       2016-07-13 19:48:27 +08:00
    @justou 我都看了 contour3d 需要一个三维网格与对应的值, interpn 也只能对三维网格插值, 问题是我的数据是散点, 要用 mgrid 生成三维网格然后再用, griddata 插值, 我现在就卡在 griddata 插值上了, 这个函数搜来搜去只有 matlab 的例子, python 只有简单的一句话没有例子 .我是实在没办法了, 能帮我写个例子么?
    nccers
        3
    nccers  
    OP
       2016-07-13 19:50:04 +08:00
    @justou griddata 插值 1D 和 2D 都有例子, 可 3D 没有, 怎么办才好...
    lazydao
        4
    lazydao  
       2016-07-13 20:23:15 +08:00 via iPad
    为啥一点要 Python ?
    nccers
        5
    nccers  
    OP
       2016-07-13 20:29:18 +08:00
    @lazydao 我和老师夸下海口, 说自己能用 python 画出来. 汗
    nccers
        6
    nccers  
    OP
       2016-07-13 20:30:29 +08:00
    nccers
        7
    nccers  
    OP
       2016-07-13 20:32:22 +08:00
    python 卡在 griddata 函数上了 他需要构建一个 ndarray 的 tuple 但没例子我也不知道这个 tuple 是什么样子的啊
    justou
        8
    justou  
       2016-07-14 02:16:44 +08:00
    不太清楚你的问题细节, 写了个 3d 插值的例子:

    # -*- coding: utf-8 -*-
    import numpy as np
    from scipy.interpolate import interpn
    from enthought.mayavi import mlab

    mlab.options.offscreen = True

    original_points = 20j
    interp_points = 40j
    figsize = (1280, 720)

    x, y, z = np.mgrid[-15:14:original_points, -12:12:original_points, -14:15:original_points]
    # x.shape: 20x20x20
    # y.shape: 20x20x20
    # z.shape: 20x20x20
    s = np.sin(x*y*z)/(x*y*z) # s.shape: 20x20x20
    mlab.contour3d(x, y, z, s)
    mlab.savefig("original.png", size=figsize)

    xs, ys, zs = np.mgrid[-15:14:interp_points, -12:12:interp_points, -14:15:interp_points]

    # Construct a 3d array of 3-dimensional points.
    arr_4d = np.concatenate((xs[..., None], ys[..., None], zs[..., None]), axis=3) # shape: 40x40x40x3
    ss = interpn((x[:, 0, 0], y[0, :, 0], z[0, 0, :]), s, arr_4d, method="nearest")

    # Weirdly though, this will also work, why?(゚д゚)
    sss = interpn((x[:, 0, 0], y[0, :, 0], z[0, 0, :]), s, (xs, ys, zs), method="nearest")
    print np.all(ss == sss) # True

    mlab.contour3d(xs, ys, zs, ss)
    mlab.savefig("interpolate.png", size=figsize)
    justou
        9
    justou  
       2016-07-14 02:21:43 +08:00
    axis 改成-1 好一点
    np.concatenate((xs[..., None], ys[..., None], zs[..., None]), axis=-1)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2344 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:21 · PVG 20:21 · LAX 05:21 · JFK 08:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.