0

I want to simulate the position of a tennis ball in a 3D plot. It want it to be a line equal to the tennis ball's position on a tennis court after it has been hit. The problem is that my code plots a blue figure instead go a line as shown in the link below. What do I do to make it a blue line?

In addition, I get an error:

TypeError: plot() missing 3 required positional arguments: 'self', 'xs', and 'ys'

I don't know how it affects the result of my code though

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

x=np.array([0,3,5,7,9,11,13,15,17,19,21,23])
y=np.array([0,0.1,0.2,0.3,0.4,0.6,0.9,1,1.2,1.4,1.7,1.9])
z=np.array([1,1.2,1.5,1.7,1.8,1.9,2,1.9,1.8,1.7,1.5,1.2])

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_trisurf(x,y,z,)
plt.show()
Axes3D.plot()

I expect the result to be a blue line instead of a blue figure as you see in the figure below

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
Kristian
  • 3
  • 2
  • Newbie-friendly explanation of the error thrown by `Axes3D.plot()` with keywords for further googling: `.plot()` is a [method](https://stackoverflow.com/questions/3786881/what-is-a-method-in-python) of the `Axes3D` *class*. The usual way to use these would be to have an *instance* of the class (here, `ax`) and use the prefix notation `ax.plot(arguments)`, but Python allows you to call it via `Axes3D.plot(ax, arguments)`. Hence `Axes3D.plot()` is allowed syntax, but fails at runtime because it lacks three arguments (the reference to the instance + the 2 method arguments). – Leporello May 27 '19 at 14:57

1 Answers1

0

You can either use ax.plot(x,y,z,) or plot3D(x,y,z,) as following. The blue surface comes from plot_trisurf

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

x=np.array([0,3,5,7,9,11,13,15,17,19,21,23])
y=np.array([0,0.1,0.2,0.3,0.4,0.6,0.9,1,1.2,1.4,1.7,1.9])
z=np.array([1,1.2,1.5,1.7,1.8,1.9,2,1.9,1.8,1.7,1.5,1.2])

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot3D(x,y,z,) # or ax.plot(x,y,z,)
plt.show()

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Thank you so much!! If you have any ideas on how I can simulate a tenniscourt (with net and lines) it would be great:) – Kristian May 25 '19 at 11:51
  • @Kristian : I would suggest you to ask a new question on that. There are several answers on how to do that. For example, [this](https://stackoverflow.com/questions/38118598/3d-animation-using-matplotlib), [this](https://stackoverflow.com/questions/54392281/matplotlib-code-for-3d-line-animation-on-top-of-a-3d-surface), [this](https://matplotlib.org/gallery/animation/random_walk.html) – Sheldore May 25 '19 at 11:57