Home  >  Article  >  Backend Development  >  python matplotlib Chinese display parameter setting analysis_python

python matplotlib Chinese display parameter setting analysis_python

韦小宝
韦小宝Original
2017-12-16 11:26:351584browse

This article mainly introduces the analysis of Chinese display parameter settings of python matplotlib, which has certain reference value. Friends who are interested in python can refer to it.

Recently, when I was learning Python's famous Drawing package matplotlib, I found that sometimes legends and other settings could not display Chinese properly, so I wanted to solve this problem.

PS: This article is only for Windows, other platforms are for reference only.

The reason

is roughly that there are no Chinese fonts in the matplotlib library.

I installed anaconda, which is the folder where the corresponding matplotlib font is located (how to find the matplotlibconfig file folder, the solution will be described below, easyman~).

C:\Anaconda64\Lib\site-packages\matplotlib\mpl-data\fonts\ttf

Solution

Now to sort me out I think a better solution is:

Set parameters every time you write code


#coding:utf-8
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
#有中文出现的情况,需要u'内容'


Once and for all (recommended)


import matplotlib
matplotlib.matplotlib_fname() #将会获得matplotlib包所在文件夹


Then enter C:\Anaconda64\Lib\site-packages\matplotlib\mpl-data and you can see it in this folder to the matplotlibrc configuration file.

1) Open the configuration file and find the following line:


#font.serif : Bitstream Vera Serif, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif


Then change the preceding NoteRemove!

2) Find Chinese fonts and put them in the matplotlib font library.

Copy the font under the Windows folder: C:\Windows\Fonts\MicrosoftYaHeiUI, and then paste it into the C:\Anaconda64\Lib\site-packages\matplotlib\mpl-data\fonts\ttf folder, And rename it Vera.ttf.

Note: The function of this step is actually to replace a default font in matplotlib with the Chinese font we copied, and change the Chinese font name to the font name in matplotlib.

Actually, there are other solutions, but they seem to have failed in my case.

Supplement

The negative sign of the coordinate axis is displayed normally


#去掉了注释符号,同时,改为False
axes.unicode_minus : False


Example


#coding:utf-8
import matplotlib.pyplot as plt
plt.plot((1,2,3),(4,3,-1))
plt.xlabel(u'横坐标')
plt.ylabel(u'纵坐标')
plt.show()


##Summary

Above This is the entire content of this article about the analysis of Chinese display parameter settings of python matplotlib. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:

Related recommendations:

Use regular expressions in python Example code of formula connector

Python implementation of outputting colored string case analysis

Python implementation of genetic algorithm code

The above is the detailed content of python matplotlib Chinese display parameter setting analysis_python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn