1、使用font_manager的FontProperties解決
通過FontProperties
來設置字符及大小,來解決中文顯示的問題,代碼如下:
import matplotlib
import matplotlib.pyplot as pltpath ="..\simsun.ttc"#改成你自己的文件路徑
font = FontProperties(fname=path, size=14) plt.figure(figsize=(8, 6))
plt.bar(range(len(sorted_indices)), similarity_matrix.sum(axis=1)[sorted_indices], tick_label=['模式1', '模式2', '模式3'])
plt.title("模式關聯度排序", fontproperties=font)
plt.xlabel("模式", fontproperties=font)
plt.ylabel("關聯度" ,fontproperties=font)
plt.show()
2、使用matplotlib中方法的fontproperties參數解決
通Matplotlib中xlabel()
、ylabel()
和title()
的參數直接指定字體,代碼如下:
import matplotlib
import matplotlib.pyplot as pltplt.figure(figsize=(8, 6))
plt.bar(range(len(sorted_indices)), similarity_matrix.sum(axis=1)[sorted_indices], tick_label=['模式1', '模式2', '模式3'])
plt.title("模式關聯度排序", fontproperties='SimSun')
plt.xlabel("模式", fontproperties='SimSun')
plt.ylabel("關聯度" ,fontproperties='SimSun')
plt.show()
3、使用matplotlib的rcParams來解決
使用matplotlib的rcParams
設置字體,會全局生效,如不想全局生效,可以參考上面的方法
plt.rcParams['font.sans-serif'] = ['SimHei'] # 步驟一(替換sans-serif字體)
plt.rcParams['axes.unicode_minus'] = False # 步驟二(解決坐標軸負數的負號顯示問題)plt.figure(figsize=(8, 6))
sns.heatmap(similarity_matrix, annot=True, cmap="YlGnBu", xticklabels=['可信性', '互動性', '吸引力', '專業性', '產品質量', '產品種類', '感知價值'],yticklabels=['可信性', '互動性', '吸引力', '專業性', '產品質量', '產品種類', '感知價值'] )
plt.title("關聯度矩陣熱力圖", fontproperties="SimSun")
plt.xlabel("模式", fontproperties="SimSun")
plt.ylabel("模式", fontproperties="SimSun")
plt.show()