问题
I need to plot a f(z) function on 3D matplotlib, but on a local x axis, defined by two dots and fill in between them, to stay as shown in the image:

I have these two points that define the local axis x, 20 values between them and the corresponding 20 values of f(z), but I can not figure out how to plot. Can someone help me?
valuesx = np.arange(0.0, 5, 5/20) # local axis values x
self.listax.append(valuesx)
for l in valuesx:
fy= 2*x**2-4 #equation
fyx = eval(fy, {'x': l})
self.listay.append(fyx)
x = [self.listax]
y = [self.listay]
z = [1, 5]
verts = [list(zip(x, y, z))]
self.axes.add_collection3d(Poly3DCollection(verts, facecolor = 'red', alpha=0.6), zs='z')
self.fig.canvas.draw()
回答1:
Sorry this is a bit of a quick and dirty answer, but the following example should help you:
https://matplotlib.org/gallery/mplot3d/polys3d.html
Adapting the above to your example:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
import numpy as np
%matplotlib inline
def f(x):
return (2*x**2-4)
valuesx = np.arange(0.0, 5, 5/20)
valuesy= np.array([f(i) for i in valuesx])
def polygon_under_graph(xlist, ylist):
'''
Construct the vertex list which defines the polygon filling the space under
the (xlist, ylist) line graph. Assumes the xs are in ascending order.
'''
return [(xlist[0], 0.)] + list(zip(xlist, ylist)) + [(xlist[-1], 0.)]
zs = 0
fig = plt.figure()
ax = fig.gca(projection='3d')
verts=[]
verts.append(polygon_under_graph(valuesx, valuesy))
poly = PolyCollection(verts, facecolors='r')
ax.add_collection3d(poly, zs=zs, zdir='x')
ax.set_xlim(0, 5)
ax.set_ylim(0, 4)
ax.set_zlim(np.min(ys), np.max(ys))
Should give you:
You can then adjust the limits how you want, and adjust the zs variable to plot along the value on the x axis.
来源:https://stackoverflow.com/questions/52322665/how-to-plot-a-function-oriented-on-a-local-x-axis-matplotlib-3d