Python绘制卫星降雨校正散点密度图

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
from sklearn.metrics import mean_squared_error
from matplotlib.pyplot import MultipleLocator
from statistics import mean
from matplotlib import rcParams
config = {"font.family":'Times New Roman',"font.size": 16,"mathtext.fontset":'stix'}
rcParams.update(config)
# 读取数据
filename=r'./data613.xlsx'
df2=pd.read_excel(filename)#读取文件
p1=df2['p1']
p2=df2['p2']
def get_regression_line(real,pred,data_range=(0,120)):
    # 拟合
    def slope(xs,ys):
        m = (((mean(xs) * mean(ys)) - mean(xs * ys)) / ((mean(xs) * mean(xs)) - mean(xs * xs)))
        b = mean(ys) - m * mean(xs)
        return m, b
    k, b = slope(real,pred)
    regression_line = []
    for a in range(0,120):
        regression_line.append((k*a)+b)
    return regression_line
# 开始绘图
fig,ax=plt.subplots(figsize=(12,9),dpi=600)
# Calculate the point density
xy = np.vstack([p1,p2])
z = gaussian_kde(xy)(xy)
scatter = ax.scatter(p1,p2,marker='o',c=z*100,edgecolors=None,s=5,label='LST',cmap='gist_rainbow')
cbar=plt.colorbar(scatter,shrink=1,orientation='vertical',extend='both',pad=0.015,aspect=30)
regression_line = get_regression_line(p1,p2,data_range=(0,120))
ax.plot(regression_line, 'r-', lw=1.5) # 绘制回归线
x, y = p1,p2
BIAS = mean(x - y)
MSE = mean_squared_error(x, y)
RMSE = np.power(MSE, 0.5)
R = np.corrcoef(x, y)[0, 1]
ax.text(1, 116, '$N=%.f$' % len(y), family = 'Times New Roman')
ax.text(15, 116, '$R=%.2f$' % R, family = 'Times New Roman')
ax.text(1, 112, '$BIAS=%.2f$' % BIAS, family = 'Times New Roman')
ax.text(1, 108, '$RMSE=%.2f$' % RMSE, family = 'Times New Roman')
plt.plot([0,120],[0,120],'k--',lw=1.5) # 绘制1:1线
# 设置边框粗细
ax.spines['bottom'].set_linewidth(2.5); # 设置底部坐标轴的粗细
ax.spines['top'].set_linewidth(2.5);    # 设置底部坐标轴的粗细
ax.spines['left'].set_linewidth(2.5);   # 设置底部坐标轴的粗细
ax.spines['right'].set_linewidth(2.5);  # 设置底部坐标轴的粗细
# 设置刻度线长短粗细
ax2=plt.gca()
ax.tick_params(which='major',width=2.5,length=5)
# ax为两条坐标轴的实例
x_major_locator=MultipleLocator(100)
# 把x轴的刻度间隔设置为1,并存在变量里
y_major_locator=MultipleLocator(100)
# 把y轴的刻度间隔设置为10,并存在变量里
ax2.xaxis.set_major_locator(x_major_locator)
# 把x轴的主刻度设置为1的倍数
ax2.yaxis.set_major_locator(y_major_locator)
font3={'family':'SimHei','size':16,'color':'k'}
plt.title('降雨相关性',fontdict=font3)
plt.xlabel('实测降雨(mm)',fontdict=font3)
plt.ylabel('卫星降雨(mm)',fontdict=font3)
plt.xticks()
plt.yticks()
plt.xlim(xmin=0, xmax=120)
plt.xticks(np.arange(0, 121, 20))
plt.ylim(ymin=0, ymax=120)
plt.yticks(np.arange(0, 121, 20))
plt.savefig('./plot281.png',dpi=500,bbox_inches='tight',pad_inches=0)
plt.show()

请确保将代码中的'卫星降雨校正数据.csv'替换为实际的数据文件路径和文件名,并根据需要进行其他自定义设置(如调整颜色映射、修改图形大小等)。

这段代码将使用seabornkdeplot函数创建散点密度图,并使用matplotlib进行图形显示。散点密度图会根据散点的分布密度进行着色,呈现出不同区域的密度情况。

猜你喜欢

转载自blog.csdn.net/2301_77925375/article/details/131389630
今日推荐