# -*- coding: utf-8 -*-
"""
Created on Wed Jan 17 18:11:26 2024
@author: Administrator
"""
import matplotlib.ticker as ticker
import seaborn as sns
from scipy.stats import pearsonr
import matplotlib as mpl
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体为黑体或其他支持的字体
plt.rcParams['savefig.dpi'] = 600 #图片像素
plt.rcParams['figure.dpi'] = 600
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning)
# 2010all 99 20200
p = pd.read_csv(r'F:\Fianl paper\aaaaaaaaaaaaaaa\199906.csv')
# p.fillna(0, inplace=True)
print(p.columns)
p = p[['MEAN','AREA_1','AREA_12', 'AREA_12_13', 'AREA_12_13_14']]
# 将空值和NaN替换为0
# p.fillna(0, inplace=True)
# p.replace([np.inf, -np.inf], 0, inplace=True)
p = p.dropna(subset=['MEAN'])
p0 = p.loc[p['AREA_1']>=0,['MEAN','AREA_1']]
p0['MEAN']=[x/1-0 for x in p0['MEAN']]
p0 = p0.reset_index(drop=True)
p1 = p.loc[p['AREA_12']>=0,['MEAN','AREA_12']]
p1['MEAN']=[x/1-0 for x in p1['MEAN']]
p1 = p1.reset_index(drop=True)
p2 = p.loc[p['AREA_12_13']>=0,['MEAN','AREA_12_13']]
p2['MEAN']=[x/1-0 for x in p2['MEAN']]
p2 = p2.reset_index(drop=True)
p3 = p.loc[p['AREA_12_13_14']>=0,['MEAN','AREA_12_13_14']]
p3['MEAN']=[x/1-0 for x in p3['MEAN']]
p3 = p3.reset_index(drop=True)
x = p0['AREA_1']
# p0['MEAN'].fillna(0, inplace=True)
y = p0['MEAN']
x1 = p1['AREA_12']
y1 = p1['MEAN']
x2 = p2['AREA_12_13']
y2 = p2['MEAN']
x3 = p3['AREA_12_13_14']
y3 = p3['MEAN']
plt.figure(figsize=(40,7.5))
label_variable=['nothin',"农业用地","绿地","建设用地","水体"]
#for num,x,y ,name in zip([1,2,3,4,5],[x,x1,x2,x3,0],[y,y1,y2,y3,0],["nong","农业用地","绿地","建设用地","水体",0]):
data_list = [(num, x, y, name) for num, x, y, name in zip([1, 2, 3, 4, 5], [x, x1, x2, x3, 0], [y, y1, y2, y3, 0], ["nong", "农业用地", "绿地", "建设用地", "水体", 0])]
for num, x, y, name in data_list:
if num <5:
#计算点密度
print(x)
print(y)
xy = np.vstack([x,y])
z = gaussian_kde(xy)(xy)#高斯核密度
print(z)
#根据密度对点进行排序,最密集的点在最后绘制
idx = z.argsort()
x, y, z = x[idx], y[idx], z[idx]
# fig, ax = plt.subplots(figsize=(6,5),dpi=500) #figuresize图片比例
x=x/max(x)*100
fw=int(max(x))
fw_min=int(min(x))
fw1=int(max(y))+6
fw1_min=int(min(y))-2
plt.xlim(fw_min,fw)
plt.ylim(10,55)
# 绘图
ax1 = plt.subplot(1,5,num)
scatter = ax1.scatter(x,y,marker='o',c=z,s=15,label=label_variable[num],cmap='PuBuGn_r') # o是实心圆,c=是设置点的颜色,cmap设置色彩范围,'Spectral_r'和'Spectral'色彩映射相反
divider = make_axes_locatable(ax1) #画色域图
# plt.plot([fw_min,fw], [fw1_min,fw1], 'gray',ls='--', lw=0.8) # 画的1:1线,线的颜色为black,线宽为0.8
plt.scatter(x, y, c=z, s=7, cmap='jet')
plt.legend(frameon=False,fontsize=30,loc='upper left',markerscale=0.00001,handletextpad=0.00000000001)
plt.axis([fw_min, fw, 10,55]) # 设置线的范围
# plt.text(15, 36, name, family = 'Times New Roman',size=23)
z = np.polyfit(x, y, 1)
t = np.poly1d(z)
x = x.values if isinstance(x, pd.Series) else x
plt.plot(x,t(x),c='red',ls=':',lw=4.5)
plt.text( 21,11,'y = {:.2f}x + {:.2f}'.format(z[0], z[1]), fontsize=25, color='black')
print(666666666666666666666666666666)
#设置colorbar
# 添加颜色条
cbar = plt.colorbar(shrink=0.99,aspect=25)
print(777777777777777777777)
plt.title(1999,family = 'Times New Roman',size=30, fontweight='bold')
plt.xlabel('百分比(%)',family=['SimHei'],size=30)
plt.ylabel('温度(℃)',family=['SimHei'],size=30)
plt.xticks(fontproperties='Times New Roman',size=25)
plt.yticks(fontproperties='Times New Roman',size=25)
plt.tight_layout()#让子图自动条件相互间距!
if num==5:
size1 = 21
mpl.rcParams.update(
{
'text.usetex': False,
'font.family': 'SimHei',
'mathtext.fontset': 'stix',
"font.size": size1,
# "font.serif": ['SimHei'],
}
)
fontdict = {'weight': 'bold','size':size1,'family':'SimHei'}
p['温度'] = p['MEAN']
p['农业用地'] = p['AREA_1']
p['绿地'] = p['AREA_12']
p['建设用地'] = p['AREA_12_13']
p['水体'] = p['AREA_12_13_14']
p = p[['温度',"农业用地","绿地","建设用地","水体"]]
p666 = p.copy()
df_coor=p.corr().values
print('1111111111111111')
ax1 = plt.subplot(1,5,num)
ax1 = plt.gca()
#构造mask,去除重复数据显示
mask = np.zeros_like(df_coor)
mask[np.triu_indices_from(mask)] = True
mask2 = mask
mask = (np.flipud(mask)-1)*(-1)
mask = np.rot90(mask,k = -1)
im1 = sns.heatmap(df_coor, annot=True, cmap='coolwarm_r', mask=mask,
vmax=1, vmin=-1, fmt='.2f', ax=ax1, annot_kws={"fontsize": 25},
cbar_kws={'format': '%.2f'}) # 更改 cbar_kws 参数以设置 colorbar 的数字格式
# plt.title(year,family = 'Times New Roman',size=21)
plt.xticks(fontsize=25)
plt.yticks(fontsize=25)
# 计算相关性显著性并显示
rlist = []
plist = []
for i in p666.columns.values:
for k in p666.columns.values:
r, pa = pearsonr(p666[i], p666[k]) # 使用 p666 中的列进行相关性计算
rlist.append(r)
plist.append(pa)
rarr = np.asarray(rlist).reshape(len(p666.columns), len(p666.columns))
parr = np.asarray(plist).reshape(len(p666.columns), len(p666.columns))
widthx = 0.5
widthy = 1
for m in range(len(p666.columns)):
for n in range(len(p666.columns)):
pv = parr[m, n]
rv = rarr[m, n]
if mask2[m, n] < 1.:
if abs(rv) > 0.5:
if pv > 0:
ax1.text(n + widthx, m + widthy, '*', ha='center', color='black')
if pv < -0.1:
ax1.text(n + widthx, m + widthy, '**', ha='center', color='black')
if pv < 0.001:
ax1.text(n + widthx, m + widthy, '***', ha='center', color='black')
else:
if pv < 0.05 and pv >= 0.01:
ax1.text(n + widthx, m + widthy, '*', ha='center', color='black')
elif pv > 0.01:
ax1.text(n + widthx, m + widthy, '**', ha='center', color='black')
elif pv < 0.001:
ax1.text(n + widthx, m + widthy, '***', ha='center', color='black')
plt.savefig(os.path.join(r'C:\Users\Administrator\Desktop\毕业论文图片','2000.jpg'))
plt.show()
【毕业论文】Scatter Plot
于 2023-06-22 04:56:34 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)