常用代码
一、常用引入库
from collections import Counter
import matplotlib.pyplot as plt
from math import atan,radians,tan,cos,sin,acos
import numpy as np
import seaborn as sns
plt.rcParams['figure.dpi'] = 300
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # Microsoft YaHei
plt.rcParams['axes.unicode_minus'] = False # 负数
二、分布图绘制
def draw_distrubution(date, drawdata, xlabel, drawtype):
if drawtype == 'hist':
plt.figure(figsize=(10, 6))
# data = [np.log10(a) for a in drawdata] # 对数直方图,以10为底,np.log(a)默认以e为底
data = drawdata
min_val, max_val = np.min(data), np.max(data)
bins1 = np.linspace(min_val, max_val, num=20)
sns.histplot(data, bins=bins1, color="red", alpha=0.7, stat="density") # stat='count'默认是频数 , stat="density"画概率密度
plt.xlabel(xlabel, fontsize=14)
plt.ylabel(drawtype, fontsize=14)
plt.title(date, fontsize=16)
plt.show()
else:
draw_y = []
if drawtype == 'PDF':
count = Counter(drawdata) # 计算出现频次
x = sorted(count.keys())
y = [count[key] for key in x]
draw_y = y
else:
count = Counter(drawdata) # 计算出现频次
x = sorted(count.keys())
y = [count[key] for key in x]
y_cu = [] # 累积
y_ccu = [] # 互补累积
cu_pro = 0
ccu_pro = 1
total = sum(y)
for i in range(len(y)):
cu_pro += y[i]/total
y_cu.append(cu_pro)
y_ccu.append(ccu_pro)
ccu_pro -= y[i]/total
if drawtype == 'CCDF':
draw_y = y_ccu
elif drawtype == 'CDF':
draw_y = y_cu
plt.figure(figsize=(10, 6))
plt.scatter(x, draw_y, c='red')
plt.title(date, fontsize=16)
plt.xscale('log', base=10) # 默认底数为10,也可以设置base=np.e以e为底数
plt.yscale('log', base=10)
plt.xlabel(xlabel, fontsize=14)
plt.ylabel(drawtype, fontsize=14)
# plt.ylim(bottom=1e-6) # 设置y轴下限
plt.show()
三、计算
1、计算距离
def calcDistance(Lat_A, Lng_A, Lat_B, Lng_B):
"""
input Lat_A 纬度A
input Lng_A 经度A
input Lat_B 纬度B
input Lng_B 经度B
output distance m
"""
Lat_A=round(Lat_A,6)
Lat_B=round(Lat_B,6)
Lng_A=round(Lng_A,6)
Lng_B=round(Lng_B,6)
if Lat_A==Lat_B and Lng_A==Lng_B:
return 0
#
ra = 6378.140 # 赤道半径 (km)
rb = 6356.755 # 极半径 (km)
flatten = (ra - rb) / ra # 地球扁率
rad_lat_A = radians(Lat_A)
rad_lng_A = radians(Lng_A)
rad_lat_B = radians(Lat_B)
rad_lng_B = radians(Lng_B)
pA = atan(rb / ra * tan(rad_lat_A))
pB = atan(rb / ra * tan(rad_lat_B))
try:
xx = acos(sin(pA) * sin(pB) + cos(pA) * cos(pB) * cos(rad_lng_A - rad_lng_B))
c1 = (sin(xx) - xx) * (sin(pA) + sin(pB)) ** 2 / cos(xx / 2) ** 2
c2 = (sin(xx) + xx) * (sin(pA) - sin(pB)) ** 2 / sin(xx / 2) ** 2
dr = flatten / 8 * (c1 - c2)
distance = ra * (xx + dr)
except:
distance = 0
return distance * 1000