Matplotlib berasal dengan mengingat plot 2 dimensi. Tapi ketika versi 1. 0 akan segera dirilis, utilitas 3 dimensi dikembangkan di atas utilitas 2 dimensi. Dengan demikian, Matplotlib memiliki sub-modul lain yang berpotensi merender implementasi 3D dari data yang tersedia saat ini. Tutorial ini akan memberi Anda pemahaman lengkap tentang plotting 3D menggunakan Matplotlib
Daftar isi
Matplotlib 3D Plotting dalam Visualisasi Data
Visualisasi data adalah vertikal khusus ilmu data di mana banyak perpustakaan dikembangkan menggunakan Python. Di antaranya, Matplotlib mendapatkan pilihan paling populer untuk merender visualisasi data untuk mendapatkan wawasan yang lebih baik. Awalnya, Matplotlib digunakan untuk membuat bagan plot 2D seperti bagan garis, bagan batang, histogram, plot pencar, plot pai, dll. Pembuat Matplotlib memutuskan untuk memperluas kemampuannya untuk mengirimkan modul perencanaan 3D juga. Plot tiga dimensi dapat digunakan dengan mengimpor toolkit mplot3d;
Sintaksis
from mpl_toolkits import mplot3dHasilkan Plot 3D Kosong
Plot 3D kosong dapat dibuat dengan meneruskan kata kunci proyeksi = '3D' ke fungsi pembuatan sumbu
Submodul axes3d_ yang disertakan dalam toolkit mpl_toolkits.mplot3d Matplotlib menyediakan metode yang diperlukan untuk membuat plot permukaan 3D dengan Python
Plot Permukaan
Plot permukaan dibuat dengan metode ax.plot_surface() Matplotlib. Secara default, plot permukaan adalah satu warna. Format umum metode ax.plot_surface() Matplotlib ada di bawah ini
ax.plot_surface(X, Y, Z) _Di mana from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np #if using a Jupyter notebook, include: %matplotlib inline
x = np.arange(-5,5,0.1) y = np.arange(-5,5,0.1) X,Y = np.meshgrid(x,y) Z = X*np.exp(-X2 - Y2)
fig = plt.figure(figsize=(6,6)) ax = fig.add_subplot(111, projection='3d')
# Plot a 3D surface ax.plot_surface(X, Y, Z)
plt.show()
_1 dan from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np #if using a Jupyter notebook, include: %matplotlib inlinex = np.arange(-5,5,0.1) y = np.arange(-5,5,0.1) X,Y = np.meshgrid(x,y) Z = X*np.exp(-X2 - Y2)
fig = plt.figure(figsize=(6,6)) ax = fig.add_subplot(111, projection='3d')
# Plot a 3D surface ax.plot_surface(X, Y, Z)
plt.show()
2 adalah larik 2D dari titik x dan y dan from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np #if using a Jupyter notebook, include: %matplotlib inlinex = np.arange(-5,5,0.1) y = np.arange(-5,5,0.1) X,Y = np.meshgrid(x,y) Z = X*np.exp(-X2 - Y2)
fig = plt.figure(figsize=(6,6)) ax = fig.add_subplot(111, projection='3d')
# Plot a 3D surface ax.plot_surface(X, Y, Z)
plt.show()
3 adalah larik 2D dengan ketinggianContoh plot permukaan 3D ada di bagian kode berikutnya. Perhatikan bagaimana argumen kata kunci from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np #if using a Jupyter notebook, include: %matplotlib inline
x = np.arange(-5,5,0.1) y = np.arange(-5,5,0.1) X,Y = np.meshgrid(x,y) Z = X*np.exp(-X2 - Y2)
fig = plt.figure(figsize=(6,6)) ax = fig.add_subplot(111, projection='3d')
# Plot a 3D surface ax.plot_surface(X, Y, Z)
plt.show()
_4 disertakan dalam metode from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np #if using a Jupyter notebook, include: %matplotlib inlinex = np.arange(-5,5,0.1) y = np.arange(-5,5,0.1) X,Y = np.meshgrid(x,y) Z = X*np.exp(-X2 - Y2)
fig = plt.figure(figsize=(6,6)) ax = fig.add_subplot(111, projection='3d')
# Plot a 3D surface ax.plot_surface(X, Y, Z)
plt.show()
5Dalam [1]
from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np #if using a Jupyter notebook, include: %matplotlib inline
x = np.arange(-5,5,0.1) y = np.arange(-5,5,0.1) X,Y = np.meshgrid(x,y) Z = X*np.exp(-X2 - Y2)
fig = plt.figure(figsize=(6,6)) ax = fig.add_subplot(111, projection='3d')
# Plot a 3D surface ax.plot_surface(X, Y, Z)
plt.show()
Plot Bingkai Kawat
Plot bingkai kawat mirip dengan plot permukaan, tetapi pada plot bingkai kawat seluruh permukaan 3d tidak ditampilkan. Sebaliknya, permukaannya didekati dengan "kabel" yang diletakkan di atas permukaan 3D. Plot permukaan 3D bingkai kawat dapat dibangun menggunakan metode from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np #if using a Jupyter notebook, include: %matplotlib inline
x = np.arange(-5,5,0.1) y = np.arange(-5,5,0.1) X,Y = np.meshgrid(x,y) Z = X*np.exp(-X2 - Y2)
fig = plt.figure(figsize=(6,6)) ax = fig.add_subplot(111, projection='3d')
# Plot a 3D surface ax.plot_surface(X, Y, Z)
plt.show()
6 Matplotlib. Cara umumnya ada di bawah iniax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) _Di mana from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np #if using a Jupyter notebook, include: %matplotlib inline
x = np.arange(-5,5,0.1) y = np.arange(-5,5,0.1) X,Y = np.meshgrid(x,y) Z = X*np.exp(-X2 - Y2)
fig = plt.figure(figsize=(6,6)) ax = fig.add_subplot(111, projection='3d')
# Plot a 3D surface ax.plot_surface(X, Y, Z)
plt.show()
_1 dan from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np #if using a Jupyter notebook, include: %matplotlib inlinex = np.arange(-5,5,0.1) y = np.arange(-5,5,0.1) X,Y = np.meshgrid(x,y) Z = X*np.exp(-X2 - Y2)
fig = plt.figure(figsize=(6,6)) ax = fig.add_subplot(111, projection='3d')
# Plot a 3D surface ax.plot_surface(X, Y, Z)
plt.show()
2 adalah larik 2D dari titik x dan y dan from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np #if using a Jupyter notebook, include: %matplotlib inlinex = np.arange(-5,5,0.1) y = np.arange(-5,5,0.1) X,Y = np.meshgrid(x,y) Z = X*np.exp(-X2 - Y2)
fig = plt.figure(figsize=(6,6)) ax = fig.add_subplot(111, projection='3d')
# Plot a 3D surface ax.plot_surface(X, Y, Z)
plt.show()
3 adalah larik 2D dengan ketinggian. Argumen kata kunci ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) 0 dan ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) 1 menentukan ukuran langkah baris dan ukuran langkah kolom. Argumen kata kunci ini mengontrol seberapa dekat "kabel" di plot bingkai kawat ditarikBagian kode selanjutnya menggambar dua plot bingkai kawat berdampingan
Di [2]
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import axes3d # if using a Jupyter notebook, include: %matplotlib inline
fig = plt.figure(figsize=(12,6)) ax1 = fig.add_subplot(121, projection='3d') ax2 = fig.add_subplot(122, projection='3d')
x = np.arange(-5,5,0.1) y = np.arange(-5,5,0.1) X,Y = np.meshgrid(x,y) Z = X*np.exp(-X2 - Y2)
# Plot a basic wireframe ax1.plot_wireframe(X, Y, Z, rstride=10, cstride=10) ax1.set_title('row step size 10, column step size 10')
ax2.plot_wireframe(X, Y, Z, rstride=20, cstride=20) ax2.set_title('row step size 20, column step size 20')
plt.show()
Plot Permukaan Gradien
Plot permukaan gradien menggabungkan plot permukaan 3D dengan plot kontur 2D. Dalam plot permukaan gradien, permukaan 3D diwarnai seperti plot kontur 2D. Bagian permukaan yang tinggi mengandung warna yang berbeda dari bagian permukaan yang rendah. Panggilan metode umum ada di bawah. Perhatikan argumen kata kunci ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) 2
surf = ax.plot_surface(X, Y, Z, cmap=<color map>, linewidth=0, antialiased=False)Argumen kata kunci ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) _3 menetapkan warna ke permukaan. Ada beragam pilihan peta warna di Matplotlib. Pilihannya mencakup ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) _4, ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) 5, dan ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) 6. Temukan semua peta warna Matplotlib di dokumentasi Matplotlib di matplotlib. org/tutorial/warna/peta warna. Bilah warna dapat ditambahkan di sepanjang sisi plot dengan memanggil metode ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) 7 dan mengirimkan objek plot permukaan
Bagian kode berikutnya membuat plot permukaan gradien menggunakan peta warna ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) 5
Dalam [3]
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import axes3d # if using a Jupyter notebook, include: %matplotlib inline
fig = plt.figure(figsize=(10,6)) ax1 = fig.add_subplot(111, projection='3d')
x = np.arange(-5,5,0.1) y = np.arange(-5,5,0.1) X,Y = np.meshgrid(x,y) Z = X*np.exp(-X2 - Y2)
mycmap = plt.get_cmap('gist_earth') ax1.set_title('gist_earth color map') surf1 = ax1.plot_surface(X, Y, Z, cmap=mycmap) fig.colorbar(surf1, ax=ax1, shrink=0.5, aspect=5)
plt.show()
Plot Permukaan 3D dengan Proyeksi Plot Kontur 2D
Plot Permukaan 3D yang dibuat dengan Matplotlib dapat diproyeksikan ke permukaan 2D. Di bawah ini adalah bagian kode yang membuat plot permukaan 3D. Proyeksi permukaan 3D divisualisasikan pada plot kontur 2D