1. 繪制三維曲面圖及其投影圖
2.?繪制曲面圖
3. 繪制曲面投影圖
4. 同時繪制曲面圖和投影圖,用兩個圖展示
5. 繪制曲面圖
6. 同時繪制曲面圖及其二維填色圖
數(shù)據(jù)分析包括探索、清理和轉(zhuǎn)換數(shù)據(jù)以從中提取有用信息。Python有許多庫可以使數(shù)據(jù)分析變得更容易,例如Pandas、NumPy和SciPy。以下是使用Python進行數(shù)據(jù)分析的基本步驟:
-
加載數(shù)據(jù):您可以使用Pandas從各種來源加載數(shù)據(jù),例如CSV、Excel文件、SQL數(shù)據(jù)庫或API。
-
探索數(shù)據(jù):使用Pandas查看數(shù)據(jù)及其結(jié)構(gòu)。您可以檢查空值、數(shù)據(jù)類型和摘要統(tǒng)計信息。此步驟有助于更好地了解數(shù)據(jù)并識別任何問題。
-
清理數(shù)據(jù):您可以使用Pandas處理缺失值、重復(fù)值、異常值和不正確的數(shù)據(jù)。在分析數(shù)據(jù)之前,數(shù)據(jù)清理是必不可少的步驟。
-
轉(zhuǎn)換數(shù)據(jù):Pandas提供了一些轉(zhuǎn)換數(shù)據(jù)的工具,包括篩選、分組、合并和透視。此步驟有助于為分析數(shù)據(jù)做準(zhǔn)備。
-
分析數(shù)據(jù):您可以使用Python庫,例如NumPy、SciPy和Pandas進行各種類型的分析,包括統(tǒng)計分析、機器學(xué)習(xí)和數(shù)據(jù)建模。
可視化是傳達數(shù)據(jù)洞察力和模式的強大工具。Python有許多數(shù)據(jù)可視化庫,包括Matplotlib、Seaborn和Plotly。以下是使用Python創(chuàng)建可視化的基本步驟:
-
加載數(shù)據(jù):您可以像上面描述的那樣加載數(shù)據(jù)。
-
選擇可視化:選擇適當(dāng)?shù)目梢暬绞?,例如散點圖、條形圖、熱圖等。
-
創(chuàng)建可視化:使用所選庫創(chuàng)建可視化,包括選擇數(shù)據(jù)、定義圖表類型和自定義圖表外觀。
-
自定義可視化:您可以自定義圖表的各種特征,包括軸標(biāo)簽、標(biāo)題、顏色和注釋。
-
保存或顯示可視化:一旦您創(chuàng)建和自定義了可視化,可以將其保存到文件或在Jupyter筆記本或其他Python環(huán)境中顯示。
使用Python進行數(shù)據(jù)分析和可視化是一個廣闊的領(lǐng)域,這僅僅是一個概述。
1.?繪制三維曲面圖及其等高線投影圖
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# 創(chuàng)建三維圖形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 生成數(shù)據(jù)
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)
# 繪制曲面圖
ax.plot_surface(x, y, z, cmap='viridis')
# 繪制投影圖
ax.contourf(x, y, z, zdir='z', offset=-15, cmap='viridis')
# 調(diào)整Z方向距離
ax.set_zlim(-15, 3)
# 顯示圖形
plt.show()
這是一個使用matplotlib
庫創(chuàng)建3D圖形的Python代碼,包括曲面圖和等高線投影圖。
代碼的第一部分導(dǎo)入必要的庫并設(shè)置3D圖形。第二部分生成數(shù)據(jù),該數(shù)據(jù)是x
和y
的函數(shù),并使用numpy
庫計算相應(yīng)的z
值。第三部分使用ax.plot_surface()
函數(shù)繪制曲面圖和ax.contourf()
函數(shù)繪制等高線投影圖。cmap
參數(shù)指定用于繪圖的顏色映射。
ax.set_zlim()
函數(shù)用于調(diào)整Z軸限制,并使用plt.show()
函數(shù)顯示圖形。
總體而言,該代碼創(chuàng)建了一個3D圖形,其中包括曲面圖和等高線投影圖,使用顏色映射來顯示函數(shù)在z
方向上的變化。
2.? 繪制曲面圖
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# 創(chuàng)建三維圖形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 生成數(shù)據(jù)
x = np.arange(5)
y = np.arange(5)
x, y = np.meshgrid(x, y)
z = np.array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
# 繪制曲面圖
ax.plot_surface(x, y, z)
# 設(shè)置坐標(biāo)軸標(biāo)簽
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# 顯示圖形
plt.show()
3.?加載三列數(shù)據(jù)文件,繪制曲面投影圖
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
# 加載數(shù)據(jù)
data = np.loadtxt('fes.dat')
x = data[:, 0]
y = data[:, 1]
z = data[:, 2]*4.3597*6.022*100
# 定義網(wǎng)格
xi = np.linspace(min(x), max(x), 100)
yi = np.linspace(min(y), max(y), 100)
X, Y = np.meshgrid(xi, yi)
# 插值數(shù)據(jù)到網(wǎng)格上
Z = griddata((x, y), z, (X, Y), method='cubic')
# 繪制投影圖
plt.imshow(Z, cmap='viridis', extent=[min(xi), max(xi), min(yi), max(yi)], origin='lower')
# 添加標(biāo)簽和標(biāo)題
plt.xlabel('X')
plt.ylabel('Y')
plt.title('等高線投影圖')
# 添加色標(biāo)
plt.colorbar()
# 顯示圖形
plt.show()
?4.?加載三列數(shù)據(jù)文件,同時繪制曲面圖和投影圖,用兩個圖展示
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
from mpl_toolkits.mplot3d import Axes3D
# 加載數(shù)據(jù)
data = np.loadtxt('fes.dat')
x = data[:, 0]
y = data[:, 1]
z = data[:, 2]*4.3597*6.022*100
# 定義網(wǎng)格
xi = np.linspace(min(x), max(x), 100)
yi = np.linspace(min(y), max(y), 100)
X, Y = np.meshgrid(xi, yi)
# 插值數(shù)據(jù)到網(wǎng)格上
Z = griddata((x, y), z, (X, Y), method='cubic')
# 繪制投影圖
fig = plt.figure(figsize=(10, 5))
ax1 = fig.add_subplot(1, 2, 1)
im = ax1.imshow(Z, cmap='viridis', extent=[min(xi), max(xi), min(yi), max(yi)], origin='lower')
ax1.set_xlabel('X')
ax1.set_ylabel('Y')
ax1.set_title('等高線投影圖')
fig.colorbar(im, ax=ax1)
# 繪制曲面圖
ax2 = fig.add_subplot(1, 2, 2, projection='3d')
ax2.plot_surface(X, Y, Z, cmap='viridis')
ax2.set_xlabel('X')
ax2.set_ylabel('Y')
ax2.set_zlabel('Z')
ax2.set_title('3D曲面圖')
# 顯示圖形
plt.show()
5.?加載三列數(shù)據(jù)文件,繪制曲面圖
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 加載數(shù)據(jù)
data = np.loadtxt('fes.dat')
x = data[:, 0]
y = data[:, 1]
z = data[:, 2]*4.3597*6.022*100
# 創(chuàng)建3D坐標(biāo)軸
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 繪制曲面
ax.plot_trisurf(x, y, z, cmap='viridis')
# 添加標(biāo)簽和標(biāo)題
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('曲面圖')
# 顯示圖形
plt.show()
6. 在同一張圖中繪制曲面圖及其二維填色圖
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.interpolate import griddata
# 設(shè)置 DPI,圖像清晰度
# 通常在 100 到 300 DPI 之間選擇一個合適的值即可。如果需要更高的分辨率,可以考慮使用矢量格式的圖像,如 PDF、SVG 等,它們不受 DPI 的限制,可以隨意縮放而不會失去清晰度。
plt.rcParams['figure.dpi'] = 600
# 加載數(shù)據(jù)
data = np.loadtxt('fes.dat')
x = data[:, 0]
y = data[:, 1]
z = data[:, 2]*4.3597*6.022*100
# 創(chuàng)建3D坐標(biāo)軸
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 繪制曲面
ax.plot_trisurf(x, y, z, cmap='viridis')
# 添加標(biāo)簽和標(biāo)題
ax.set_xlabel('Ti-B coordination number')
ax.set_ylabel('Ti-Al coordination number')
ax.set_zlabel('Energy (kJ/mol)')
ax.set_title('3-dimension contour and surface plot')
# 定義網(wǎng)格
xi = np.linspace(min(x), max(x)+0.5, 500)
yi = np.linspace(min(y)-0.5, max(y), 500)
X, Y = np.meshgrid(xi, yi)
# 設(shè)置縱軸坐標(biāo)刻度范圍
# ax.set_zlim(-30, 0)
# 插值數(shù)據(jù)到網(wǎng)格上
Z = griddata((x, y), z, (X, Y), method='linear')
# 繪制投影圖
contour = ax.contourf(X, Y, Z, cmap='viridis', levels=40, offset=-25)
# fig.colorbar(contour)
# 添加colorbar
# fig.add_axes() 方法用于在圖形中添加新的坐標(biāo)軸對象,參數(shù)指定了新坐標(biāo)軸的位置和大小。這個方法接受一個參數(shù)列表 [left, bottom, width, height],這里的 left 表示新坐標(biāo)軸的左邊緣位置, bottom 表示下邊緣位置, width 表示坐標(biāo)軸的寬度, height 表示坐標(biāo)軸的高度。
cbar_ax = fig.add_axes([0.88, 0.10, 0.02, 0.7])
fig.colorbar(contour, cax=cbar_ax)
# 設(shè)置圖片大小
# fig.set_size_inches(10, 6)
# 顯示圖形
plt.show()
這個腳本從名為'fes.dat'的文件中加載數(shù)據(jù),文件包含三列數(shù)據(jù):第一列和第二列對應(yīng)于二維平面上的x和y坐標(biāo),第三列對應(yīng)于每個點的值z。腳本然后使用matplotlib將數(shù)據(jù)繪制成3D曲面,添加軸標(biāo)簽和標(biāo)題,并使用scipy將數(shù)據(jù)插值到網(wǎng)格上。最后,它創(chuàng)建一個二維等高線圖,將插值數(shù)據(jù)投影到x-y平面上,并顯示整個圖形。
以下是腳本的詳細說明:文章來源:http://www.zghlxwxcb.cn/news/detail-760215.html
- 導(dǎo)入必要的庫:numpy、matplotlib、mpl_toolkits.mplot3d和scipy.interpolate。
- 使用numpy.loadtxt從文件'fes.dat'中加載數(shù)據(jù),并將x、y和z數(shù)據(jù)提取到單獨的數(shù)組中。
- 創(chuàng)建一個新的圖形并添加一個3D軸,使用matplotlib.pyplot.figure和matplotlib.pyplot.subplot。
- 使用mpl_toolkits.mplot3d.Axes3D.plot_trisurf將原始數(shù)據(jù)繪制成3D曲面。
- 使用mpl_toolkits.mplot3d.Axes3D.set_xlabel、mpl_toolkits.mplot3d.Axes3D.set_ylabel、mpl_toolkits.mplot3d.Axes3D.set_zlabel和mpl_toolkits.mplot3d.Axes3D.set_title將軸標(biāo)簽和標(biāo)題添加到繪圖中。
- 使用numpy.linspace和numpy.meshgrid定義一個新的x-y點網(wǎng)格。
- 使用scipy.interpolate.griddata將原始數(shù)據(jù)插值到新網(wǎng)格上。
- 使用mpl_toolkits.mplot3d.Axes3D.contourf創(chuàng)建插值數(shù)據(jù)的二維等高線圖。
- 使用matplotlib.pyplot.show顯示整個圖形。
注意,# fig.colorbar(contour)
行被注釋掉了。如果取消注釋這行代碼,它會向圖中添加一個顏色條,顯示顏色和z值之間的對應(yīng)關(guān)系。文章來源地址http://www.zghlxwxcb.cn/news/detail-760215.html
參考資料
- Matplotlib Official Website: Tutorials — Matplotlib 3.7.0 documentation
- Seaborn Official Website: User guide and tutorial — seaborn 0.12.2 documentation
- Plotly Official Website: https://plotly.com/python/
- DataCamp: https://www.datacamp.com/
- Udacity: Learn the Latest Tech Skills; Advance Your Career | Udacity
- Coursera: https://www.coursera.org/
到了這里,關(guān)于【python繪圖(一)】Python數(shù)據(jù)分析和可視化的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!