gpt4 book ai didi

python - 在 python 中用 3D 表面覆盖 2D 图

转载 作者:太空狗 更新时间:2023-10-29 21:45:00 25 4
gpt4 key购买 nike

我正在尝试绘制许多 xz 图,每个图都具有不同的 y 值,并覆盖一个表面。我看过很多关于如何在 python 中绘制 3D 表面的示例,但除了 this post ,似乎没有什么与我的询问非常吻合。

我需要做的事情的图像如下所示(注意:忽略“常量 x” - 这是因为变量布局比我在这里解释的更复杂):

Magnitude vs. Frequency

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

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 轴绘图(显示出上一张图片中的页面)。你能帮我做吗?

最佳答案

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

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()

查看结果:

slices along axis in 3d

关于python - 在 python 中用 3D 表面覆盖 2D 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51401442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com