In Python, the Chinese is not displayed by default, as in the following code:
[Python]View PlainCopy
- Import Matplotlib.pyplot as Plt
- # define text box and arrow formatting
- Decisionnode = dict (Boxstyle = "Sawtooth", FC = "0.8")
- Leafnode = dict (Boxstyle = "Round4", FC = "0.8")
- Arrow_args = dict (Arrowstyle = "<-")
- # Draw an annotation with arrows
- def plotnode (Nodetxt, Centerpt, PARENTPT, NodeType):
- CreatePlot.ax1.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.ax1 = Plt.subplot (111, Frameon = False)
- Plotnode (U' decision node ', (0.5, 0.1), (0.1, 0.5), Decisionnode)
- Plotnode (U' leaf node ', (0.8, 0.1), (0.3, 0.8), Leafnode)
- Plt.show ()
- 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
- From Pylab Import *
- mpl.rcparams[' font.sans-serif '] = [' Simhei ']
The source code is modified as follows:
[Python]View PlainCopy
- Import Matplotlib.pyplot as Plt
- From Pylab Import *
- mpl.rcparams[' font.sans-serif '] = [' Simhei ']
- # define text box and arrow formatting
- Decisionnode = dict (Boxstyle = "Sawtooth", FC = "0.8")
- Leafnode = dict (Boxstyle = "Round4", FC = "0.8")
- Arrow_args = dict (Arrowstyle = "<-")
- # Draw an annotation with arrows
- def plotnode (Nodetxt, Centerpt, PARENTPT, NodeType):
- CreatePlot.ax1.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.ax1 = Plt.subplot (111, Frameon = False)
- Plotnode (U' decision node ', (0.5, 0.1), (0.1, 0.5), Decisionnode)
- Plotnode (U' leaf node ', (0.8, 0.1), (0.3, 0.8), Leafnode)
- Plt.show ()
- Createplot ()
Finally get the image
Success!
Original link: http://blog.csdn.net/u013038499/article/details/52449768
"Go" Python, matplotlib drawing cannot display Chinese problems