如何在局部x轴matplotlib 3d上绘制函数?

2024-09-29 23:30:56 发布

您现在位置:Python中文网/ 问答频道 /正文

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

here

我有这两个点来定义局部轴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() 

Tags: 函数self定义matplotlibnp绘制局部append
1条回答
网友
1楼 · 发布于 2024-09-29 23:30:56

抱歉,这是一个有点快速和肮脏的答案,但下面的例子应该可以帮助您:

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

应该给你:

enter image description here

然后,可以根据需要调整限制,并调整zs变量以沿x轴上的值绘制。你知道吗

相关问题 更多 >

    热门问题