Python 处理dat文件并画图
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Python 处理dat文件并画图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            import numpy as np
import my_class
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
file = r"D:\lunwen\lunwenshuju\各種作業數據\H500.txt"
f = open(file, "r")
data = np.loadtxt(file, dtype=str,delimiter='\n')
lines = f.readlines()
dataset = []
for line in lines:data = line.strip("\n")data = data.split('\t')dataset.append(data)
dataset = np.array(dataset)
a = np.zeros(48)
i = 0
b = 0
while i < 48:a[i] = bi = i+1b = b+18
a = a.astype(int)  # 化為整數
dataset = np.delete(dataset, a, axis=0)
# 刪除空格
# 建立一個37*17*48的三維數組
hgt = np.zeros((17, 37, 48))
# 將二維變成三維
for k in range(48):for i in range(17):c = dataset[i][0]c = c.replace(" ", "")w = 6for j in range(37):hgt[i, j, k] = float(c[w-6:w])w = w+6
# 對hgt進行處理
# 畫出氣候場
hgt_qihou = hgt.mean(2)
# 畫出距平場
hgt_juping = hgt
for i in range(48):hgt_juping[:, :, i] = hgt_juping[:,:,i]-hgt_qihou
hgt_juping = hgt_juping.mean(2)
#  畫出均方差場
hgt_junfangcha = hgt
for i in range(48):hgt_junfangcha[:,:,i] =hgt_junfangcha[:,:,i]+np.power((hgt_junfangcha[:,:,i] - hgt_qihou), 2)
hgt_junfangcha = hgt_junfangcha.mean(2)
hgt_junfangcha = np.sqrt(hgt_junfangcha)hgt_max = np.max(hgt_qihou)  # 算出最大值
hgt_min = np.min(hgt_qihou)  # 算出最小值
# print(hgt_qihou.shape)
# print(hgt_min)
# print(hgt_max)# 畫圖
lon = np.arange(60, 150+2.5, 2.5)  # 設立經度
lat = np.arange(0, 40+2.5, 2.5)  # 設立緯度
lon, lat = np.meshgrid(lon, lat)
#  畫圖設置
plt.rcParams.update({'font.size': 13})
#  解決中文亂碼問題
plt.rcParams['font.sans-serif'] = ['KaiTi']
#  解決負號亂碼問題
plt.rcParams['axes.unicode_minus'] = False
fig = plt.figure(figsize=[8, 8])
ax = fig.add_subplot(111, projection=ccrs.PlateCarree())
# 設置x, y軸標簽
ax.set_xticks(np.arange(60, 150+7.5, 7.5), crs=ccrs.PlateCarree())
ax.set_yticks(np.arange(0, 40+2.5, 5), crs=ccrs.PlateCarree())
xtick_str = ['60', ' ',  '75',' ',  '90',' ', '105',' ', '120',' ', '135',' ', '150']
ax.set_xticklabels(xtick_str, fontsize=18)
ytick_str = ['0', ' ', '10', ' ', '20', ' ', '30', ' ', '40']
ax.set_yticklabels(ytick_str, fontsize=18)
my_class.readshapefile(r"D:\lunwen\lunwenshuju\Shp\ocean_shp\Pacific\Export_Output_5.shp", linewidth=1, ax=ax)  # 太平洋
my_class.readshapefile(r"D:\lunwen\lunwenshuju\Shp\ne_10m_admin_0_countries\ne_10m_admin_0_countries.shp", linewidth=1, ax=ax)  #世界地圖 
my_class.readshapefile(r"D:\lunwen\lunwenshuju\Shp\ocean_shp\Indian\iho.shp", linewidth=1, ax=ax)  # 印度洋
my_class.readshapefile(r"D:\lunwen\lunwenshuju\Shp\china\china.shp", linewidth=1, ax=ax)
levels = np.arange(5310, 5890, 1)
precipRateESurface_conf = ax.contourf(lon, lat, hgt_qihou, cmap='RdBu_r', extend='both', levels=levels)
T_cb = fig.colorbar(precipRateESurface_conf, orientation='vertical', shrink=0.75)
# 標簽字體設置
font = {'family': 'Times New Roman','weight': 'normal','size': 20
}
T_cb.set_label("gpm", fontdict=font)
ax.set_title("500hP高度場的氣候場")
plt.show()
 
                        
                        
                        dat數據將后綴改成txt后長這樣:
?上面沒有經緯度,只有對應經緯度的數據,因此先進行數據處理。
思路:首先每行讀取數據按照經緯度,數據是由北向南,由西向東的。通過np.delete()刪除每行年份和月份。然后將字符轉化成數字讀取轉化成三維數據,最后進行數據處理后繪圖就行
總結
以上是生活随笔為你收集整理的Python 处理dat文件并画图的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Autoware-激光雷达目标检测与跟踪
- 下一篇: AV1学习笔记一
