Python绘制散点密度图的三种方式详解
作者:气象水文科研猫 时间:2021-12-07 00:21:04
方式一
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import gaussian_kde
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib import rcParams
config = {"font.family":'Times New Roman',"font.size": 16,"mathtext.fontset":'stix'}
rcParams.update(config)
# 读取数据
import pandas as pd
filename=r'F:/Rpython/lp37/testdata.xlsx'
df2=pd.read_excel(filename)#读取文件
x=df2['data1'].values
y=df2['data2'].values
xy = np.vstack([x,y])
z = gaussian_kde(xy)(xy)
idx = z.argsort()
x, y, z = x[idx], y[idx], z[idx]
fig,ax=plt.subplots(figsize=(12,9),dpi=100)
scatter=ax.scatter(x,y,marker='o',c=z,edgecolors='',s=15,label='LST',cmap='Spectral_r')
cbar=plt.colorbar(scatter,shrink=1,orientation='vertical',extend='both',pad=0.015,aspect=30,label='frequency') #orientation='horizontal'
font3={'family':'SimHei','size':16,'color':'k'}
plt.ylabel("估计值",fontdict=font3)
plt.xlabel("预测值",fontdict=font3)
plt.savefig('F:/Rpython/lp37/plot70.png',dpi=800,bbox_inches='tight',pad_inches=0)
plt.show()
方式二
from statistics import mean
import matplotlib.pyplot as plt
from sklearn.metrics import explained_variance_score,r2_score,median_absolute_error,mean_squared_error,mean_absolute_error
from scipy import stats
import numpy as np
from matplotlib import rcParams
config = {"font.family":'Times New Roman',"font.size": 16,"mathtext.fontset":'stix'}
rcParams.update(config)
def scatter_out_1(x,y): ## x,y为两个需要做对比分析的两个量。
# ==========计算评价指标==========
BIAS = mean(x - y)
MSE = mean_squared_error(x, y)
RMSE = np.power(MSE, 0.5)
R2 = r2_score(x, y)
MAE = mean_absolute_error(x, y)
EV = explained_variance_score(x, y)
print('==========算法评价指标==========')
print('BIAS:', '%.3f' % (BIAS))
print('Explained Variance(EV):', '%.3f' % (EV))
print('Mean Absolute Error(MAE):', '%.3f' % (MAE))
print('Mean squared error(MSE):', '%.3f' % (MSE))
print('Root Mean Squard Error(RMSE):', '%.3f' % (RMSE))
print('R_squared:', '%.3f' % (R2))
# ===========Calculate the point density==========
xy = np.vstack([x, y])
z = stats.gaussian_kde(xy)(xy)
# ===========Sort the points by density, so that the densest points are plotted last===========
idx = z.argsort()
x, y, z = x[idx], y[idx], z[idx]
def best_fit_slope_and_intercept(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
m, b = best_fit_slope_and_intercept(x, y)
regression_line = []
for a in x:
regression_line.append((m * a) + b)
fig,ax=plt.subplots(figsize=(12,9),dpi=600)
scatter=ax.scatter(x,y,marker='o',c=z*100,edgecolors='',s=15,label='LST',cmap='Spectral_r')
cbar=plt.colorbar(scatter,shrink=1,orientation='vertical',extend='both',pad=0.015,aspect=30,label='frequency')
plt.plot([0,25],[0,25],'black',lw=1.5) # 画的1:1线,线的颜色为black,线宽为0.8
plt.plot(x,regression_line,'red',lw=1.5) # 预测与实测数据之间的回归线
plt.axis([0,25,0,25]) # 设置线的范围
plt.xlabel('OBS',family = 'Times New Roman')
plt.ylabel('PRE',family = 'Times New Roman')
plt.xticks(fontproperties='Times New Roman')
plt.yticks(fontproperties='Times New Roman')
plt.text(1,24, '$N=%.f$' % len(y), family = 'Times New Roman') # text的位置需要根据x,y的大小范围进行调整。
plt.text(1,23, '$R^2=%.3f$' % R2, family = 'Times New Roman')
plt.text(1,22, '$BIAS=%.4f$' % BIAS, family = 'Times New Roman')
plt.text(1,21, '$RMSE=%.3f$' % RMSE, family = 'Times New Roman')
plt.xlim(0,25) # 设置x坐标轴的显示范围
plt.ylim(0,25) # 设置y坐标轴的显示范围
plt.savefig('F:/Rpython/lp37/plot71.png',dpi=800,bbox_inches='tight',pad_inches=0)
plt.show()
方式三
import pandas as pd
import numpy as np
from scipy import optimize
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import Normalize
from scipy.stats import gaussian_kde
from matplotlib import rcParams
config={"font.family":'Times New Roman',"font.size":16,"mathtext.fontset":'stix'}
rcParams.update(config)
# 读取数据
filename=r'F:/Rpython/lp37/testdata.xlsx'
df2=pd.read_excel(filename)#读取文件
x=df2['data1'].values.ravel()
y=df2['data2'].values.ravel()
N = len(df2['data1'])
#绘制拟合线
x2 = np.linspace(-10,30)
y2 = x2
def f_1(x,A,B):
return A*x + B
A1,B1 = optimize.curve_fit(f_1,x,y)[0]
y3 = A1*x + B1
# Calculate the point density
xy = np.vstack([x,y])
z = gaussian_kde(xy)(xy)
norm = Normalize(vmin = np.min(z), vmax = np.max(z))
#开始绘图
fig,ax=plt.subplots(figsize=(12,9),dpi=600)
scatter=ax.scatter(x,y,marker='o',c=z*100,edgecolors='',s=15,label='LST',cmap='Spectral_r')
cbar=plt.colorbar(scatter,shrink=1,orientation='vertical',extend='both',pad=0.015,aspect=30,label='frequency')
cbar.ax.locator_params(nbins=8)
cbar.ax.set_yticklabels([0.005,0.010,0.015,0.020,0.025,0.030,0.035])#0,0.005,0.010,0.015,0.020,0.025,0.030,0.035
ax.plot(x2,y2,color='k',linewidth=1.5,linestyle='--')
ax.plot(x,y3,color='r',linewidth=2,linestyle='-')
fontdict1 = {"size":16,"color":"k",'family':'Times New Roman'}
ax.set_xlabel("PRE",fontdict=fontdict1)
ax.set_ylabel("OBS",fontdict=fontdict1)
# ax.grid(True)
ax.set_xlim((0,25))
ax.set_ylim((0,25))
ax.set_xticks(np.arange(0,25.1,step=5))
ax.set_yticks(np.arange(0,25.1,step=5))
plt.savefig('F:/Rpython/lp37/plot72.png',dpi=800,bbox_inches='tight',pad_inches=0)
plt.show()
来源:https://mp.weixin.qq.com/s/b8N_3Bj05oveYQqWN6VhcQ
标签:Python,散点,密度图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
经典SQL语句大全
2009-08-26 16:44:00
浅谈python numpy中nonzero()的用法
2021-09-23 00:06:01
python实现简单socket程序在两台电脑之间传输消息的方法
2021-04-25 21:14:26
Python中字符串格式化str.format的详细介绍
2021-08-19 01:28:37
思考如何提高交互设计水平?
2009-12-08 12:18:00
python 实时遍历日志文件
2021-03-30 09:26:20
python一键去抖音视频水印工具
2023-09-26 22:58:21
![](https://img.aspxhome.com/file/2023/3/63173_0s.jpg)
python常用排序算法的实现代码
2022-08-21 08:50:00
Python如何加载模型并查看网络
2021-11-01 15:53:22
![](https://img.aspxhome.com/file/2023/3/115123_0s.png)
django+tornado实现实时查看远程日志的方法
2021-09-16 07:16:45
浅谈ROC曲线的最佳阈值如何选取
2021-10-07 07:49:29
![](https://img.aspxhome.com/file/2023/9/103009_0s.jpg)
.NET中获取程序根目录的常用方法介绍
2023-07-09 19:52:41
使用Python处理Excel表格的简单方法
2023-12-07 08:05:04
![](https://img.aspxhome.com/file/2023/1/99021_0s.jpg)
Python的GUI编程之Pack、Place、Grid的区别说明
2022-10-19 05:53:57
![](https://img.aspxhome.com/file/2023/6/95316_0s.jpg)
利用python3如何给数据添加高斯噪声
2023-06-11 20:52:17
![](https://img.aspxhome.com/file/2023/8/59298_0s.png)
Python+Selenium定位不到元素常见原因及解决办法(报:NoSuchElementException)
2022-06-05 22:03:00
Python实现计算图像RGB均值方式
2024-01-01 20:50:31
![](https://img.aspxhome.com/file/2023/3/99513_0s.jpg)
谈谈从phpinfo中能获取哪些值得注意的信息
2023-11-23 23:52:17
![](https://img.aspxhome.com/file/2023/0/111040_0s.png)
Python如何读取、写入JSON数据
2021-10-26 18:28:19
PHP入门基础之注释的写法
2023-10-20 07:26:17