Python中怎么画散点密度图,有多少方法
Admin 2022-08-11 群英技术资讯 601 次浏览
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()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
接触过Java的朋友对于类应该不陌生,我们在学习python过程中,也会学习到类,那么python的类如何理解?很多新手学习python时,可能对此比较困惑。对此,下面小编就给大家介绍一下python的类以及使用。感兴趣的朋友就继续往下看吧。
在C/C++语言中,struct被称为结构体。而在Python中,struct是一个专门的库,用于处理字节串与原生Python数据结构类型之间的转换。本篇,将详细介绍二进制数据结构struct的使用方式。
这篇文章主要介绍了Python中的pprint打印模块,pprint()采用分行打印输出,下文关于其相关介绍,需要的小伙伴可以参考一下
要进行时间转换,在python里面是非常简单的,这里会使用到 time 模块里的 strptime() 和 strftime()。下面我们用实例来演示一下:strp
在本篇文章里小编给大家整理的是一篇关于pygame实现方块动画实例讲解内容,以后需要的朋友们可以学习参考下。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008