How to plot a function oriented on a local x axis matplotlib 3d?

青春壹個敷衍的年華 提交于 2021-01-28 06:51:17

问题


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:

here

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!