最初接触python画图的时候,这个散点密度图也是经历了一波三折才做出来。
直接给一个画图的代码,方便有需要的同学做个参考
from statistics import mean
import matplotlib
import matplotlib.pyplot as plt
plt.ion()
from collections import defaultdict
from pandas import set_option
from operator import itemgetter
from sklearn.metrics import explained_variance_score,r2_score,median_absolute_error,mean_squared_error,mean_absolute_error
from matplotlib.colors import LogNorm
from scipy.stats import gaussian_kde
from scipy import stats
def scatter_out_1(x, y, title_name, savename): ## 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)
plt.plot([0, 1], [0, 1], 'black', lw=0.8) # 画的1:1线,线的颜色为black,线宽为0.8
plt.scatter(x, y, c=z, s=7, edgecolor='', cmap='jet')
# edgecolor: 设置轮廓颜色的,none是无色
# 将c设置成一个y值列表并使用参数 cmap.cm.XX来使用某个颜色渐变(映射)
# https://matplotlib.org/examples/color/colormaps_reference.html 从这个网页可以获得colorbard的名称
# c=是设置数据点颜色的,在2.0的matplotlib中,edgecolor默认为'none'
# c=还可以使用RGB颜色,例如 c = (0,0,0.8) 分别表示 红绿蓝,值越接近0,颜色越深.越接近1,颜色越浅
plt.plot(x, regression_line, 'red', lw=0.8) # 预测与实测数据之间的回归线
plt.axis([0, 1, 0, 1]) # 设置线的范围
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(0.03, 0.95, '$N=%.f$' % len(y), family = 'Times New Roman') # text的位置需要根据x,y的大小范围进行调整。
plt.text(0.03, 0.90, '$R^2=%.3f$' % R2, family = 'Times New Roman')
plt.text(0.03, 0.85, '$BIAS=%.4f$' % BIAS, family = 'Times New Roman')
plt.text(0.03, 0.80, '$RMSE=%.3f$' % RMSE, family = 'Times New Roman')
plt.xlim(-0.1, 1.1) # 设置x坐标轴的显示范围
plt.ylim(-0.1, 1.1) # 设置y坐标轴的显示范围
# plt.title(title_name,family = 'Times New Roman')
# plt.colorbar()
plt.savefig(savename +'.png', bbox_inches='tight', dpi=1000, clear=True)
# 第一个实参表示用什么样的文件名保存图片,
# bbox_inches='tight'指定将图周围多余的空白剪裁掉
# dpi表示图像的分辨率
# clear: bool,可选;默认值:False, 如果为True并且该图已经存在,那么它将被清除。
plt.show()
plt.pause(5) # 暂停3秒钟
plt.close() # 关闭当前显示的图像
return
最后显示效果图。