[Unable to display Chinese characters when plt draws under Mac OS]

Under Mac OS, plt drawing Chinese cannot display the problem

Problem Description

  • When using pycharm for plt drawing, the title and some attributes contain Chinese, and the result is displayed as a box, as shown in the figure below

Solution

  • I found a lot of methods on the Internet, and it took a long time. I found that it is not necessary to install various fonts or to change the configuration, and the download of fonts is very troublesome. The common methods are as follows

Solution 1

  • Use the following code, the format isu'中文'

    import numpy as np
    import matplotlib.pyplot as plt
    plt.rcParams['font.sans-serif'] = ['SimHei']
    # plt.rc('font', family='SimHei', size=13)
    

    I can implement the above method under the win10 system, but it still doesn't work under the Mac os.

Solution 2

  • I accidentally found that a big guy shared an interesting method. You can first check the Chinese fonts that come with python, and enter the following code to view:

    from matplotlib.font_manager import FontManager
    fm = FontManager()
    mat_fonts = set(f.name for f in fm.ttflist)
    print (mat_fonts)
    

    As shown in the figure below, the system comes with many fonts
    insert image description here

  • So just putting the following code at the beginning of your script will solve the problem

    import numpy as np
    import matplotlib.pyplot as plt
    plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
    
  • The effect is as follows:
    Please add a picture description

Guess you like

Origin blog.csdn.net/crist_meng/article/details/126973543