首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在python中使用3D表面覆盖2D绘图

在python中使用3D表面覆盖2D绘图
EN

Stack Overflow用户
提问于 2018-07-18 19:57:17
回答 1查看 1.2K关注 0票数 9

我正在尝试许多xz图,每个在不同的y值,并且覆盖一个表面。我见过很多关于如何在python中绘制3D表面的例子,但除了this post之外,似乎没有什么能与我的查询非常接近。

我需要做的图片如下所示(注意:忽略“常量x”-这是由于一个比我在这里解释的更复杂的变量布局):

我的代码如下所示,简单地获取数据并绘制每个单独的幅值与频率图(xz图):

代码语言:javascript
复制
import numpy as np
import glob, os
import codecs
import re
import matplotlib.pyplot as plt
#-------------------------------------------------------------------------                   
os.chdir('C:/Users/elarrick/Desktop/201807161056')
dataFolders = glob.glob('./*.ltda')

dataLines = []
freq = []
OpenLoopMag = []
OpenLoopPhase = []
for item in dataFolders:
    name, ext = os.path.splitext(item)
    if ext == '.ltda':
        print item
        dataLines = []
        f = codecs.open(item, encoding='utf-8')
        for line in f:
            if '<p>' in line:
                dataLines.append(line)      #All lines with <p> are an entry in dataLines
        #print "\n\n", dataLines
        #break
        for item in dataLines:
            item = re.sub("<p>", "", item)
            item = re.sub("True</p>", "", item)
            item = item.replace(",", "")
            splitItem = item.split()
            #print splitItem
            freq.append(float(splitItem[0]))
            OpenLoopMag.append(float(splitItem[1]))
            OpenLoopPhase.append(float(splitItem[2]))
        print "Frequencies: ", freq
        print "\n\n\n\n\n\nOpenLoopMag: ", OpenLoopMag
#   This is where I will make the plots for each x,y position
        name = name.strip(".\\")
        name = name.replace("-","NEG")
        plt.semilogx(freq, OpenLoopMag)
        #plt.plot(freq, OpenLoopMag)
        plt.xlabel("Frequency, (Hz)")
        plt.ylabel("Magnitude")
        plt.title("{0}".format(name))
        plt.xlim([20,2000])
        #plt.ylim([-43.2,10.9])
        ticks = [20,40,70,100,200,400,700,1000,2000]
        plt.xticks(ticks,ticks)
        plt.savefig("plot_{0}.png".format(name))

#________ Clear the values for the next data folder_______#            
        freq = []
        OpenLoopMag = []
        OpenLoopPhase = []
        break
    else:
        print "Something went wrong - check ColorMap.py"
        sys.exit()

我需要做的下一件事是抓取每个图,找到获取数据的y值,然后沿着y轴(如上一张图片中的页面所示)绘制。你能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2018-07-18 23:14:57

下面是一个与您的绘图非常相似的示例。

代码语言:javascript
复制
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt
import numpy as np

# Generate test data
Ny,Nx = 100,100
x = np.linspace(0,1,Nx)
y = np.linspace(0,1,Ny)
x,y = np.meshgrid(x,y)
z = (1 - x**2) * y**.5

# Indices for the y-slices and corresponding slice positions along y-axis
slice_i = [20, 40, 60, 80]
slice_pos = np.array(slice_i) / Ny

def polygon_under_graph(x, y):
    '''
    Construct the vertex list which defines the polygon filling the space under
    the (x, y) line graph.  Assumes the xlist are in ascending order.
    '''
    return [(x[0], 0.)] + list(zip(x, y)) + [(x[-1], 0.)]

fig = plt.figure()
ax = fig.gca(projection='3d')

verts = []
for i in slice_i:
    verts.append(polygon_under_graph(x[i], z[i]))

poly = PolyCollection(verts, facecolors='gray', edgecolors='k')

# add slices to 3d plot
ax.add_collection3d(poly, zs=slice_pos, zdir='y')

# plot surface between first and last slice as a mesh
ax.plot_surface(x[slice_i[0]:slice_i[-1]], 
                y[slice_i[0]:slice_i[-1]],
                z[slice_i[0]:slice_i[-1]], 
                rstride=10, cstride=10, alpha=0, edgecolors='k')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

查看结果:

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51401442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档