0

So I needed the help of matplotlib to visualize a few things in multivariable calculus, and one of them is a parametric curve. I don't really know python properly but I tried to understand the example on their website (https://matplotlib.org/stable/gallery/mplot3d/lines3d.html#sphx-glr-gallery-mplot3d-lines3d-py) and plot my own parametric curve.

import numpy as np
import matplotlib.pyplot as plt
ax = plt.figure().add_subplot(projection = '3d')
theta = np.linspace(0, 4*np.pi, 100)
x = np.cos(theta) + 3
y = 0
z = np.sin(theta)

now when I entered the above lines of code in IDLE there was no error, but then when I entered

ax.plot(x,y,z, label='Parametric curve')

It gave me the error :

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    ax.plot(x,y,z, label = 'parametric curve')
  File "C:\Users\Koustubh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1572, in plot
    lines = super().plot(xs, ys, *args, **kwargs)
  File "C:\Users\Koustubh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\matplotlib\axes\_axes.py", line 1605, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]
  File "C:\Users\Koustubh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\matplotlib\axes\_base.py", line 315, in __call__
    yield from self._plot_args(this, kwargs)
  File "C:\Users\Koustubh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\matplotlib\axes\_base.py", line 501, in _plot_args
    raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (100,) and (1,)

so can anyone help me with what did I do wrong here ? I am just trying really hard to visualize a parametric curve, but don't know any python yet. Edit #1 : so I replaced y= 0 with y = np.sin(theta)-np.sin(theta) and it worked fine and I got my plot. But why didn't defining y as zero normally work ?

Koustubh Jain
  • 129
  • 1
  • 7
  • 1
    Your ``y`` only have one single value. If you'd like your y to be zero everywhere, write ``np.zeros((len(x)))`` – Karina Oct 13 '21 at 05:39
  • What is the logic that makes you think ``y = 0`` could work? I'm not sure how to explain it, but maybe I can explain it better if I understand your point of view. – Karina Oct 13 '21 at 10:20
  • idk, I typed it that way and it did render the curve correctly – Koustubh Jain Oct 14 '21 at 16:29

0 Answers0