昆明地图
def kmdt(x):
import matplotlib.pyplot as plt
import json
with open('km.json','rb') as f:
data=json.load(f)
for fea in range(len(data['features'])):
df=data['features'][fea]['geometry']['coordinates']
for f1 in range(len(df)):
df1=df[f1]
for f2 in range(len(df1)):
d=df1[f2]
for i in range(len(d)):
km=plt.plot([d[i-1][0],d[i][0]],[d[i-1][1],d[i][1]],'k',linewidth=x
)
return km
插值
def chazhi(x,xx,cz):
import numpy as np
from scipy import interpolate
if cz=='':
cz='nearest'
lenx=len(x)
weizhi=np.where(x==x)[0]
lenw=len(weizhi)
xh=np.array([0.0]*lenw)
for i in range(0,lenw):
xh[i]=x[weizhi[i]]
f=interpolate.interp1d(weizhi,xh,kind=cz)
ynew=f(xx)
return ynew
一元回归
def huigui(x,y):
from sklearn import linear_model
import numpy as np
model = linear_model.LinearRegression()
xx=np.array(x).reshape(-1, 1)
model.fit(xx,y)
k=model.coef_
b=model.intercept_
ff='y = %.2f+%.2f'%(b,k)
return(k,b,ff)
去空值,求平均
def offnan(x,y):
import numpy as np
a=np.array(x)
b=np.array(y)
aa=list(np.where(a==a)[0])
bb=list(np.where(b==b)[0])
c=list(set(aa)&set(bb))
return (a[c],b[c])
def avern(x):
import numpy as np
a=np.array(x)
le=len(a)
su=0.0
js=0.0
for i in range(0,le):
if a[i]==a[i]:
su=su+a[i]
js=js+1
av=su/js
return av
归一化
def guiyi(x,Type='oz'):
import numpy as np
a=np.array(x)
ma=max(a)
mi=min(a)
me=np.mean(a)
b=np.array([0.0]*len(a))
if Type=='oz':
for i in range(0,len(a)):
b[i]=(a[i]-mi)/(ma-mi)
if Type=='0':
for i in range(0,len(a)):
b[i]=(a[i]-me)/(ma-mi)
return b
标准化
def biaozhun(x):
from sklearn import preprocessing
a=preprocessing.scale(x)
return(a)
def zhongxin(x):
import numpy as np
a=np.array(x)
b=np.mean(a)
for i in range(0,len(a)):
a[i]=a[i]-b
return a
获取文件列表
def listfile(dirname,postfix = ''):
import os
filelist = []
files = os.listdir(dirname)
for item in files:
#filelist.append([dirname,item])
if os.path.isfile(dirname+item):
if item.endswith(postfix):
filelist.append([dirname,item])
else:
if os.path.isdir(dirname+item):
filelist.extend(listfile(dirname+item+'/',postfix))
return filelist
灰色关联
def huise(a,b,c,rho):
import pandas as pd
import numpy as np
a=np.array(a)
length=len(c)
lent=len(a)
a=a/a[0]
aa=list(a)
data=[]
dataa=[]
szi=[0.0]*lent
zi=[]
for i in range(0,length):
cc=c[i]
bb=np.array(b[cc])
bb=bb/bb[0]
data.append(list(bb))
dataa.append(aa)
data=np.array(data)
dataa=np.array(dataa)
gmin=abs(data-dataa).min()
gmax=abs(data-dataa).max()
for i in range(0,length):
cc=c[i]
bb=np.array(b[cc])
bb=bb/bb[0]
for j in range(0,lent):
szi[j]=(gmin+rho*gmax)/(abs(a[j]-bb[j])+rho*gmax)
zi.append(list(szi))
zi=np.array(zi)
qz=np.mean(zi,1)
return qz
多元回归
def dyhuigui(y,x):
from sklearn import linear_model
import numpy as np
import pandas as pd
model = linear_model.LinearRegression()
# xx=np.array(x).reshape(-1, 1)
model.fit(x,y)
k=model.coef_
b=model.intercept_
r=model.score(x,y)
return(k,b,r)
极射赤面投影地图
def jiquditu(axes):
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.path as mpath
import matplotlib.ticker as mticker
import cartopy.crs as ccrs
ax=axes
ax.set_extent([-180, 180, 0, 90], crs=ccrs.PlateCarree())
theta = np.linspace(0, 2*np.pi, 1000)
center, radius = [0.5, 0.5], 0.5
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
circle = mpath.Path(verts * radius + center)
ax.set_boundary(circle, transform=ax.transAxes)
ax.coastlines()
line=ax.gridlines(draw_labels=False)
line.ylocator=mticker.FixedLocator([0,90])#手动设置x轴刻度
line.xlocator=mticker.FixedLocator(np.arange(-180,181,30))#手动设置x轴刻度
#创建要标注的labels字符串
ticks=np.arange(0,181,30)
etick=['0°']+['%d°E'%tick for tick in ticks if (tick !=0) & (tick!=180)]+['180°']
ticks=np.arange(150,0,-30)
wtick=['%d°W'%tick for tick in ticks if (tick !=0) & (tick!=180)]
labels=etick+wtick
#创建与labels对应的经纬度标注位置
#xticks=[i for i in np.arange(0,210,30)]+[i for i in np.arange(-32,-180,-30)]
for i in range(0,12):
p=-7
if i%6==0:
p=-3
if i%6==5 or i%6==1:
p=-5
if i%6==4 or i%6==2:
p=-6
ax.text(i*30,p,labels[i],transform=ccrs.PlateCarree(),
horizontalalignment='center',verticalalignment='center')
ax.plot(np.linspace(-180,180,200),[30]*200,':',transform=ccrs.PlateCarree(),color='k',linewidth=0.4)
ax.plot(np.linspace(-180,180,200),[60]*200,':',transform=ccrs.PlateCarree(),color='k',linewidth=0.4)
return ax