!usrbinenvpythoncoding:utf8Matplotlibdoesnotnativelysupportgradients。Matplotlib本身并不支持渐变。However,wecanemulateagradientfilledrectanglebyanAxesImageoftherightsizeandcoloring。然而我们可以通过正好大小和颜色的AxesImage对象模拟渐变的正方形Inparticular,weuseacolormaptogeneratetheactualcolors。特别的,我们可以使用colormap来生成实际的颜色Itisthensufficienttodefinetheunderlyingvaluesonthecornersoftheimageandletbicubicinterpolationfilloutthearea。先定义图片角落的基础值,然后利用双三次的算法来对图片的其它区域进行插值。Wedefinethegradientdirectionbyaunitvectorv。通过单位向量v定义了梯度的方向Thevaluesatthecornersarethenobtained我们先在要获得图片角上的值,通过什么方式获得呢?bythelengthsoftheprojectionsofthecornervectorsonv。角落的向量,在V上的投影的长度Asimilarapproachcanbeusedtocreateagradientbackgroundforanaxes。同样的方法,可以被用于创建axes的背景色的渐变。Inthatcase,itishelpfultousesAxescoordinates(extent(0,1,0,1),transformax。transAxes)tobeindependentofthedatacoordinates。在创建axes背景色的渐变时,使用Axes的坐标系,是非常有标注的。这样做可以独立数据坐标系。importmatplotlib。pyplotaspltimportnumpyasnpnp。random。seed(19680801)defgradientimage(ax,extent,direction0。3,cmaprange(0,1),kwargs):Drawagradientimagebasedonacolormap。基于colormap生成一个有颜色有梯度变化的图片Parametersax:Axes坐标系Theaxestodrawon。图片就画在这个坐标系里面extentTheextentoftheimageas(xmin,xmax,ymin,ymax)。图片绘画的范围,xmin和xMax标注了,在x轴方向的范围;ymin和ymax标注了在y轴方向的范围;Bydefault,thisisinAxescoordinatesbutmaybechangedusingthetransformkeywordargument。默认情况下,使用Axes的坐标系,如果想改坐标系可以使用transform的坐标系。direction:floatThedirectionofthegradient。颜色梯度的变化范围内。Thisisanumberinrange0(vertical)to1(horizontal)。如果是0就是垂直的,1就是水平的。cmaprange:float,floatThefraction(cmin,cmax)ofthecolormapthatshouldbeusedforthegradient,wherethecompletecolormapis(0,1)。这个参数控制颜色范围,在范围必须在0到1之间选一个范围kwargsOtherparametersarepassedonto。Axes。imshow()。Inparticularusefuliscmap。其它的一些参数,传递给Axes。imshow()。对cmap非常有用。如果direction是水平方向的,direction0,phi0如果direction是垂直方向的,direction1,phi3。1421。57phidirectionnp。pi2新建一个numpy数组,数据维度为一行两列vnp。array(〔np。cos(phi),np。sin(phi)〕)这个的,是代表两个numpy数组进行叉乘。print(v〔1,0〕)print(v〔1,0〕)渐变范围:图像四个角的颜色Xnp。array(〔〔v〔1,0〕,v〔1,1〕〕,〔v〔0,0〕,v〔0,1〕〕〕)cmparange是颜色范围;这是一个解包的过程,a为最小值,b为最大值a,bcmaprange对颜色进行归一化,将颜色归一化到a,b之间这里采用的是最大值归一化,步骤如下:第一步:XX。max():这一步是将X中的数全部变成小数第二步:XX。max()(ba)这一步是将X中的值,变成0到ba之间的值第三步:XX。max()(ba)a这一步是将X中的值,变成a到b之间的值对上面的式子,采用交换律就得到了下面的式子Xa(ba)XX。max()X带表了颜色的范围;extent字面意思是范围的意思,指定了画图的范围;interpolation:指定了颜色的填充值的算法vmin,vmax指定颜色覆盖的数据范围imax。imshow(X,extentextent,interpolationbicubic,vmin0,vmax1,kwargs)returnimdefgradientbar(ax,x,y,width0。5,bottom0):这个方法完成柱子的渐变,计算每个柱子的范围。:paramax::paramx::paramy::paramwidth::parambottom::return:forleft,topinzip(x,y):left是柱子的最左边的x轴对应的值top是柱子的高度,是y方向的值right是柱子最右边的x轴对应的值bottom是柱子最底部的值这里描述了一个长方形,并不是用坐标系描述的;所谓的坐标系描述方法:在一个坐标系中定义一个长方形,需要四个点,对应长方形的四个角的坐标而是用范围描述的:范围描述是,直接在坐标系中切四刀,然后就构成了一个矩阵。rightleftwidthcmap指定了色系cmparange指定了色系的变化范围gradientimage(ax,extent(left,right,bottom,top),cmapplt。cm。Bluesr,cmaprange(0,0。8))x轴的最大值和最小值xmin,xmaxxlim0,10y轴的最小值和最大值ymin,ymaxylim0,1fig,axplt。subplots()设置坐标系的范围,xlim:设置x轴的范围ylim:设置y轴的范围autoscaleon,不能进行自动缩放ax。set(xlimxlim,ylimylim,autoscaleonFalse)backgroundimage设置背景色direction1为水平方向extent(0,1,0,1)代表百分之百transformax。transAxes代表用axes的坐标系cmap指定颜色cmaprange指定颜色范围gradientimage(ax,direction1,extent(0,1,0,1),transformax。transAxes,cmapplt。cm。RdYlGn,cmaprange(0。2,0。8),alpha0。5)代表要画的柱子数据N10代表每个柱子在x轴的坐标print(np。arange(10))xnp。arange(N)0。15代表每个柱子的高度ynp。random。rand(N)gradientbar(ax,x,y,width0。7)Settheaspectoftheaxisscaling设置轴的缩放:equal:samescalingforxandyauto:fllthepositionrectanglewithdataax。setaspect(auto)plt。show()