How to change angle of 3D plot in Python? (original) (raw)

Last Updated : 03 Jan, 2021

Prerequisites: Matplotlib, NumPy

In this article, we will see how can we can view our graph from different angles, Here we use three different methods to plot our graph. Before starting let’s see some basic concepts of the required module for this objective.

Approach:

Step 1: Import libraries.

Python3

from mpl_toolkits import mplot3d

import numpy as np

import matplotlib.pyplot as plt

Step 2: Plotting3-D axis figure.

Python3

from mpl_toolkits import mplot3d

import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure(figsize = ( 8 , 8 ))

ax = plt.axes(projection = '3d' )

Step 3: Creating a Datasheet for all the 3-axis of the sample.

Python3

from mpl_toolkits import mplot3d

import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure(figsize = ( 8 , 8 ))

ax = plt.axes(projection = '3d' )

z = np.linspace( 0 , 15 , 1000 )

x = np.sin(z)

y = np.cos(z)

ax.plot3D(x, y, z, 'green' )

Step 4: Use view_init() can be used to rotate the axes programmatically.

Syntax: view_init(elev, azim)

Parameters:

Python3

from mpl_toolkits import mplot3d

import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure(figsize = ( 8 , 8 ))

ax = plt.axes(projection = '3d' )

z = np.linspace( 0 , 15 , 1000 )

x = np.sin(z)

y = np.cos(z)

ax.plot3D(x, y, z, 'green' )

ax.view_init( - 140 , 60 )

Below is the full Implementation:

Example 1: In this example, we will plot a curve graph with an elevated angle of -140 degrees and a horizontal angle of 60 degrees.

Python3

from mpl_toolkits import mplot3d

import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure(figsize = ( 8 , 8 ))

ax = plt.axes(projection = '3d' )

z = np.linspace( 0 , 15 , 1000 )

x = np.sin(z)

y = np.cos(z)

ax.plot3D(x, y, z, 'green' )

ax.view_init( - 140 , 60 )

plt.show()

Output:

Example 2: In this example, we will plot a curve graph with an elevated angle of 120 degrees and a horizontal angle of 30 degrees.

Python3

from mpl_toolkits import mplot3d

import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure(figsize = ( 8 , 8 ))

ax = plt.axes(projection = '3d' )

z = np.linspace( 0 , 15 , 1000 )

x = np.sin(z)

y = np.cos(z)

ax.plot3D(x, y, z, 'green' )

ax.view_init( 120 , 30 )

plt.show()

Output:

Example 3: In this example, we will plot a rectangular plate graph with an angle view of 50 degrees.

Python3

import numpy as np

from matplotlib import pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

from math import sin, cos

fig = plt.figure(figsize = ( 8 , 8 ))

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

y = np.linspace( - 1 , 1 , 200 )

x = np.linspace( - 1 , 1 , 200 )

x,y = np.meshgrid(x, y)

z = x + y

a = 50

t = np.transpose(np.array([x, y, z]), ( 1 , 2 , 0 ))

m = [[cos(a), 0 , sin(a)],[ 0 , 1 , 0 ],

`` [ - sin(a), 0 , cos(a)]]

X,Y,Z = np.transpose(np.dot(t, m), ( 2 , 0 , 1 ))

ax.set_xlabel( 'X' )

ax.set_ylabel( 'Y' )

ax.set_zlabel( 'Z' )

ax.plot_surface(X,Y,Z, alpha = 0.5 ,

`` color = 'red' )

plt.show()

Output: