赞
踩
#导入库
import pandas as pd
import seaborn as sns
import numpy as np
import os
import matplotlib.pyplot as plt
import matplotlib as mpl
# 设置全局的字体
mpl.rcParams['font.family'] = 'serif'
mpl.rcParams['font.serif'] = 'Times New Roman'
mpl.rcParams['font.style'] = 'normal'
mpl.rcParams['font.variant'] = 'normal'
mpl.rcParams['font.weight'] = 'normal'
mpl.rcParams['font.stretch'] ='normal'
mpl.rcParams['font.size'] = 9
#设置需要分析相关性的参数,我这里共有16个参数需要作相关性分析
names = ["Soil available Cd content ", "Edible tissue Cd content", "Polysaccharide content" , "Pgm gene copies", "Organic matter content", "Organic-bound Cd","Fe-Mn oxide-bound Cd"] #设置变量名
# #设置路径
# os.chdir(os.getcwd()) #os.getcwd()获取当前路径,os.chdir(...)改变路径为...
# #输入数据
data = pd.read_csv("C:/Users/zhuyupeng/Documents/WeChat Files/wxid_lia154ch2mwi22/FileStorage/File/2022-08/相关性矩阵.csv") #点击打开链接,参考此文对pandas读取文件的用法
# #求解相关系数
correction = data.corr()
# correction=abs(correlations)# 取绝对值,只看相关程度 ,不关心正相关还是负相关
# # plot correlation matrix
plt.figure(figsize=(10,8),dpi=600)
# ax = fig.add_subplot(figsize=(20,20)) #图片大小为20*20
ax = sns.heatmap(correction,cmap="Greens", linewidths=0.05,vmax=1, vmin=0 ,annot=True,annot_kws={'size':12,'weight':'bold'})
#热力图参数设置(相关系数矩阵,颜色,每个值间隔等)
plt.xticks(np.arange(7)+0.5,names,fontsize=15) #横坐标标注点
plt.yticks(np.arange(7)+1.5,names,fontsize=15) #纵坐标标注点
#ax.set_xticks(ticks) #生成刻度
#ax.set_yticks(ticks)
#ax.set_xticklabels(names) #生成x轴标签
#ax.set_yticklabels(names)
ax.set_title('Characteristic correlation')#标题设置
# plt.savPlt.style.useefig('cluster.tif',dpi=300)
plt.style.use("ggplot")
plt.savefig("C:/Users/zhuyupeng/Desktop/新建文件夹/相关性.png",bbox_inches = 'tight')
# plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。