线性回归可以基于SPSS或R语言以及MATLAB实现,不过SPSS散点图不方便格式控制,本人对于R语言及MATLAB不太了解,于是通过查询相关包,最终使用python实现了一元线性回归分析。
由于结果图用于论文,故图整体配色为黑色。
若代码有不足之处,请大家指导。
线性回归原理参考
# coding:utf8
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from scipy.stats import linregress
from sklearn.metrics import mean_squared_error # 计算均方差
plt.rcParams['font.family'] = ['SimHei'] # 设置字体
plt.rcParams['axes.unicode_minus'] = False
def fun_line(x, a, b):
return a*x + b
# 获取数据
data = pd.read_excel(ur"../yanzheng_avg_years.xls").astype(float)
x = data['modis_avg'].values.ravel() # 将多维数组转换成一维数组
y = data['casa_avg'].values.ravel()
N = len(data['casa_avg'])
# 获取斜率 截距 p值 R^2
slope, intercept, r_value, p_value, std_err = linregress(x, y)
# print(p_value) # 此处判断p值
slope = round(slope, 3) # 斜率
intercept = round(intercept, 3) # 截距
r2 = round(r_value**2, 3) # R^2
rmse = round(np.sqrt(mean_squared_error(x, y)), 3) # 均方根误差
x2 = np.linspace(-10, max(data['casa_avg']))
y2 = x2
y3 = fun_line(x, slope, intercept)
fig, ax = plt.subplots(figsize=(6, 5), dpi=200, sharey=True, facecolor='w')
ax.scatter(x, y, c='k', s=5, edgecolor='black', marker='s') # k:black s:square 正方形
ax.plot(x2, y2, c='k', linewidth=1.5, linestyle='--', zorder=2) # zorder 控制绘图顺序
ax.plot(x, y3, c='k', linewidth=1.5, linestyle='-', zorder=2)
fontdict1 = {'size': 14, 'color': 'k'}
ax.set_xlabel(ur'MOD17A3 NPP(gC${m^{-2}a^{-1}}$)', fontdict=fontdict1)
ax.set_ylabel(ur'ANPP模拟值(gC${m^{-2}a^{-1}}$)', fontdict=fontdict1)
# 将刻度范围
ax.set_xlim((0, int(max(x)) / 100 * 100 + 100))
ax.set_ylim((0, int(max(y)) / 100 * 100 + 100))
# 修改刻度线指向
ax.tick_params(left=True, direction='in', labelsize=12)
# 修改坐标轴
for spine in ['top', 'right']:
ax.spines[spine].set_visible(False)
# 设置文本注释
ax.text(50, 640, r'$R^2$ = %.3f' % r2, fontdict=fontdict1)
ax.text(50, 600, r'y = %.3fx%.3f' % (slope, intercept), fontdict=fontdict1)
ax.text(50, 550, r'P < 0.01', fontdict=fontdict1)
ax.text(650, 630, 'y = x', fontdict=fontdict1)
plt.savefig(ur"../IMG/回归分析.png")
plt.show()