I am attempting to take many xz plots, each at various y values, and overlay a surface. I have seen many examples of how to plot 3D surfaces in python, but aside from this post, nothing seems to match my inquiry very closely.
The image of what I need to do is shown below (Note: ignore the "constant x" - this is due to a more complicated variable layout than I explained here):
My code is as follows, and simply takes data and plots each of the individual Magnitude vs. Frequency plots (xz plots):
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()
The next thing I need is to grab each plot, find the y value at which the data was taken, and plot along a y axis (shown coming out of the page in the previous picture). Can you help me do that?
Here is an example that resembles your drawing quite well.
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()
See result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With