arkkimede
arkkimede

Reputation: 105

mplot3d (python) why plotting a line in 3d the coordinates need the metod flatten

I'm starting to learn python and the related graphical library. After some experience in 2D I started to use 3D. What I would like to do is plotting a circle in 3D. I report a minimal example

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

fig = plt.figure(figsize=plt.figaspect(1))  # only solution to define axis aspect equal
ax = fig.add_subplot((111), projection='3d')

t = np.linspace(0, np.pi * 2, 360, endpoint=True)

x = np.cos(t)
y = np.sin(t)
z = zeros((1, len(x)))

ax.plot(x.flatten(), y.flatten(), z.flatten(), color='red')

plt.show()

The question is: why if I use only x, y, z (without flatten) I obtain an error like:

input operand has more dimensions than allowed by the axis remapping?

Thank you

Upvotes: 0

Views: 109

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40697

Your problem is the shape of z. You've defined it as (1,N), when it should be (N,). Use z = np.zeros(shape=t.shape) and you won't need to flatten your array anymore

Upvotes: 1

Related Questions