0

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.

MrSomeone
  • 71
  • 1
  • 11

1 Answers1

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

jared
  • 4,165
  • 1
  • 8
  • 31
  • This works for now. Do the functions have to be designed to use numpy functions like np.sin or would generic python functions work? – MrSomeone Jul 06 '23 at 03:56
  • 1
    @MrSomeone It depends on what operations you're doing. But if you want to take the sine of a value, you'll have to use `np.sin` instead of `math.sin`. What other operations do you have in mind? – jared Jul 06 '23 at 04:08
  • No special operations in mind, just wanting to know how arbitrary these functions can be – MrSomeone Jul 06 '23 at 05:01
  • @MrSomeone If you do go down this path, read up on numpy. The basics can get you pretty far. – jared Jul 06 '23 at 05:23