我正尝试绘制许多xz图,每个处于不同的y值,并覆盖表面。我已经看到了许多有关如何在python中绘制3D曲面的示例,但是除了this post之外,似乎没有什么与我的询问非常吻合。
下面是我需要做的图像(注意:忽略“常数x”-这是由于变量布局比我在这里解释的更为复杂):
我的代码如下,仅获取数据并绘制各个幅值与频率图(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轴绘制(如上图所示)。。你能帮我做到吗?
答案 0 :(得分:6)
这是一个非常类似于您的绘图的示例。
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()
查看结果: