英文:
How do I plot a parametrized function in matplotlib?
问题
如何在matplotlib中绘制这样的函数?这是一个返回点(x,y,z)的函数:
def f(t):
return (t, t+4, t)
这应该描述了三维空间中的一条线。
将它们分成三个单独的分量函数对我来说是不可接受的。
英文:
How do I plot such function in matplotlib? It a function that returns a point (x,y,z):
def f(t):
return (t, t+4, t)
This is supposed to describe a line in 3D space
Splitting them up into three separate component functions is not acceptable for me.
答案1
得分: 3
You don't need to split up the function. Just use numpy to create vectors and unpack the result for the plotting.
import numpy as np
import matplotlib.pyplot as plt
plt.close("all")
def f(t):
return (t, t+4, t)
t = np.linspace(0, 10, 100)
fig, ax = plt.subplots(subplot_kw={"projection":"3d"})
ax.plot(*f(t))
ax.set(xlabel="x", ylabel="y", zlabel="z")
fig.show()
英文:
You don't need to split up the function. Just use numpy to create vectors and unpack the result for the plotting.
import numpy as np
import matplotlib.pyplot as plt
plt.close("all")
def f(t):
return (t, t+4, t)
t = np.linspace(0, 10, 100)
fig, ax = plt.subplots(subplot_kw={"projection":"3d"})
ax.plot(*f(t))
ax.set(xlabel="x", ylabel="y", zlabel="z")
fig.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论