要解决matplotlib中文乱码问题,可以按照以下步骤进行操作:
导入matplotlib库和字体模块:import matplotlib.pyplot as pltfrom matplotlib.font_manager import FontProperties选择合适的中文字体,可以使用系统中已经安装的中文字体,也可以下载其他中文字体。这里以SimHei字体为例:font = FontProperties(fname="SimHei.ttf", size=12)注意,SimHei.ttf是SimHei字体文件的路径,如果你的电脑上没有这个字体文件,可以使用其他中文字体的路径。
设置默认字体为中文字体:plt.rcParams['font.family'] = font.get_name()正常使用中文即可,例如:plt.plot([1, 2, 3, 4], [1, 4, 9, 16])plt.title('中文标题', fontproperties=font)plt.xlabel('横轴', fontproperties=font)plt.ylabel('纵轴', fontproperties=font)plt.show()这样就能够在matplotlib中正常显示中文了。

