[轉]python (matplotlib)畫三維圖像


[轉]python (matplotlib)畫三維圖像

覺得有用的話,歡迎一起討論相互學習~

我的微博我的github我的B站

版權聲明:本文為CSDN博主「Mr-Cat伍可貓」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/Mr_Cat123/article/details/100054757

文章目錄

關於三維圖像的內容很多博友已經寫了
推薦: 三維繪圖畫三維圖3d圖-英文版中文版三維圖
上面寫的都非常詳細,很推薦,特別是英文版和中文版三維圖那個,基於此,只給我寫的一個例子

1 三維圖

畫 f ( x , y ) = x 2 + y 2 f(x,y)=x^2+y^2 f(x,y)=x2+y2的三維圖
在這里插入圖片描述

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = np.arange(-10,10,0.2)
y = np.arange(-10,10,0.2)
f_x_y=np.power(x,2)+np.power(y,2)
fig = plt.figure()
ax = plt.gca(projection='3d')
ax.plot(x,y,f_x_y)

畫出2維不相關高斯分布的3維圖,即下面公式中n=2的情況
在這里插入圖片描述
在這里插入圖片描述

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
from mpl_toolkits.mplot3d import Axes3D #畫三維圖不可少
from matplotlib import cm  #cm 是colormap的簡寫

# 1_dimension gaussian function
def gaussian(x,mu,sigma):
    f_x = 1/(sigma*np.sqrt(2*np.pi))*np.exp(-np.power(x-mu, 2.)/(2*np.power(sigma,2.)))
    return(f_x)

# 2_dimension gaussian function
def gaussian_2(x,y,mu_x,mu_y,sigma_x,sigma_y):
    f_x_y = 1/(sigma_x*sigma_y*(np.sqrt(2*np.pi))**2)*np.exp(-np.power\
              (x-mu_x, 2.)/(2*np.power(sigma_x,2.))-np.power(y-mu_y, 2.)/\
              (2*np.power(sigma_y,2.)))
    return(f_x_y)

#設置2維表格
x_values = np.linspace(-5,5,2000)
y_values = np.linspace(-5,5,2000)
X,Y = np.meshgrid(x_values,y_values)
#高斯函數
mu_x,mu_y,sigma_x,sigma_y = 0,0,0.8,0.8
F_x_y = gaussian_2(X,Y,mu_x,mu_y,sigma_x,sigma_y)
#顯示三維圖
fig = plt.figure()
ax = plt.gca(projection='3d')
ax.plot_surface(X,Y,F_x_y,cmap='jet')
# 顯示等高線圖
#ax.contour3D(X,Y,F_x_y,50,cmap='jet')

2 三維等高線

將上面等高線打開,三維圖注釋掉

#ax.plot_surface(X,Y,F_x_y,cmap='jet')
# 顯示等高線圖
ax.contour3D(X,Y,F_x_y,50,cmap='jet')

在這里插入圖片描述

3 二維等高線

將上面的圖截取截面就是2維平面,是一個個圓形
在這里插入圖片描述

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
from mpl_toolkits.mplot3d import Axes3D #畫三維圖不可少
from matplotlib import cm  #cm 是colormap的簡寫

#定義坐標軸函數
def setup_axes(fig, rect):
    ax = axisartist.Subplot(fig, rect)
    fig.add_axes(ax)

    ax.set_ylim(-4, 4)
    #自定義刻度
#    ax.set_yticks([-10, 0,9])
    ax.set_xlim(-4,4)
    ax.axis[:].set_visible(False)

	#第2條線,即y軸,經過x=0的點
    ax.axis["y"] = ax.new_floating_axis(1, 0)
    ax.axis["y"].set_axisline_style("-|>", size=1.5)
#    第一條線,x軸,經過y=0的點
    ax.axis["x"] = ax.new_floating_axis(0, 0)
    ax.axis["x"].set_axisline_style("-|>", size=1.5)

    return(ax)
# 1_dimension gaussian function
def gaussian(x,mu,sigma):
    f_x = 1/(sigma*np.sqrt(2*np.pi))*np.exp(-np.power(x-mu, 2.)/(2*np.power(sigma,2.)))
    return(f_x)

# 2_dimension gaussian function
def gaussian_2(x,y,mu_x,mu_y,sigma_x,sigma_y):
    f_x_y = 1/(sigma_x*sigma_y*(np.sqrt(2*np.pi))**2)*np.exp(-np.power\
              (x-mu_x, 2.)/(2*np.power(sigma_x,2.))-np.power(y-mu_y, 2.)/\
              (2*np.power(sigma_y,2.)))
    return(f_x_y)

#設置畫布
fig = plt.figure(figsize=(8, 8)) #建議可以直接plt.figure()不定義大小
ax1 = setup_axes(fig, 111)
ax1.axis["x"].set_axis_direction("bottom")
ax1.axis['y'].set_axis_direction('right')
#在已經定義好的畫布上加入高斯函數
x_values = np.linspace(-5,5,2000)
y_values = np.linspace(-5,5,2000)
X,Y = np.meshgrid(x_values,y_values)
mu_x,mu_y,sigma_x,sigma_y = 0,0,0.8,0.8
F_x_y = gaussian_2(X,Y,mu_x,mu_y,sigma_x,sigma_y)
#顯示三維圖
#fig = plt.figure()
#ax = plt.gca(projection='3d')
#ax.plot_surface(X,Y,F_x_y,cmap='jet')
# 顯示3d等高線圖
#ax.contour3D(X,Y,F_x_y,50,cmap='jet')
# 顯示2d等高線圖,畫8條線
plt.contour(X,Y,F_x_y,8)

4 三維表面圖上畫曲線

fig = plt.figure()
ax = fig.gca(projection='3d')
temp_test = np.squeeze(temp[:,0,:,:])
Lat,Lon = np.meshgrid(lat,lon)
# Temp = np.zeros((lat.size,lon.size))
Temp = temp_test[0]    #first hour
surf = ax.plot_surface(Lat,Lon,Temp) 
fig.colorbar=(surf)  #畫表面圖
ax.plot(lat_new, lon_new, t_interp,linewidth=10,color='r')  #畫曲線

plt.show()

由於我的值結果范圍太小,看不出來,這條曲線是在表面上畫一個環
在這里插入圖片描述
在這里插入圖片描述

5 三維曲線投影到坐標軸

由於三維曲面投影到坐標軸已經有了答案,在一開始我給的鏈接或者官網都有,如下:
在這里插入圖片描述
(代碼可以點開始給的鏈接進入查看)
但是三維 曲 線 曲線 曲線的投影還沒有給,所以這里通過查找一番之后總結如下(參考python,matlab)
以下我使用的是python

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
#輸入經緯度和海拔值(也就是x,y,z)
ax.plot(lat_new, lon_new, temp_list[layer], linewidth=10, color='r')

plt.show()

在這里插入圖片描述
現在要將這個圖投影到x-z坐標面上

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(lat_new, lon_new, temp_list[layer], linewidth=10, color='r')
null = [30]*len(lat_new)  #在y=30處的面

ax.plot(null, lon_new, temp_list[layer])
# ax.plot(lat_new,null, temp_list[layer])
# ax.plot(lat_new, lon_new, null)

plt.show()

在這里插入圖片描述
同時在三個面上投影

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(lat_new, lon_new, temp_list[layer], linewidth=10, color='r')
#至於要在多大的值上投影,可以自己測試找到最合適的
x_z = [min(lat_new)-0.5]*len(lat_new)
y_z = [max(lon_new)+0.5]*len(lon_new)
x_y = [min(temp_list[layer])-0.5]*len(temp_list[layer])

ax.plot(x_z, lon_new, temp_list[layer])
ax.plot(lat_new, y_z, temp_list[layer])
ax.plot(lat_new, lon_new, x_y)

plt.show()

在這里插入圖片描述


免責聲明!

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



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