解决热力图坐标轴文字保存显示不全,数字越界显示不全,刻度条负号显示为框的问题

import numpy as np
import pandas as pd

data = pd.read_excel("数据.xlsx",sheet_name="Sheet1",header=0,encoding="utf-8")
data=data.iloc[:,1:]
import seaborn as sns
import matplotlib.pyplot as plt
titlea=["变量1","变量2","变量3","变量4","变量5"]
data1=data[["变量1","变量2","变量3","变量4","变量5"]]


#######保存相关系数小数点后两位###########
####法1:
cor1=data1.corr(method="spearman")
#sns.heatmap(cor1, fmt='.1g')#保留两位

####法2:
cor0=np.round(data1.corr(method="spearman"),2)
cor=pd.DataFrame(cor0,columns=titlea)
#cor=cor.apply(lambda x:x.astype(float))

####解决中文显示乱码########
sns.set(font='simhei')

 #####解决刻度条负号为框#####
plt.rcParams['axes.unicode_minus'] = False

plt.subplots(figsize=(10, 10)) # 设置画面大小
str1='heatmap64.png' 
sns.heatmap(cor, annot=True, vmax=1, square=True, cmap="Blues")

 ####这句用于处理边界数字越界显示不全####
plt.ylim(0,len(cor))

 ####解决保存图时坐标文字显示不全#######
plt.tight_layout()

plt.savefig(str1)

这个文章很仔细
https://blog.csdn.net/cymy001/article/details/79576019

    原文作者:weixin_42663919
    原文地址: https://blog.csdn.net/weixin_42663919/article/details/106565898
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞