我需要在3Dmatplotlib上绘制一个f(z)函数,但在局部x轴上,由两个点定义,并填充它们之间,以保持如图所示:

我有这两个点来定义局部轴x,它们之间的20个值和f(z)的相应的20个值,但是我不知道如何作图。有人能帮我吗?
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() 发布于 2018-09-14 22:02:02
很抱歉,这是一个有点快速和肮脏的答案,但以下示例应该会对您有所帮助:
https://matplotlib.org/gallery/mplot3d/polys3d.html
将以上内容应用于您的示例:
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))应该会给你带来:

然后,您可以根据需要调整限制,并调整zs变量以沿x轴上的值绘制。
https://stackoverflow.com/questions/52322665
复制相似问题