本文讲述如何利用python绘制如上的相关系数热力图
一.数据说明和需要安装的库
数据是31个省市有关教育的12个指标,如下所示。,在文章最后自取:
需要安装如下库:
pip install pandas
pip install matplotlib
pip install seaborn
我感觉在下面这个python package安装比较好
二.准备绘图
首先导入相关库
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
读取数据
data = pd.read_csv('D:\P\数据分析\相关系数热力图\教育指标.csv')
df = pd.DataFrame(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,
square=True,
cbar=True,
cmap='coolwarm_r',
)
plt.savefig("我是废强热力图.png",dpi=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)
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,
square=True,
cbar=True,
cmap=f'{i}',
)
plt.savefig('图片\\'+f"我是废强热力图颜色{i}.png", dpi=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)
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,
square=True,
cbar=True,
cmap='coolwarm_r',
)
plt.savefig("我是废强热力图.png", dpi=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,
square=True,
cbar=True,
cmap=f'{i}',
)
plt.savefig('图片\\' + f"我是废强热力图颜色{i}.png", dpi=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
|