Note

Go to the endto download the full example code.

Filled polygon#

fill() draws a filled polygon based on lists of pointcoordinatesx,y.

This example uses theKoch snowflake as an example polygon.

importmatplotlib.pyplotaspltimportnumpyasnpdefkoch_snowflake(order,scale=10):"""    Return two lists x, y of point coordinates of the Koch snowflake.    Parameters    ----------    order : int        The recursion depth.    scale : float        The extent of the snowflake (edge length of the base triangle).    """def_koch_snowflake_complex(order):iforder==0:# initial triangleangles=np.array([0,120,240])+90returnscale/np.sqrt(3)*np.exp(np.deg2rad(angles)*1j)else:ZR=0.5-0.5j*np.sqrt(3)/3p1=_koch_snowflake_complex(order-1)# start pointsp2=np.roll(p1,shift=-1)# end pointsdp=p2-p1# connection vectorsnew_points=np.empty(len(p1)*4,dtype=np.complex128)new_points[::4]=p1new_points[1::4]=p1+dp/3new_points[2::4]=p1+dp*ZRnew_points[3::4]=p1+dp/3*2returnnew_pointspoints=_koch_snowflake_complex(order)x,y=points.real,points.imagreturnx,y

Basic usage:

x,y=koch_snowflake(order=5)plt.figure(figsize=(8,8))plt.axis('equal')plt.fill(x,y)plt.show()
fill

Use keyword argumentsfacecolor andedgecolor to modify the colorsof the polygon. Since thelinewidth of the edge is 0 in the defaultMatplotlib style, we have to set it as well for the edge to become visible.

x,y=koch_snowflake(order=2)fig,(ax1,ax2,ax3)=plt.subplots(1,3,figsize=(9,3),subplot_kw={'aspect':'equal'})ax1.fill(x,y)ax2.fill(x,y,facecolor='lightsalmon',edgecolor='orangered',linewidth=3)ax3.fill(x,y,facecolor='none',edgecolor='purple',linewidth=3)plt.show()
fill

References

The use of the following functions, methods, classes and modules is shownin this example:

Tags:styling: shapelevel: beginnerpurpose: showcase

Total running time of the script: (0 minutes 1.331 seconds)

Gallery generated by Sphinx-Gallery