Python自学笔记Matplotlib风羽自定义Word文档下载推荐.docx

上传人:b****5 文档编号:15710498 上传时间:2022-11-15 格式:DOCX 页数:14 大小:21KB
下载 相关 举报
Python自学笔记Matplotlib风羽自定义Word文档下载推荐.docx_第1页
第1页 / 共14页
Python自学笔记Matplotlib风羽自定义Word文档下载推荐.docx_第2页
第2页 / 共14页
Python自学笔记Matplotlib风羽自定义Word文档下载推荐.docx_第3页
第3页 / 共14页
Python自学笔记Matplotlib风羽自定义Word文档下载推荐.docx_第4页
第4页 / 共14页
Python自学笔记Matplotlib风羽自定义Word文档下载推荐.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

Python自学笔记Matplotlib风羽自定义Word文档下载推荐.docx

《Python自学笔记Matplotlib风羽自定义Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Python自学笔记Matplotlib风羽自定义Word文档下载推荐.docx(14页珍藏版)》请在冰豆网上搜索。

Python自学笔记Matplotlib风羽自定义Word文档下载推荐.docx

'

Demonstrationofwindbarbplots

importmatplotlib.pyplotasplt

importnumpyasnp

x=np.linspace(-5,5,5)

X,Y=np.meshgrid(x,x)

U,V=12*X,12*Y

data=[(-1.5,.5,-6,-6),(1,-1,-46,46),(-3,-1,11,-11),(1,1.5,80,80),(0.5,0.25,25,15),(-1.5,-0.5,-5,40)]

data=np.array(data,dtype=[('

x'

np.float32),('

y'

u'

v'

np.float32)])

#Defaultparameters,uniformgrid

ax=plt.subplot(2,2,1)

ax.barbs(X,Y,U,V)

#Arbitrarysetofvectors,makethemlongerandchangethepivotpoint

#(pointaroundwhichthey'

rerotated)tobethemiddle

ax=plt.subplot(2,2,2)

ax.barbs(data['

],data['

],length=8,pivot='

middle'

#Showingcolormappingwithuniformgrid.Fillthecircleforanemptybarb,

#don'

troundthevalues,andchangesomeofthesizeparameters

ax=plt.subplot(2,2,3)

ax.barbs(X,Y,U,V,np.sqrt(U*U+V*V),fill_empty=True,rounding=False,sizes=dict(emptybarb=0.25,spacing=0.2,height=0.3))

#Changecolorsaswellastheincrementsforpartsofthebarbs

ax=plt.subplot(2,2,4)

],flagcolor='

r'

barbcolor=['

b'

'

g'

],barb_increments=dict(half=10,full=20,flag=100),flip_barb=True)

plt.show()

 

二、源代码解读

1.classBarbs()

classBarbs(mcollections.PolyCollection):

@docstring.interpd

def__init__(self,ax,*args,**kw):

'

...'

def_find_tails(self,mag,rounding=True,half=5,full=10,flag=50):

def_make_barbs(self,u,v,nflags,nbarbs,half_barb,empty_flag,length,pivot,sizes,fill_empty,flip):

defset_UVC(self,U,V,C=None):

defset_offsets(self,xy):

通过读源代码可知类Barbs有五个方法分别为__init__、_find_tails、_make_barbs、set_UVC、set_offsets。

2.__init__

"

"

Theconstructortakesonerequiredargument,anAxes

instance,followedbytheargsandkwargsdescribed

bythefollowingpylabinterfacedocumentation:

%(barbs_doc)s

self._pivot=kw.pop('

pivot'

tip'

self._length=kw.pop('

length'

7)

barbcolor=kw.pop('

barbcolor'

None)

flagcolor=kw.pop('

flagcolor'

self.sizes=kw.pop('

sizes'

dict())

self.fill_empty=kw.pop('

fill_empty'

False)

self.barb_increments=kw.pop('

barb_increments'

self.rounding=kw.pop('

rounding'

True)

self.flip=kw.pop('

flip_barb'

transform=kw.pop('

transform'

ax.transData)

#Flagcolorandandbarbcolorprovideconvenienceparametersfor

#settingthefacecolorandedgecolor,respectively,ofthebarb

#polygon.Wealsoworkheretomaketheflagthesamecolorasthe

#restofthebarbbydefault

ifNonein(barbcolor,flagcolor):

kw['

edgecolors'

]='

face'

ifflagcolor:

facecolors'

]=flagcolor

elifbarbcolor:

]=barbcolor

else:

#Settofacecolorpassedinordefaulttoblack

kw.setdefault('

k'

#Parseoutthedataarraysfromthevariousconfigurationssupported

x,y,u,v,c=_parse_args(*args)

self.x=x

self.y=y

xy=np.hstack((x[:

np.newaxis],y[:

np.newaxis]))

#Makeacollection

barb_size=self._length**2/4#Empiricallydetermined

mcollections.PolyCollection.__init__(self,[],(barb_size,),

offsets=xy,

transOffset=transform,**kw)

self.set_transform(transforms.IdentityTransform())

self.set_UVC(u,v,c)

__init__()方法为初始化方法,此方法中flagcolor、barbcolor为设置风羽颜色的关键字,中间的说明文字提示颜色设置是针对所有的风羽的,所以通过颜色设置达不到风羽中既有空心白色三角又有实心黑色三角。

初始化方法中在对一些参数进行了初始化赋值后执行了set_UVC()方法,所以我们顺着这个set_UVC()方法往下继续读。

3.set_UVC()

self.u=ma.masked_invalid(U,copy=False).ravel()

self.v=ma.masked_invalid(V,copy=False).ravel()

ifCisnotNone:

c=ma.masked_invalid(C,copy=False).ravel()

x,y,u,v,c=delete_masked_points(self.x.ravel(),

self.y.ravel(),

self.u,self.v,c)

x,y,u,v=delete_masked_points(self.x.ravel(),self.y.ravel(),

self.u,self.v)

magnitude=np.hypot(u,v)

flags,emptyflags,barbs,halves,empty=self._find_tails(magnitude,

self.rounding,

**self.barb_increments)

#Gettheverticesforeachofthebarbs

plot_barbs=self._make_barbs(u,v,flags,emptyflags,barbs,halves,empty,

self._length,self._pivot,self.sizes,

self.fill_empty,self.flip)

self.set_verts(plot_barbs)

#Setthecolorarray

self.set_array(c)

#Updatetheoffsets

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 动态背景

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1