The solution of matplotlib Chinese garbled in Python

Source: Internet
Author: User
Matplotlib is a good drawing package for Python, but it does not natively support Chinese (it seems that there is no Chinese font in its default configuration), so if Chinese is present in the drawing, it will appear garbled

Matplotlib is a good drawing package for Python, but it does not natively support Chinese (it seems that there is no Chinese font in its default configuration), so if Chinese is present in the drawing, it will appear garbled.

Matplotlib when drawing images with Chinese annotations, there will be garbled problems.

Instance code:


Import Matplotlibimport Matplotlib.pyplot as plt# define text box and arrow format Decisionnode =dict (boxstyle= "Sawtooth", fc= "0.8") Leafnode =dict (boxstyle= "Round4", fc= "0.8") Arrow_args=dict (arrowstyle= "<-") #绘制带箭头的注解def Plotnode (Nodetxt,centerpt, Parentpt,nodetype):  createPlot.axl.annotate (nodetxt,xy=parentpt,xycoords= ' axes fraction ', xytext=centerpt, textcoords= ' axes fraction ', va= "center", ha= "center", Bbox=nodetype,arrowprops=arrow_args) def createplot ():  Fig =plt.figure (1,facecolor= ' white ')  FIG.CLF ()  createplot.axl=plt.subplot (111,frameon=false)  Plotnode ( U ' decision point ', (0.5,0.1), (0.1,0.5), Decisionnode)  plotnode (U ' leaf node ', (0.8,0.1), (0.3,0.8), Leafnode)  plt.show ()

Workaround: Introduce fonts into your code

Import Matplotlib.pyplot as Pltimport matplotlib# defines a custom font, the file name is the system chinese font myfont = Matplotlib.font_manager. Fontproperties (fname= ' C:/windows/fonts/simkai.ttf ') #解决负号 '-' Display as a block problem matplotlib.rcparams[' Axes.unicode_minus ']= False decisionnode =dict (boxstyle= "Sawtooth", fc= "0.8") Leafnode=dict (boxstyle= "Round4", fc= "0.8") arrow_args=dict ( Arrowstyle= "<-") def Plotnode (nodetxt,centerpt,parentpt,nodetype):  createPlot.axl.annotate (nodetxt,xy= parentpt,xycoords= ' axes fraction ', xytext=centerpt,textcoords= ' axes fraction ', va= ' center ', ha= ' center ', bbox= Nodetype,arrowprops=arrow_args,fontproperties=myfont) def createplot ():  fig =plt.figure (1,facecolor= ' white ')  FIG.CLF ()  createplot.axl=plt.subplot (111,frameon=false)  Plotnode (U ' decision Point ', (0.5,0.1), (0.1,0.5), Decisionnode)  plotnode (U ' leaf node ', (0.8,0.1), (0.3,0.8), Leafnode)  plt.show ()

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.