📜  Python中的 Matplotlib.patches.ConnectionPatch 类

📅  最后修改于: 2022-05-13 01:55:06.938000             🧑  作者: Mango

Python中的 Matplotlib.patches.ConnectionPatch 类

Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。

matplotlib.patches.ConnectionPatch

matplotlib.patches.ConnectionPatch 是 matplotlib.patches.FancyArrowPatch 类的子类,用于制作两点之间的连接线。

以下是有效的 Kwargs 密钥列表;

KeyDescription
arrowstylethe arrow style
connectionstylethe connection style
relposdefault is (0.5, 0.5)
patchAdefault is bounding box of the text
patchBdefault is None 
 
shrinkAdefault is 2 points
shrinkBdefault is 2 points
mutation_scaledefault is text size (in points)
mutation_aspectdefault is 1.
?any key for matplotlib.patches.PathPatch

xyA 和 xyB 的坐标由字符串coordsA 和 coordsB 表示。

PropertyDescription
‘figure points’points from the lower left corner of the figure
‘figure pixels’pixels from the lower left corner of the figure
‘figure fraction’0, 0 is lower left of figure and 1, 1 is upper right
‘axes points’points from lower left corner of axes
‘axes pixels’pixels from lower left corner of axes
‘axes fraction’0, 0 is lower left of axes and 1, 1 is upper right
‘data’use the coordinate system of the object being annotated (default)
‘offset points’offset (in points) from the xy value
‘polar’you can specify theta, r for the annotation, even in cartesian plots. Note that if you are using a polar axes, you do not need to specify polar for the coordinate system since that is the native “data” coordinate system.

示例 1:

Python3
from matplotlib.patches import ConnectionPatch
import matplotlib.pyplot as plt
 
fig, (ax1, ax2) = plt.subplots(1, 2,
                               figsize =(6, 3))
 
# Draw a simple arrow between
# two points in axes coordinates
# within a single axes.
xyA = (0.2, 0.2)
xyB = (0.8, 0.8)
coordsA = "data"
coordsB = "data"
con = ConnectionPatch(xyA, xyB,
                      coordsA, coordsB,
                      arrowstyle ="-|>",
                      shrinkA = 5, shrinkB = 5,
                      mutation_scale = 20,
                      fc ="w")
 
ax1.plot([xyA[0], xyB[0]], [xyA[1],
                            xyB[1]], "o")
ax1.add_artist(con)
 
# Draw an arrow between the
# same point in data coordinates,
# but in different axes.
xy = (0.3, 0.2)
con = ConnectionPatch(
    xyA = xy, coordsA = ax2.transData,
    xyB = xy, coordsB = ax1.transData,
    arrowstyle ="->", shrinkB = 5)
 
ax2.add_artist(con)
 
# Draw a line between the different
# points, defined in different coordinate
# systems.
con = ConnectionPatch(
    # in axes coordinates
    xyA =(0.6, 1.0), coordsA = ax2.transAxes,
    # x in axes coordinates, y in data coordinates
    xyB =(0.0, 0.2), coordsB = ax2.get_yaxis_transform(),
    arrowstyle ="-")
 
ax2.add_artist(con)
 
ax1.set_xlim(0, 1)
ax1.set_ylim(0, 1)
ax2.set_xlim(0, .5)
ax2.set_ylim(0, .5)
 
plt.show()


Python3
import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
import numpy as np
 
 
# make figure and assign axis
# objects
fig = plt.figure(figsize =(9, 5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
fig.subplots_adjust(wspace = 0)
 
# pie chart parameters
ratios = [.27, .56, .17]
explode = [0.1, 0, 0]
 
# rotate so that first wedge is
# split by the x-axis
angle = -180 * ratios[0]
ax1.pie(ratios, autopct ='% 1.1f %%',
        startangle = angle,
        explode = explode)
 
# bar chart parameters
 
xpos = 0
bottom = 0
ratios = [.33, .54, .07, .06]
width = .2
colors = [[.1, .3, .5],
          [.1, .3, .3],
          [.1, .3, .7],
          [.1, .3, .9]]
 
for j in range(len(ratios)):
    height = ratios[j]
    ax2.bar(xpos, height, width,
            bottom = bottom,
            color = colors[j])
     
    ypos = bottom + ax2.patches[j].get_height() / 2
    bottom += height
    ax2.text(xpos,
             ypos,
             "% d %%" % (ax2.patches[j].get_height() * 100),
             ha ='center')
 
ax2.set_title('')
ax2.legend(('50-65', 'Over 65', '35-49', 'Under 35'))
ax2.axis('off')
ax2.set_xlim(- 2.5 * width, 2.5 * width)
 
# use ConnectionPatch to draw
# lines between the two plots
# get the wedge data
theta1, theta2 = ax1.patches[0].theta1, ax1.patches[0].theta2
center, r = ax1.patches[0].center, ax1.patches[0].r
bar_height = sum([item.get_height() for item in ax2.patches])
 
# draw top connecting line
x = r * np.cos(np.pi / 180 * theta2) + center[0]
y = np.sin(np.pi / 180 * theta2) + center[1]
con = ConnectionPatch(xyA =(-width / 2, bar_height),
                      coordsA = ax2.transData,
                      xyB =(x, y),
                      coordsB = ax1.transData)
 
con.set_color([0, 0, 0])
con.set_linewidth(4)
ax2.add_artist(con)
 
# draw bottom connecting line
x = r * np.cos(np.pi / 180 * theta1) + center[0]
y = np.sin(np.pi / 180 * theta1) + center[1]
 
con = ConnectionPatch(xyA =(-width / 2, 0),
                      coordsA = ax2.transData,
                      xyB =(x, y),
                      coordsB = ax1.transData)
 
con.set_color([0, 0, 0])
ax2.add_artist(con)
con.set_linewidth(4)
 
plt.show()


输出:

示例 2:

Python3

import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
import numpy as np
 
 
# make figure and assign axis
# objects
fig = plt.figure(figsize =(9, 5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
fig.subplots_adjust(wspace = 0)
 
# pie chart parameters
ratios = [.27, .56, .17]
explode = [0.1, 0, 0]
 
# rotate so that first wedge is
# split by the x-axis
angle = -180 * ratios[0]
ax1.pie(ratios, autopct ='% 1.1f %%',
        startangle = angle,
        explode = explode)
 
# bar chart parameters
 
xpos = 0
bottom = 0
ratios = [.33, .54, .07, .06]
width = .2
colors = [[.1, .3, .5],
          [.1, .3, .3],
          [.1, .3, .7],
          [.1, .3, .9]]
 
for j in range(len(ratios)):
    height = ratios[j]
    ax2.bar(xpos, height, width,
            bottom = bottom,
            color = colors[j])
     
    ypos = bottom + ax2.patches[j].get_height() / 2
    bottom += height
    ax2.text(xpos,
             ypos,
             "% d %%" % (ax2.patches[j].get_height() * 100),
             ha ='center')
 
ax2.set_title('')
ax2.legend(('50-65', 'Over 65', '35-49', 'Under 35'))
ax2.axis('off')
ax2.set_xlim(- 2.5 * width, 2.5 * width)
 
# use ConnectionPatch to draw
# lines between the two plots
# get the wedge data
theta1, theta2 = ax1.patches[0].theta1, ax1.patches[0].theta2
center, r = ax1.patches[0].center, ax1.patches[0].r
bar_height = sum([item.get_height() for item in ax2.patches])
 
# draw top connecting line
x = r * np.cos(np.pi / 180 * theta2) + center[0]
y = np.sin(np.pi / 180 * theta2) + center[1]
con = ConnectionPatch(xyA =(-width / 2, bar_height),
                      coordsA = ax2.transData,
                      xyB =(x, y),
                      coordsB = ax1.transData)
 
con.set_color([0, 0, 0])
con.set_linewidth(4)
ax2.add_artist(con)
 
# draw bottom connecting line
x = r * np.cos(np.pi / 180 * theta1) + center[0]
y = np.sin(np.pi / 180 * theta1) + center[1]
 
con = ConnectionPatch(xyA =(-width / 2, 0),
                      coordsA = ax2.transData,
                      xyB =(x, y),
                      coordsB = ax1.transData)
 
con.set_color([0, 0, 0])
ax2.add_artist(con)
con.set_linewidth(4)
 
plt.show()

输出: