how to draw a line in 3d space in python
Matplotlib was introduced keeping in mind, only two-dimensional plotting. Merely at the time when the release of 1.0 occurred, the 3d utilities were adult upon the 2nd and thus, nosotros have 3d implementation of data available today! The 3d plots are enabled by importing the mplot3d toolkit. In this commodity, we volition deal with the 3d plots using matplotlib.
Example:
Python3
import
numpy as np
import
matplotlib.pyplot as plt
fig
=
plt.figure()
ax
=
plt.axes(projection
=
'3d'
)
Output:
With the above syntax 3 -dimensional axes are enabled and information can be plotted in 3 dimensions. 3 dimension graph gives a dynamic approach and makes information more than interactive. Similar ii-D graphs, we tin can apply different ways to stand for three-D graph. We can brand a besprinkle plot, contour plot, surface plot, etc. Let's have a look at different 3-D plots.
Plotting iii-D Lines and Points
Graph with lines and signal are the simplest 3 dimensional graph. ax.plot3d and ax.scatter are the role to plot line and point graph respectively.
Example ane: 3 dimensional line graph
Python3
from
mpl_toolkits
import
mplot3d
import
numpy as np
import
matplotlib.pyplot as plt
fig
=
plt.figure()
ax
=
plt.axes(projection
=
'3d'
)
z
=
np.linspace(
0
,
1
,
100
)
x
=
z
*
np.sin(
25
*
z)
y
=
z
*
np.cos(
25
*
z)
ax.plot3D(x, y, z,
'green'
)
ax.set_title(
'3D line plot geeks for geeks'
)
plt.evidence()
Output:
Example 2: 3 dimensional scattered graph
Python3
from
mpl_toolkits
import
mplot3d
import
numpy as np
import
matplotlib.pyplot as plt
fig
=
plt.figure()
ax
=
plt.axes(projection
=
'3d'
)
z
=
np.linspace(
0
,
1
,
100
)
ten
=
z
*
np.sin(
25
*
z)
y
=
z
*
np.cos(
25
*
z)
c
=
x
+
y
ax.besprinkle(x, y, z, c
=
c)
ax.set_title(
'3d Besprinkle plot geeks for geeks'
)
plt.bear witness()
Output:
Plotting Surface graphs and Wireframes
Surface graph and Wireframes graph work on gridded data. They take filigree value and plot information technology on three-dimensional surface.
Example 1: Surface graph
Python3
from
mpl_toolkits
import
mplot3d
import
numpy every bit np
import
matplotlib.pyplot every bit plt
x
=
np.outer(np.linspace(
-
2
,
2
,
ten
), np.ones(
10
))
y
=
10.re-create().T
z
=
np.cos(x
*
*
2
+
y
*
*
three
)
fig
=
plt.figure()
ax
=
plt.axes(projection
=
'3d'
)
ax.plot_surface(x, y, z, cmap
=
'viridis'
, edgecolor
=
'green'
)
ax.set_title(
'Surface plot geeks for geeks'
)
plt.show()
Output:
Instance 2: Wireframes
Python3
from
mpl_toolkits
import
mplot3d
import
numpy as np
import
matplotlib.pyplot as plt
def
f(x, y):
render
np.sin(np.sqrt(x
*
*
2
+
y
*
*
2
))
10
=
np.linspace(
-
1
,
5
,
10
)
y
=
np.linspace(
-
1
,
5
,
ten
)
10, Y
=
np.meshgrid(x, y)
Z
=
f(X, Y)
fig
=
plt.figure()
ax
=
plt.axes(projection
=
'3d'
)
ax.plot_wireframe(X, Y, Z, color
=
'green'
)
ax.set_title(
'wireframe geeks for geeks'
);
Output:
Plotting Profile Graphs
Contour graph takes all the input data in two-dimensional regular grids, and the Z data is evaluated at every indicate.We use ax.contour3D role to plot a contour graph.
Example:
Python3
from
mpl_toolkits
import
mplot3d
import
numpy as np
import
matplotlib.pyplot equally plt
def
f(ten, y):
return
np.sin(np.sqrt(x
*
*
2
+
y
*
*
3
))
ten
=
np.linspace(
-
1
,
5
,
10
)
y
=
np.linspace(
-
1
,
5
,
x
)
X, Y
=
np.meshgrid(x, y)
Z
=
f(X, Y)
fig
=
plt.figure()
ax
=
plt.axes(projection
=
'3d'
)
ax.contour3D(Ten, Y, Z)
Output:
Plotting Surface Triangulations
The above graph is sometimes overly restricted and inconvenient. So by this method, nosotros employ a set of random draws. The function ax.plot_trisurf is used to draw this graph. It is not that clear only more than flexible.
Example:
Python3
from
mpl_toolkits
import
mplot3d
import
numpy as np
import
matplotlib.pyplot every bit plt
theta
=
2
*
np.pi
*
np.random.random(
100
)
r
=
6
*
np.random.random(
100
)
ten
=
np.ravel(r
*
np.sin(theta))
y
=
np.ravel(r
*
np.cos(theta))
z
=
f(x, y)
ax
=
plt.axes(projection
=
'3d'
)
ax.scatter(x, y, z, c
=
z, cmap
=
'viridis'
, linewidth
=
0.25
);
ax
=
plt.axes(project
=
'3d'
)
ax.plot_trisurf(x, y, z, cmap
=
'viridis'
, edgecolor
=
'green'
);
Output:
Plotting Möbius strip
Möbius strip likewise called the twisted cylinder, is a 1-sided surface without boundaries. To create the Möbius strip call up about its parameterization, it'due south a 2-dimensional strip, and we need two intrinsic dimensions. Its bending range from 0 to ii pie around the loop and width ranges from -i to i.
Example:
Python3
from
mpl_toolkits
import
mplot3d
import
numpy as np
import
matplotlib.pyplot as plt
from
matplotlib.tri
import
Triangulation
theta
=
np.linspace(
0
,
2
*
np.pi,
10
)
west
=
np.linspace(
-
1
,
v
,
8
)
west, theta
=
np.meshgrid(west, theta)
phi
=
0.five
*
theta
r
=
1
+
w
*
np.cos(phi)
x
=
np.ravel(r
*
np.cos(theta))
y
=
np.ravel(r
*
np.sin(theta))
z
=
np.ravel(w
*
np.sin(phi))
tri
=
Triangulation(np.ravel(w), np.ravel(theta))
ax
=
plt.axes(projection
=
'3d'
)
ax.plot_trisurf(x, y, z, triangles
=
tri.triangles,
cmap
=
'viridis'
, linewidths
=
0.two
);
Output:
jacksontreadevent77.blogspot.com
Source: https://www.geeksforgeeks.org/three-dimensional-plotting-in-python-using-matplotlib/
0 Response to "how to draw a line in 3d space in python"
Post a Comment