#coding=utf-8
import matplotlib.pyplot as plt
import numpy as np
nonLeafNode=dict(boxstyle="sawtooth",fc="0.8") #非叶节点,箱子格式,和
leafNodes=dict(boxstyle="round4",fc="0.8")
line=dict(arrowstyle="<-")
def plotNode(nodeName,targetPt,parentPt,nodeType):
"""
pyplot.annotate(s,xy, xytext=None, xycoords=’data’,textcoords=’data’, arrowprops=None, **kwargs)
s=nodeName='nonLeafNode' //是annotate的文本解释,是注解内容。
xy=parentPt=(0.4,0.8) //为点的坐标
xycoords='axes fraction' 和textcoords='axes fraction' //是坐标xy和xytext的说明,
若textcoords=None,则默认textNone与xycoords相同,若都未设置,默认为data
'figure points' //xy将以图片像素源点开始,也就是左下点
'figure pixels' //xy将以图片坐标轴为起点,也是左下的
'figure fraction' //图片的相对位置坐标xy(0.4,0.8)
'axes points' //子图坐标轴的源点
'axes pixels' //子图坐标轴的起点
'axes fraction' //坐标中的相对位置
'data' use the coordinate system of the object being
annotated (default) //默认,也就是数据点 xy=parentPt=(0.4,0.8)
xytext=targetPt=(0.2,0.1) //为注解内容位置坐标,当该值为None时,注解内容放在xy处。
arrowprops=line=dict(arrowstyle="<-") //用于设置字典的形状,类型为字典类型。
**kwargs //用于接收其他设置参数,比如bbox用于设置文本的边框形状
va="center" //居中,可以不写。
"""
createPlot.ax1.annotate(nodeName,xy=parentPt,xycoords='data',xytext=targetPt,textcoords='axes fraction',\
bbox=nodeType,arrowprops=line)
def createPlot():
"""
plt.figure()//对图片的设置。
figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, **kwargs)
num //图片编号
figsize //是一个整数型元组,指定图片的宽和高
dpi //图片像素精度。
facecolor //背景颜色
edgecolor //边界颜色
返回一个Figure对象。也就是返回一张图片。
"""
fig=plt.figure(2,dpi=1,facecolor='white') #图片
fig.clf() #清空图片
createPlot.ax1=plt.subplot(111,frameon=False) #plt.subplot()相当于还是plot类型,就像节点的子节点还是节点。
plotNode('nonLeafNode',(0.2,0.1),(0.4,0.8),nonLeafNode) #(0.4,0.8) #相当于坐标,'nonLeafNode'标记。nonLeafNode 用于设置文本的边框形状
plotNode('leafNodes',(0.8,0.1),(0.6,0.8),leafNodes)
plt.show()
createPlot()
python画图
最新推荐文章于 2022-07-09 12:10:35 发布