Python matplotlib Chinese display parameter setting parsing _python

Source: Internet
Author: User
This article mainly introduced the Python matplotlib Chinese display parameter setting analysis, has the certain reference value, to the Python interest friend may refer to.

Recently in the study of Python's famous drawing package matplotlib found that sometimes the legend and other settings can not display the normal Chinese, so I want to solve the problem.

PS: This article is for Windows only and the other platforms are for informational purposes only.

Reason

Basically, there is no Chinese font in the Matplotlib library.

I installed the anaconda, which is the corresponding Matplotlib font in the folder (how to find the Matplotlib configuration folder where the solution will be described below, easyman~).

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

Solution Solutions

Now tidy up what I think is a better solution:

Parameter settings every time you write code


#coding: Utf-8import matplotlib.pyplot as pltplt.rcparams[' Font.sans-serif ']=[' Simhei '] #用来正常显示中文标签plt. rcparams[' Axes.unicode_minus ']=false #用来正常显示负号 # There is a situation in Chinese that requires U ' content '


Once and for all (recommended)


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


Then go to the C:\Anaconda64\Lib\site-packages\matplotlib\mpl-data folder to see the MATPLOTLIBRC configuration file.

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


#font. Serif:bitstream Vera Serif, New Century schoolbook, Century schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Rom An No9 L, Times New Roman, Times, Palatino, Charter, serif


Then, remove the previous comment!

2) Find the Chinese font in the Matplotlib font library.

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

Note: The role of this step is actually to replace the matplotlib in a default font with the Chinese font we copied, the Chinese font name changed to the font name in Matplotlib.

In fact, there are other options, but it seems that I did not succeed.

Add

Negative sign of Axis normal display


#去掉了注释符号, at the same time, changed to Falseaxes.unicode_minus:False


Example


#coding: Utf-8import Matplotlib.pyplot as Pltplt.plot ((), (4,3,-1)) Plt.xlabel (U ' horizontal axis ') Plt.ylabel (U ' ordinate ') Plt.show ()


Summarize

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.