如何在matplotlib中绘制参数化函数?

huangapple go评论89阅读模式
英文:

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

如何在matplotlib中绘制参数化函数?

英文:

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

如何在matplotlib中绘制参数化函数?

huangapple
  • 本文由 发表于 2023年7月6日 11:20:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定