python数据分析:三维数据样条插值并制图

数据(x,y,z)代表导线宽度,长度,和升温30℃时的通流值

  • 加载模块,导入数据
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
from scipy import interpolate
from matplotlib import cm

data=pd.read_excel('./WHPdata123.xls')  #读取数据
current=data.iloc[0:8,1:7]   #提取用于分析的列
print(current)
   1     2     4     6     8     10
0  11.6  16.6  23.8  29.2  34.6  38.6
1   8.2  11.8  17.2  20.6  23.8  26.8
2   7.0  10.0  14.2  17.6  20.0  22.4
3   6.2   8.8  12.4  15.4  18.2  20.2
4   5.6   8.0  11.4  14.2  16.4  18.0
5   5.2   7.4  10.6  13.2  15.2  17.2
6   4.8   7.0  10.0  12.4  14.0  15.8
7   4.5   6.5   9.4  11.6  13.4  15.2
  • 网格化数据
current1 = np.asarray(current)    #转化为numpy数组
length1 = np.arange(1,9)
width1 = np.array([1,2,4,6,8,10])
M,N = np.meshgrid(width1,length1)   #转化为X-Y网格

plt.rcParams['font.sans-serif'] = 'SimHei' ## 设置中文显示
plt.rcParams['axes.unicode_minus'] = False   #字符显示正确
  • 原始数据画第一幅图
fig = plt.figure(figsize=(16,6))      #设置画布尺寸
ax1=plt.subplot(1,2,1,projection='3d')    #第一幅图
ax1.view_init(30,60)                 #视角(x,y)分别代表水平垂直转角
ax1.set_title('升温30℃时导线长宽与载流的关系图(WHP原始数据)',size=17) ## 添加WHP标题
ax1.set_xlabel('Width (mm)',size=15)   ## 添加x轴标签
ax1.set_ylabel('Length (mm)',size=15)  ## 添加y轴名称
ax1.set_zlabel('Current (A)',size=15)  ## 添加z轴名称
ax1.set_xlim((0,11))       ## 确定x轴范围
ax1.set_ylim((0,9))        ## 确定y轴范围
ax1.set_zlim((0,45))       ## 确定z轴范围
ax1.set_xticks(list(range(0,12,2)))  ## 规定x轴刻度
ax1.set_yticks(list(range(9)))  ## 确定y轴刻度
WHP1=ax1.plot_surface(M,N,current1,cmap=cm.coolwarm,alpha=.8,antialiased=True) #画三维曲面
ax1.scatter(M,N,current1,c='g')     #画三维散点图
plt.colorbar(pic1,boundaries=[np.min(current1),10,15,20,25,30,np.max(current1)]) #放置颜色标尺
  • 插值画第二幅图
func2=interpolate.interp2d(width1,length1,current1,kind='cubic')  #三次样条插值
length2 = np.linspace(1,8,36)
width2 = np.linspace(1,10,46)
current2=func2(width2,length2)
J,K = np.meshgrid(width2,length2)

ax2=plt.subplot(1,2,2,projection='3d')          #画第二幅图
ax2.view_init(30,60)

ax2.set_title('升温30℃时导线长宽与载流的关系图(WHPcubic)',size=17) ## 添加标题
ax2.set_xlabel('Width (mm)',size=15) ## 添加x轴标签
ax2.set_ylabel('Length (mm)',size=15) ## 添加y轴名称
ax2.set_zlabel('Current (A)',size=15) ## 添加z轴名称

ax2.set_xlim((0,11))  ## 确定x轴范围
ax2.set_ylim((0,9))  ## 确定y轴范围
ax2.set_zlim((0,45))  ## 确定z轴范围

ax2.set_xticks(list(range(11)))## 规定x轴刻度
ax2.set_yticks(list(range(9)))## 确定y轴刻度
WHP2=ax2.plot_surface(J,K,current2,cmap=cm.coolwarm,alpha=.8,antialiased=True)
ax2.scatter(J,K,current2,s=.5,c='g')
plt.colorbar(pic2,boundaries=[np.min(current2),10,15,20,25,30,np.max(current2)])
  • 保存数据,输出图面
np.savetxt("./current2.csv", current2, fmt="%.2f", delimiter=',')   #保存插值后的数据
plt.savefig('./WHP电流.png')                                          #保存图片
plt.show()

《python数据分析:三维数据样条插值并制图》

    原文作者:whp1920
    原文地址: https://blog.csdn.net/whp1920/article/details/112611750
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞