一行代码解决Python中plot画图无法显示中文问题

问题描述

使用matplotlib的pyplot画图无法显示中文,显示出两个框

一行代码解决Python中plot画图无法显示中文问题_第1张图片

def draw_train_process(iters, train_costs):
    title = "中文"
    plt.title(title, fontsize=24)
    plt.xlabel("iter", fontsize=14)
    plt.ylabel("cost", fontsize=14)
    plt.plot(iters, train_costs, color='red', label='training cost')
    plt.grid()
    plt.show()

解决方案:

一行代码设置一下字体就好了,问就是书里看的

def draw_train_process(iters, train_costs):
    title = "中文"
    plt.rcParams['font.sans-serif'] = 'SimHei' # 设置字体为SimHei
    plt.title(title, fontsize=24)
    plt.xlabel("iter", fontsize=14)
    plt.ylabel("cost", fontsize=14)
    plt.plot(iters, train_costs, color='red', label='training cost')
    plt.grid()
    plt.show()

一行代码解决Python中plot画图无法显示中文问题_第2张图片

你可能感兴趣的:(人生经验和踩过的坑,python,开发语言)