数据是31个省市有关教育的12个指标,如下所示。,在文章最后自取:
pip install pandas
pip install matplotlib
pip install seaborn
首先导入相关库
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
读取数据
data = pd.read_csv('D:\P\数据分析\相关系数热力图\教育指标.csv')
df = pd.DataFrame(data)
# print(data)
计算出相关系数并输出,这里选择的是皮尔逊相关系数,当然你也可以选择其他相关系数,有关其他相关系数可以参考这篇文章
cor = data.corr(method='pearson')
print(cor) # 输出相关系数
因为我这里有中文
,所以需要进行下面的设置
。我这里设置为黑体,当然你也可以选择其他字体
rc = {'font.sans-serif': 'SimHei',
'axes.unicode_minus': False}
sns.set(font_scale=0.7,rc=rc) # 设置字体大小
好了,开始绘图啦:
sns.heatmap(cor,
annot=True, # 显示相关系数的数据
center=0.5, # 居中
fmt='.2f', # 只显示两位小数
linewidth=0.5, # 设置每个单元格的距离
linecolor='blue', # 设置间距线的颜色
vmin=0, vmax=1, # 设置数值最小值和最大值
xticklabels=True, yticklabels=True, # 显示x轴和y轴
square=True, # 每个方格都是正方形
cbar=True, # 绘制颜色条
cmap='coolwarm_r', # 设置热力图颜色
)
plt.savefig("我是废强热力图.png",dpi=600)#保存图片,分辨率为600
plt.ion() #显示图片
由于这里的配色是在是有太多太多,所以不打算一个个手动更换,因此我们可以使用循环语句
cmap='coolwarm_r'#在这里更换颜色
colors=“Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r,RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r,gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r,hsv, hsv_r, icefire,icefire_r, inferno,inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r”
代码循环画图
:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = pd.read_csv('D:\P\数据分析\相关系数热力图\教育指标.csv')
df = pd.DataFrame(data)
# print(data)
################################一行一行读取数据
# for i in range(len(data)):
# document=df[i:i+1]
# print(document,'\n')
################################
# 首先计算出相关系数
cor = data.corr(method='pearson')
print(cor) # 输出相关系数
rc = {'font.sans-serif': 'SimHei',
'axes.unicode_minus': False}
sns.set(font_scale=0.7,rc=rc) # 设置字体大小
#设置热力图颜色配色
colors="Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r,RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r,gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r,hsv, hsv_r, icefire,icefire_r, inferno,inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r"
color=colors.split(',')
for i in color:
i=i.strip()
print(i)
sns.heatmap(cor,
annot=True, # 显示相关系数的数据
center=0.5, # 居中
fmt='.2f', # 只显示两位小数
linewidth=0.5, # 设置每个单元格的距离
linecolor='blue', # 设置间距线的颜色
vmin=0, vmax=1, # 设置数值最小值和最大值
xticklabels=True, yticklabels=True, # 显示x轴和y轴
square=True, # 每个方格都是正方形
cbar=True, # 绘制颜色条
cmap=f'{i}', # 设置热力图颜色
)
plt.savefig('图片\\'+f"我是废强热力图颜色{i}.png", dpi=600) # 保存图片,分辨率为600
plt.ion() # 显示图片,这个可以方便后面自动关闭
plt.pause(0.5)
plt.close()#关闭图片
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = pd.read_csv('D:\P\数据分析\相关系数热力图\教育指标.csv')
df = pd.DataFrame(data)
# print(data)
################################一行一行读取数据
# for i in range(len(data)):
# document=df[i:i+1]
# print(document,'\n')
################################
# 首先计算出相关系数
cor = data.corr(method='pearson')
print(cor) # 输出相关系数
rc = {'font.sans-serif': 'SimHei',
'axes.unicode_minus': False}
sns.set(font_scale=0.7, rc=rc) # 设置字体大小
sns.heatmap(cor,
annot=True, # 显示相关系数的数据
center=0.5, # 居中
fmt='.2f', # 只显示两位小数
linewidth=0.5, # 设置每个单元格的距离
linecolor='blue', # 设置间距线的颜色
vmin=0, vmax=1, # 设置数值最小值和最大值
xticklabels=True, yticklabels=True, # 显示x轴和y轴
square=True, # 每个方格都是正方形
cbar=True, # 绘制颜色条
cmap='coolwarm_r', # 设置热力图颜色
)
plt.savefig("我是废强热力图.png", dpi=600) # 保存图片,分辨率为600
plt.ion() # 显示图片
plt.close('all') # 关闭图片
# 设置热力图颜色配色
colors = "Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r,RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r,gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r,hsv, hsv_r, icefire,icefire_r, inferno,inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r"
color = colors.split(',')
for i in color:
i = i.strip()
print(i)
sns.heatmap(cor,
annot=True, # 显示相关系数的数据
center=0.5, # 居中
fmt='.2f', # 只显示两位小数
linewidth=0.5, # 设置每个单元格的距离
linecolor='blue', # 设置间距线的颜色
vmin=0, vmax=1, # 设置数值最小值和最大值
xticklabels=True, yticklabels=True, # 显示x轴和y轴
square=True, # 每个方格都是正方形
cbar=True, # 绘制颜色条
cmap=f'{i}', # 设置热力图颜色
)
plt.savefig('图片\\' + f"我是废强热力图颜色{i}.png", dpi=600) # 保存图片,分辨率为600
plt.ion() # 显示图片,这个可以方便后面自动关闭
plt.pause(0.5)
plt.close() # 关闭图片
最后:数据链接:,直接点击链接,或复制网址,有提取码
链接:https://pan.baidu.com/s/1qcfw5TUh0c4C6igoipmdGA?pwd=5fii
提取码:5fii
参考链接
https://mp.weixin.qq.com/s/shQOmqR0JXkp_pGCfLuCPA