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 ()