Python draws satellite rainfall correction scatter density map

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) # draw regression line
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) # Draw 1:1 line
# Set the border thickness
ax.spines['bottom'].set_linewidth(2.5); # Set the thickness of the bottom axis
ax.spines['top'].set_linewidth(2.5); # Set the thickness of the bottom axis
ax.spines['left'].set_linewidth(2.5); # Set the thickness of the bottom axis
ax.spines['right'].set_linewidth(2.5); # Set the thickness of the bottom axis
# Set the length and thickness of the scale line
ax2= plt.gca()
ax.tick_params(which='major',width=2.5,length=5)
# ax is an instance of two coordinate axes
x_major_locator=MultipleLocator(100)
# Set the scale interval of the x-axis to 1, and There is a variable
y_major_locator=MultipleLocator(100)
#Set the scale interval of the y-axis to 10, and there is a variable
ax2.xaxis.set_major_locator(x_major_locator)
#Set the main scale of the x-axis to a multiple of 1
ax2.yaxis.set_major_locator( y_major_locator)
font3={'family':'SimHei','size':16,'color':'k'} plt.title
('rainfall correlation',fontdict=font3)
plt.xlabel('measured rainfall (mm )', fontdict=font3)
plt.ylabel('Satellite rainfall (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()

Make sure to replace 'Satellite Rain Correction Data.csv' in the code with the actual data file path and filename, and make other customizations as needed (such as adjusting the colormap, modifying the graph size, etc.).

This code will use seabornthe kdeplotfunction to create a scatter density plot and matplotlibdisplay it graphically using The scatter density map will be colored according to the distribution density of the scatter points, showing the density of different regions.

Guess you like

Origin blog.csdn.net/2301_77925375/article/details/131389630