本文主要是介绍plt画图中文乱码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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()
这篇关于plt画图中文乱码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!