matplotlib(5)-- 等高線contourf、contour;imshow() ; colorbar(); 3D數據處理


 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 #定義計算高度的函數
 5 def f(x,y):
 6     return (1 - x/2 + x**5 +y**3) * np.exp(-x**2 - y**2)
 7 
 8 n = 256
 9 x = np.linspace(-3, 3, n)
10 y = np.linspace(-3, 3, n)
11 #核心函數是plt.contourf(),但在這個函數中輸入的參數是x,y對應的網格數據以及此網格對應的高度值
12 #因此我們調用np.meshgrid(x,y)把x,y值轉換成網格數據
13 X, Y = np.meshgrid(x, y)
14 
15 #plt.contourf 與 plt.contour 區別:
16 # f:filled,也即對等高線間的填充區域進行填充(使用不同的顏色)
17 # contourf:將不會再繪制等高線(顯然不同的顏色分界就表示等高線本身)
18 
19 #use plt.contourf to filling contours(輪廓)
20 #X, Y and value for (X ,Y) point
21 plt.contourf(X, Y,f(X, Y), 8, alpha = 0.75, cmap = plt.get_cmap('rainbow'))
22 
23 #use plt.contour to add contour lines
24 C = plt.contour(X, Y,f(X, Y), 8, colors = "black", linewidth = .5)
25 #關於cmap顏色選擇,參考 https://matplotlib.org/examples/color/colormaps_reference.html
26 
27 #adding label(為等高線上注明等高線的含義)
28 plt.clabel(C, inline = True, fontsize = 10)
29 
30 plt.xticks(())
31 plt.yticks(())
32 
33 plt.show()

 

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 # image data
 5 a = np.array([0.313660827978, 0.365348418405, 0.423733120134,
 6               0.365348418405, 0.439599930621, 0.525083754405,
 7               0.423733120134, 0.525083754405, 0.651536351379]).reshape(3,3)
 8 
 9 #reshape()是數組對象中的方法,用於改變數組的形狀。具體用法參考博文:https://blog.csdn.net/qq_28618765/article/details/78083895
10 
11 #plt.imshow()參數設置參考 https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imshow.html
12 plt.imshow(a, interpolation='nearest', cmap='cool', origin='upper')
13 #參數interpolation為邊界的模糊度或者圖片的模糊度,參考博文 https://blog.csdn.net/qq_41634283/article/details/84933753
14 #參數origin: {'upper', 'lower'}, optional
15     # 將數組的[0,0]索引放置在軸的左上角或左下角。約定“上”通常用於矩陣和圖像。如果未給出,則使用rcparams[“image.origin”],默認為“upper”。
16 
17 plt.colorbar(shrink=.92)
18 #給figure添加顏色條或者漸變條,參考博文 https://www.jianshu.com/p/d97c1d2e274f
19 #colorbar()參數設置參考https://matplotlib.org/api/_as_gen/matplotlib.pyplot.colorbar.html
20 
21 plt.xticks(())
22 plt.yticks(())
23 
24 plt.show()

 

 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3 from mpl_toolkits.mplot3d import Axes3D        #導入繪制3D數據的模塊
 4 
 5 fig = plt.figure()
 6 ax = Axes3D(fig)
 7 # X, Y value
 8 X = np.arange(-4, 4, 0.25)
 9 Y = np.arange(-4, 4, 0.25)
10 X, Y = np.meshgrid(X, Y)  #np.meshgrid(x,y)把x,y值轉換成網格數據
11 # height value
12 Z = np.sin(np.sqrt(X ** 2 + Y ** 2))
13 
14 ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'), edgecolor = "black")
15 #plot_surfac()參數設置參考 https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html
16 # *X*, *Y*, *Z*:Data values as 2D arrays
17 # *rstride* :Array row stride (step size), defaults to 10
18 # *cstride*:Array column stride (step size), defaults to 10
19 #  *cmap*:A colormap for the surface patches.
20 ax.contourf(X, Y, Z, zdir = "z", offset = -2, cmap = "rainbow" )
21 
22 ax.set_zlim(-2, 2)
23 
24 plt.show()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM