matplotlib日常练习(4)

Path 的应用

import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
# Define the points along with first curve to be drawn
verts1 = [(-1.5, 0.),        # left, bottom
          (0., 1.),          # left, top
          (1.5, 0.),         # right, top
          (0., -1.0),        # right, bottom
          (-1.5, 0.)]        # ignored
# How to draw the plot along above points
codes1 = [Path.MOVETO,       # Go to first point specified in vert1
         Path.LINETO,        # Draw a line from first point to second point
         Path.LINETO,        # Draw another line from current point to next point
         Path.LINETO,        # Draw another line from current point to next point
         Path.CLOSEPOLY]     # Close the loop
# Create complete path with points and lines/curves
path1 = Path(verts1, codes1)
# Repeat the same for 2nd curve
verts2 = [(-1.5, 0.),        # left, bottom
          (0., 2.5),         # left, top
          (1.5, 0.),         # right, top
          (0., -2.5),        # right, bottom
          (-1.5, 0.)]        # ignored
codes2 = [Path.MOVETO,       # Move to the first point
          Path.CURVE3,       # Curve from first point along the control point(2nd point) and terminate on end point(3rd)
          Path.CURVE3,       # Curve from current point along the control point(next point) and terminate on end point(next)
          Path.CURVE3,
          Path.CURVE3]       # close by the curved loop
path2 = Path(verts2, codes2)
# Define the figure
fig = plt.figure()
ax = fig.add_subplot(111)
# zorder overrides default order of plotting different patches. In this case we want second path to be plotted first and 
# then first patch so that both are visible. Otherwise smaller patch could hide behind larger one
patch1 = patches.PathPatch(path1, lw=4, zorder=2)
ax.add_patch(patch1)
patch2 = patches.PathPatch(path2, facecolor='orange', lw=2, zorder=1)
ax.add_patch(patch2)
# Set the limits for x and y axis
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
plt.show()


https://www.jianshu.com/p/41caeff82be2

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论