Python與R語言,Matlab繪製三維空間座標圖

Python

# 導入畫圖模塊
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
import numpy as np

# 定義兩個函數
def z1(x,y):
    return 0.02 * x + 0.015 * y - 0.00008 * x * y + 0.00007 * x * x - 0.00002 * y * y + 15
def z2(x,y):
    return 0.02 * x + 0.015 * y - 0.00008 * x * y + 0.00007 * x * x - 0.00002 * y * y + 20

x = np.linspace(0,100,40,endpoint = False)
y = np.linspace(0,300,40,endpoint = False)
X, Y = np.meshgrid(x, y)
Z1 = z1(X,Y)
Z2 = z2(X,Y)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z1, rstride=1, cstride=1, cmap='viridis', edgecolor='none')
ax.plot_surface(X, Y, Z2, rstride=1, cstride=1, cmap='viridis', edgecolor='none')
# ax.plot_wireframe(X, Y, Z1,color = 'orange')
# ax.plot_wireframe(X, Y, Z2,color = 'skyblue')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
#調整觀察角度和方位角。這裏將俯仰角設爲60度,把方位角調整爲35度
ax.view_init(60, 35)
ax.set_title('surface')
# C:/Users/Admin/Desktop/result.jpg爲圖片存儲路徑,改爲你自己的路徑即可
plt.savefig('C:/Users/Admin/Desktop/result.jpg')
plt.show()

在這裏插入圖片描述

R語言繪製

library(plotly)

x = seq(0,100,5)
y = seq(0,400,5)
z = seq(15, 19,1)
z1 = outer(x, y, FUN = function(x, y) 0.02*x+0.015*y-0.00008*x*y+0.00007*x^2-0.00002*y^2+15)
z2 = outer(x, y, FUN = function(x, y) 0.02*x+0.015*y-0.00008*x*y+0.00007*x^2-0.00002*y^2+16)
plot_ly(showscale=FALSE)%>%add_surface(x=x,y=y,z=~z1,color="black")%>%add_surface(x=x,y=y,z=~z2,colors="red")

在這裏插入圖片描述

Matlab繪製

clear
clc
x=-300:5:100;
y=-80:5:20;
for i=1:length(x)
    for j=1:length(y)
        z1(i,j)=0.02*x(i)+0.015*y(j)-0.00008*x(i).*y(j)+0.00007*x(i).^2-0.00002*y(j).^2+15;
        z2(i,j)=0.02*x(i)+0.015*y(j)-0.00008*x(i).*y(j)+0.00007*x(i).^2-0.00002*y(j).^2+20;
    end
end
surf(y,x,z1)
hold on
mesh(y,x,z2)
shading interp
alpha(0.3)
colormap (summer)
axis([-100 30 -350 150 10 30])
xlabel('x')
ylabel('y')
zlabel('z')
title('surface')

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章