"Go" Python, matplotlib drawing cannot display Chinese problems

Source: Internet
Author: User

In Python, the Chinese is not displayed by default, as in the following code:

[Python]View PlainCopy
  1. Import Matplotlib.pyplot as Plt
  2. # define text box and arrow formatting
  3. Decisionnode = dict (Boxstyle = "Sawtooth", FC = "0.8")
  4. Leafnode = dict (Boxstyle = "Round4", FC = "0.8")
  5. Arrow_args = dict (Arrowstyle = "<-")
  6. # Draw an annotation with arrows
  7. def plotnode (Nodetxt, Centerpt, PARENTPT, NodeType):
  8. CreatePlot.ax1.annotate (nodetxt, xy = parentpt, xycoords = ' axes fraction ', Xytext = centerpt, textcoords = ' axes Fraction ', VA = ' center ', ha = ' center ', bbox = nodeType, arrowprops = Arrow_args)
  9. Def createplot ():
  10. Fig = Plt.figure (1, facecolor=' white ')
  11. FIG.CLF ()
  12. Createplot.ax1 = Plt.subplot (111, Frameon = False)
  13. Plotnode (U' decision node ', (0.5, 0.1), (0.1, 0.5), Decisionnode)
  14. Plotnode (U' leaf node ', (0.8, 0.1), (0.3, 0.8), Leafnode)
  15. Plt.show ()
  16. Createplot ()

Get the image as follows:

The reason for the Chinese garbled is that the default setting of the font does not have a Chinese font, so we just have to add the name of the Chinese font manually.

Manually add the following code

[Python]View PlainCopy
    1. From Pylab Import *
    2. mpl.rcparams[' font.sans-serif '] = [' Simhei ']


The source code is modified as follows:

[Python]View PlainCopy
  1. Import Matplotlib.pyplot as Plt
  2. From Pylab Import *
  3. mpl.rcparams[' font.sans-serif '] = [' Simhei ']
  4. # define text box and arrow formatting
  5. Decisionnode = dict (Boxstyle = "Sawtooth", FC = "0.8")
  6. Leafnode = dict (Boxstyle = "Round4", FC = "0.8")
  7. Arrow_args = dict (Arrowstyle = "<-")
  8. # Draw an annotation with arrows
  9. def plotnode (Nodetxt, Centerpt, PARENTPT, NodeType):
  10. CreatePlot.ax1.annotate (nodetxt, xy = parentpt, xycoords = ' axes fraction ', Xytext = centerpt, textcoords = ' axes Fraction ', VA = ' center ', ha = ' center ', bbox = nodeType, arrowprops = Arrow_args)
  11. Def createplot ():
  12. Fig = Plt.figure (1, facecolor=' white ')
  13. FIG.CLF ()
  14. Createplot.ax1 = Plt.subplot (111, Frameon = False)
  15. Plotnode (U' decision node ', (0.5, 0.1), (0.1, 0.5), Decisionnode)
  16. Plotnode (U' leaf node ', (0.8, 0.1), (0.3, 0.8), Leafnode)
  17. Plt.show ()
  18. Createplot ()

Finally get the image

Success!

Original link: http://blog.csdn.net/u013038499/article/details/52449768

"Go" Python, matplotlib drawing cannot display Chinese problems

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.