1 import numpy as np 2 import math 3 import pandas as pd 4 import matplotlib.pyplot as plt 5 import pylab 6 7 df = pd.read_excel(r'XXXX') 8 9 plt.rcParams['font.sans-serif'] = ['SimHei'] #解决中文显示 10 plt.rcParams['axes.unicode_minus'] = False #解决符号无法显示 11 12 x = df['TDW'] 13 y = df['Grain'] 14 # 计算相关度 15 def computeCorrelation(x,y): 16 xBar = np.mean(x) 17 yBar = np.mean(y) 18 SSR = 0.0 19 varX = 0.0 20 varY = 0.0 21 for i in range(0,len(x)): 22 diffXXbar = x[i] - xBar 23 difYYbar = y[i] - yBar 24 SSR += (diffXXbar * difYYbar) 25 varX += diffXXbar**2 26 varY += difYYbar**2 27 SST = math.sqrt(varX * varY) 28 return SSR/SST 29 30 # 计算R平方 31 def polyfit(x,y,degree): 32 results = {} 33 coeffs = np.polyfit(x,y,degree) 34 results['polynomial'] = coeffs.tolist() 35 p = np.poly1d(coeffs) 36 yhat = p(x) 37 ybar = np.sum(y)/len(y) 38 ssreg = np.sum((yhat - ybar)**2) 39 sstot = np.sum((y - ybar)**2) 40 results['determination'] = ssreg/sstot 41 return results 42 43 result = computeCorrelation(x,y) 44 r = result 45 r_2 = result**2 46 print("r:",r) 47 print("r^2:",r*r) 48 49 50 51 pylab.plot(x,y,'o') 52 53 z = np.polyfit(x,y,1) 54 p = np.poly1d(z) 55 pylab.plot(x,p(x),'r-') 56 57 # the line equation: 58 gs = ( 'y = %.4fx + %.4f '%(z[0],z[1])) 59 print(gs) 60 61 #添加数据标签 还可以优化 62 plt.text(20,9.9,gs,fontsize = 12) 63 plt.text(21,9.5,'$r^{2}=0.343$*',fontsize = 12) 64 plt.title('干物质积累与产量的相关性分析',fontsize=20) 65 plt.xlabel('TDW(t·$ha^{-1}$)',fontsize=12) 66 plt.ylabel('Grain($t·ha^{-1}$)',fontsize=12) 67 plt.show() 68 # plt.savefig('zaodao01.jpg',dpi=600)
最终的呈现效果:

优化版:
1 import numpy as np 2 import math 3 import pandas as pd 4 import matplotlib.pyplot as plt 5 import pylab 6 7 plt.rcParams['font.sans-serif'] = 'SimHei' #解决中文显示 8 plt.rcParams['axes.unicode_minus'] = False #解决符号无法显示 9 10 x = df['You'] 11 y = df['日产量'] 12 13 xq = x[0:12] 14 xw = x[12:20] 15 xe = x[20:28] 16 yq =y[0:12] 17 yw = y[12:20] 18 ye = y[20:28] 19 20 #构建总的点 label=''可使其不在图例中显示 21 pylab.plot(x,y,'o',label = '') 22 23 # 构建三个类型的点 24 pylab.plot(xq,yq,'o', label = '高产') 25 pylab.plot(xw,yw,'o',label = '中产' ) 26 pylab.plot(xe,ye,'o', label = '低产') 27 28 plt.legend(loc='upper left') 29 30 # 计算相关度 31 def computeCorrelation(x,y): 32 xBar = np.mean(x) 33 yBar = np.mean(y) 34 SSR = 0.0 35 varX = 0.0 36 varY = 0.0 37 for i in range(0,len(x)): 38 diffXXbar = x[i] - xBar 39 difYYbar = y[i] - yBar 40 SSR += (diffXXbar * difYYbar) 41 varX += diffXXbar**2 42 varY += difYYbar**2 43 SST = math.sqrt(varX * varY) 44 return SSR/SST 45 46 # 计算R平方 47 def polyfit(x,y,degree): 48 results = {} 49 coeffs = np.polyfit(x,y,degree) 50 results['polynomial'] = coeffs.tolist() 51 p = np.poly1d(coeffs) 52 yhat = p(x) 53 ybar = np.sum(y)/len(y) 54 ssreg = np.sum((yhat - ybar)**2) 55 sstot = np.sum((y - ybar)**2) 56 results['determination'] = ssreg/sstot 57 return results 58 59 result = computeCorrelation(x,y) 60 r = result 61 r_2 = result**2 62 print("r:",r) 63 print("r^2:",r*r) 64 65 66 z = np.polyfit(x,y,1) 67 p = np.poly1d(z) 68 pylab.plot(x,p(x),'r-') 69 70 # the line equation: 71 gs = ( 'y = %.4fx + %.4f '%(z[0],z[1])) 72 print(gs) 73 74 #添加数据标签 75 plt.text(370,90,gs,fontsize = 12) 76 plt.text(420,86,'$r^{2}=$%.3f*'%r_2,fontsize = 12) 77 plt.title('有效穗数与日产量的相关性分析',fontsize=20) 78 plt.xlabel('有效穗数(10$^{3}$)',fontsize=12) 79 plt.ylabel('日产量(t·ha$^{-1}$)',fontsize=12) 80 #plt.savefig('Rzx03.jpg',dpi=600)

本人的文档都是自我记录,以便日后查看。
浙公网安备 33010602011771号