如何在Matplotlib中的3D视图中绘制2D流线 [英] How to plot a 2d streamline in 3d view in matplotlib

查看:334
本文介绍了如何在Matplotlib中的3D视图中绘制2D流线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在3d视图中绘制2d流线,例如 this .根据帖子的建议,我需要从2d提取流线和箭头绘制,然后将其转换为3d数据.如何将2d流线数据转换为3d数据并使用mplot3d进行绘图?

I need to plot a 2d streamline in 3d view like this. As suggested by the post, I need to extract streamlines and arrows from a 2d plot and then transform it to 3d data. How to transform this 2d streamline data to 3d data and plot using mplot3d?

预先感谢

Raj

@ gg349,在您的帮助下,我可以在3d视图中绘制流线型.情节是此处

@gg349, with your help I could plot streamline in 3d view. The plot is here

我有两个问题:

  1. 如何像以前的如何提取imshow()数据并将其绘制在3d中.带有imshow()的2d流线为此处

    How to extract a imshow() data and plot it in 3d. The 2d streamline with imshow() is here

    推荐答案

    该示例将帮助您入门:

    import matplotlib.pyplot as plt
    import numpy as np
    
    fig_tmp, ax_tmp = plt.subplots()
    x, y = np.mgrid[0:2.5:1000j, -2.5:2.5:1000j]
    vx, vy = np.cos(x - y), np.sin(x - y)
    res = ax_tmp.streamplot(x.T, y.T, vx, vy, color='k')
    fig_tmp.show()
    # extract the lines from the temporary figure
    lines = res.lines.get_paths()
    #for l in lines:
    #    plot(l.vertices.T[0],l.vertices.T[1],'k')
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    for line in lines:
        old_x = line.vertices.T[0]
        old_y = line.vertices.T[1]
        # apply for 2d to 3d transformation here
        new_z = np.exp(-(old_x ** 2 + old_y ** 2) / 4)
        new_x = 1.2 * old_x
        new_y = 0.8 * old_y
        ax.plot(new_x, new_y, new_z, 'k')
    

    这将生成一个中间临时图形:

    this generates an intermediate temporary figure:

    从中提取线.然后,您可以按自己的喜好应用2d到3d点转换,并在新的3d图形中绘制相同的线条:

    from which the lines are extracted. Then you apply your 2d to 3d point transformation of your liking, and plot the same lines in a new 3d figure:

    这篇关于如何在Matplotlib中的3D视图中绘制2D流线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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