Python中的3D繪圖命令總結(jié)
導語
很多情況下,為了能夠觀察到數(shù)據(jù)之間的內(nèi)部的關(guān)系,可以使用繪圖來更好的顯示規(guī)律。
比如在下面的幾張動圖中,使用matplotlib中的三維顯示命令,使得我們可以對于logistic回歸網(wǎng)絡的性能與相關(guān)參數(shù)有了更好的理解。


下面的動圖顯示了在訓練網(wǎng)絡時,不同的學習速率對于算法收斂之間的影響。


下面給出了繪制這些動態(tài)曲線的相關(guān)的python指令:
01 3D plot
1.基本語法
在安裝matplotlib之后,自動安裝有 mpl_toolkits.mplot3d。
#Importing Libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
#3D Plotting
fig = plt.figure()
ax = plt.axes(projection="3d")
#Labeling
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')
plt.show()
2.Python Cmd
使用pythoncmd 插入相應的語句。
3.舉例
(1) Ex1
#!/usr/local/bin/python
# -*- coding: gbk -*-
#******************************
# TEST2.PY -- by Dr. ZhuoQing 2020-11-16
#
# Note:
#******************************
from headm import *
from mpl_toolkits.mplot3d import axes3d
ax = plt.axes(projection='3d')
x = [1,2,3,4,5,6,7,8,9]
y = [2,3,4,6,7,8,9,5,1]
z = [5,6,2,4,8,6,5,6,1]
ax.plot3D(x,y,z)
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')
plt.show()
#------------------------------------------------------------
# END OF FILE : TEST2.PY
#******************************
▲ 3D plot的演示
(2) Ex2
from mpl_toolkits.mplot3d import axes3d
ax = plt.axes(projection='3d')
angle = linspace(0, 2*pi*5, 400)
x = cos(angle)
y = sin(angle)
z = linspace(0, 5, 400)
ax.plot3D(x,y,z)
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')
plt.show()
▲ 3D繪制的例子
(3) Ex3
import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import numpy as np import matplotlib.pyplot as plt mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d') theta = np.linspace(-4 * np.pi, 4 * np.pi, 100) z = np.linspace(-2, 2, 100) r = z**2 + 1 x = r * np.sin(theta) y = r * np.cos(theta) ax.plot(x, y, z, label='parametric curve') ax.legend() plt.show()

02 繪制Scatter
利用和上面的相同的繪制命令,將原來的plot3D修改成為 scatter即可。
from mpl_toolkits.mplot3d import axes3d
ax = plt.axes(projection='3d')
angle = linspace(0, 2*pi*5, 40)
x = cos(angle)
y = sin(angle)
z = linspace(0, 5, 40)
ax.scatter(x,y,z, color='b')
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')
plt.show()
▲ Scatter 的例子
03 繪制3D Surface
(1) Ex1

▲ 3D surface例子
#!/usr/local/bin/python
# -*- coding: gbk -*-
#******************************
# TEST2.PY -- by Dr. ZhuoQing 2020-11-16
#
# Note:
#******************************
from headm import *
from mpl_toolkits.mplot3d import axes3d
ax = plt.axes(projection='3d')
x = arange(-5, 5, 0.1)
y = arange(-5, 5, 0.1)
x,y = meshgrid(x, y)
R = sqrt(x**2+y**2)
z = sin(R)
ax.plot_surface(x, y, z)
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')
plt.show()
#------------------------------------------------------------
# END OF FILE : TEST2.PY
#******************************
▲ 3D 繪制Surface

▲ 繪制3D球表面
(2) 舉例
'''
***********
3D surface (color map)
***********
Demonstrates plotting a 3D surface colored with the coolwarm color map.
The surface is made opaque by using antialiased=False.
Also demonstrates using the LinearLocator and custom formatting for the
z axis tick labels.
'''
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
▲ 彩色表面繪制
以上就是Python中的3D繪圖命令總結(jié)的詳細內(nèi)容,更多關(guān)于Python 3D繪圖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
py3nvml實現(xiàn)GPU相關(guān)信息讀取的案例分析
這篇文章主要介紹了py3nvml實現(xiàn)GPU相關(guān)信息讀取,此時就可以考慮使用py3nvml這樣的工具,針對于GPU任務執(zhí)行的過程進行細化的分析,有助于提升GPU的利用率和程序執(zhí)行的性能,需要的朋友可以參考下2022-01-01
Python Pandas中根據(jù)列的值選取多行數(shù)據(jù)
這篇文章主要介紹了Python Pandas中根據(jù)列的值選取多行數(shù)據(jù)的實例代碼,本文通過實例代碼給大家介紹的非常詳細 ,需要的朋友可以參考下2019-07-07
Python如何用str.format()批量生成網(wǎng)址(豆瓣讀書為例)
這篇文章主要介紹了Python如何用str.format()批量生成網(wǎng)址(豆瓣讀書為例),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
matplotlib中plt.hist()參數(shù)解釋及應用實例
本文主要介紹了matplotlib中plt.hist()參數(shù)解釋及應用實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
Python數(shù)學建模StatsModels統(tǒng)計回歸之線性回歸示例詳解
這篇文章主要為大家介紹了Python數(shù)學建模中StatsModels統(tǒng)計回歸之線性回歸的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10

