Python  - Matplotlib                                                       Home : www.sharetechnote.com

 

 

 

 

Plot3D

 

 

 

 

 

Draw a line between two points

 

This show how to draw a single line from (-1,-2,-3) to (1,2,3)

 

Filename : Line3D.py

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import mpl_toolkits.mplot3d.axes3d as axes3d

 

fig = plt.figure()

 

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

 

ax.plot([-1,1],[-2,2],[-3,3],'r--')

 

ax.set_xlim3d(-5, 5)

ax.set_ylim3d(-5, 5)

ax.set_zlim3d(-5, 5)

 

plt.show()

 

Result :

 

 

 

Drawing lines connecting multiple points

 

This show how to draw the line traces connecting multiple points

 

Filename : Line3D.py

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import mpl_toolkits.mplot3d.axes3d as axes3d

 

fig = plt.figure()

 

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

 

xList = [0,1,2,3,4]

yList = [-1,1,-1,1,-1]

zList = [2,-2,2,-2,2]

 

ax.plot(xList, yList, zList,'b')

 

ax.set_xlim3d(-5, 5)

ax.set_ylim3d(-5, 5)

ax.set_zlim3d(-5, 5)

 

plt.show()

 

Result :

 

 

 

3D Parametric Plot

 

This show how to draw 3d Parameteric Plot

 

Filename : Line3D.py

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import mpl_toolkits.mplot3d.axes3d as axes3d

 

fig = plt.figure()

 

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

 

t = np.linspace(0, 4 * np.pi, 100)

x = t

y = np.cos(t)

z = np.sin(t)

 

ax.plot(x, y, z,'b')

 

ax.set_xlim3d(0, t.max())

ax.set_ylim3d(-2, 2)

ax.set_zlim3d(-2, 2)

 

plt.show()

 

Result :

 

 

 

Ticks, Tick Mark, Axis Label

 

This show how to format tick mark,tick label,axis label

 

Filename : Line3D.py

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import mpl_toolkits.mplot3d.axes3d as axes3d

 

fig = plt.figure()

 

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

 

t = np.linspace(0, 4 * np.pi, 100)

x = t

y = np.cos(t)

z = np.sin(t)

 

ax.plot(x, y, z,'b')

 

ax.set_xlim3d(0, t.max())

ax.set_ylim3d(-2, 2)

ax.set_zlim3d(-2, 2)

 

ax.set_xlabel('x', fontsize=15, rotation=0)

ax.set_ylabel('y', fontsize=15, rotation=0)

ax.set_zlabel('z', fontsize=15, rotation=90)

 

ax.xaxis.set_ticks([0,np.pi,2*np.pi,3*np.pi,4*np.pi])

ax.set_xticklabels([0,'$\pi$','2$\pi$','3$\pi$','4$\pi$'])

 

ax.yaxis.set_ticks([-2,-1,0,1,2])

ax.zaxis.set_ticks([-2,-1,0,1,2])

 

plt.show()

 

Result :

 

 

 

Turn Off Axis, Axis Labels, Grid

 

Filename : Line3D.py

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import mpl_toolkits.mplot3d.axes3d as axes3d

 

fig = plt.figure()

 

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

 

t = np.linspace(0, 4 * np.pi, 100)

x = t

y = np.cos(t)

z = np.sin(t)

 

ax.plot(x, y, z,'b')

 

ax.set_xlim3d(0, t.max())

ax.set_ylim3d(-2, 2)

ax.set_zlim3d(-2, 2)

 

ax.set_xlabel('x', fontsize=15, rotation=0)

ax.set_ylabel('y', fontsize=15, rotation=0)

ax.set_zlabel('z', fontsize=15, rotation=90)

 

ax.xaxis.set_ticks([0,np.pi,2*np.pi,3*np.pi,4*np.pi])

ax.set_xticklabels([0,'$\pi$','2$\pi$','3$\pi$','4$\pi$'])

 

ax.yaxis.set_ticks([-2,-1,0,1,2])

ax.zaxis.set_ticks([-2,-1,0,1,2])

ax.grid(False)

ax.axis('off')

ax.set_xticks([])

ax.set_yticks([])

ax.set_zticks([])

 

plt.show()

 

Result :

 

 

 

 

 

Reference :

 

[1] Matplotlib tutorial (Nicolas P. Rougier)