Python implements matplotlib to display Chinese characters,

Source: Internet
Author: User

Python implements matplotlib to display Chinese characters,

This example describes how to implement matplotlib to display Chinese Characters in Python. We will share this with you for your reference. The details are as follows:

Note]

It may have nothing to do with the topic of this article, but I still want to point out: when using the matplotlib library, the following two import methods are equivalent (I mean equivalent, of course, this can be discussed :)

import matplotlib.pyplot as plt
import pylab as plt

【]

Method 1: FontProperties

Import matplotlib. pyplot as pltfrom matplotlib. font_manager import FontProperties # Step 1 #... font = FontProperties (fname = r "c: \ windows \ fonts \ simsun. ttc ", size = 14) # Step 2 plt. xlabel ("X axis", fontproperties = font) # Step 3 plt. ylabel ("Y axis", fontproperties = font) plt. title ("title", fontproperties = font) plt. show ()

In general, it is ugly.

Method 2: fontproperties

Import matplotlib. pyplot as plt #... plt. xlabel ("X axis") plt. ylabel ("Y axis", fontproperties = "SimSun") # Step 1 () plt. title ("title", fontproperties = "SimHei") # () plt. show ()

Flexibility, another advantage: Global font settings are not contaminated

Method 3: rcParams

Import matplotlib. pyplot as pltplt. rcParams ['font. sans-serif '] = ['simhei'] # Step 1 (replace sans-serif font) plt. rcParams ['axes. unicode_minus '] = False # Step 2 (to solve the problem of displaying the negative number of the coordinate axis )#... plt. xlabel ("X axis") plt. ylabel ("Y axis") plt. title ("title") plt. show ()

Simple usage. Disadvantage: Contaminated global font settings. (So the second step is required)

Method 4: rc

Import matplotlib. pyplot as pltfont = {'family ': 'simhei', 'weight': 'bold', 'SIZE': '16'} plt. rc ('font', ** font) # Step 1 (set more font attributes) plt. rc ('axes ', unicode_minus = False) # Step 2 (To solve the negative number display problem of the coordinate axis )#... plt. xlabel ("X axis") plt. ylabel ("Y axis") plt. title ("title") plt. show ()

Flexible usage. Disadvantage: The Global font is affected (so the second step is required)

[Summary]

It is set only when method 2 is used, and the global font settings are not contaminated, making it more flexible
Method 3 and method 4 do not require hard encoding of the font path, and they are configured at a time and used multiple times for convenience.

[Test code]

Import numpy as npimport matplotlib. pyplot as pltfont = {'family ': 'dfkai-SB', 'weight': 'bold ', 'SIZE': '16'} plt. rc ('font', ** font) # pass in the font dict as kwargsplt. rc ('axes ', unicode_minus = False) x = np. linspace (0, 10,100 0) y = np. sin (x) z = np. cos (x ** 2) plt. figure (figsize = (8, 4) plt. plot (x, y, label = "$ sin (x) $", color = "red", linewidth = 2) plt. plot (x, z, "B --", label = "$ cos (x ^ 2) $") plt. xlabel ("X axis") plt. ylabel ("Y axis") plt. title ("title") plt. ylim (-1.2, 1.2) plt. legend () plt. show ()

Running result:

[Appendix]

English names of some Chinese Fonts

SimSun
Simhei SimHei
Microsoft YaHei
Microsoft is in bold Microsoft JhengHei
Xin NSimSun
New body PMingLiU
Fine body MingLiU
Standard entity DFKai-SB
Imitation Song FangSong
Body KaiTi
Shu LiSu
Youyuan YouYuan
STXihei
STKaiti
STSong
文中 STZhongsong
STFangsong
Founder Shu-Ti FZShuTi
Founder Yao Ti FZYaoti
文 cloud STCaiyun
Mandarin amber STHupo
STLiti
STXingkai
Chinese New Wei STXinwei

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.