import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Simhei']
plt.rcParams['axes.unicode_minus'] = False
decisionNode = dict( boxstyle = 'sawtooth', fc = '0.8' ) #boxstyle为文本框类型,sawtooth为锯齿形
leafNode = dict( boxstyle = 'round4', fc = '0.8' ) #round4为长方圆形,fc是边框线粗细
arrow_args = dict( arrowstyle = '<-' ) #arrowstyle为箭头的样式
def plotNode(nodeTxt, centerpt, parentPt, nodeType):
fig = plt.figure() #括号中未写,下一个函数先定义了一个1,然后调用此函数2次,默认变量会是2,3,也就是说执行这个程序会出现3张图而不是一张,其中第一张是红色底,这个就是默认变量的全局有效性,要想画在一个图中,只需要在此处规定为fig = plt.figure(1)第一张图就可以,下方默认变量第一次定义过红色,全局有效,同一张图中,在后面改无效,以第一次为准
ax1 = fig.add_subplot(111)
ax1.annotate(nodeTxt, xy=parentPt, xycoords='axes fraction', xytext=centerpt,\
textcoords='axes fraction', va='center', ha='center', bbox=nodeType,\
arrowprops=arrow_args)
#四个位置分别是:要显示的文本(和箭头在一起),箭头位置也就是终点,起点,箭头样式
#也可以:
#plt.figure(1)
#plt.subplot(111)
#plt.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='red') #定义一个画布,背景为白色,不定义颜色默认白色
fig.clf() #清空画布
ax1 = fig.add_subplot(111, frameon=False) #frameon表示是否绘制坐标轴矩形
plotNode('决策节点', (0.5, 0.1), (0.1, 0.5), decisionNode)
plotNode('叶节点', (0.8, 0.1), (0.3, 0.8), leafNode)
plt.show()
createPlot()
python——默认变量全局有效性
最新推荐文章于 2023-05-04 00:05:31 发布