python - 如何在matplotlib中正确绘制带有旋转矩形的图形棒?

标签 python matplotlib rectangles

我正在尝试弄清楚如何正确绘制以下内容。

假设我有两个节点,即 R^2 中的 2 个点,即 src=(x1,y1) 和 dst=(x2,y2)。

我想在它们之间画一条边缘/线,但从一个盒子开始。

这是我用 Tikz 创建的示例: enter image description here

src 和 dest 是这些节点的中心。请注意,它们也可以处于不同的高度。问题是当我尝试构建盒子(使用 patch.Rectangle)时,我必须旋转它,然后它会改变它的大小。

提前致谢

最佳答案

如果您愿意对图形使用填充矩形,并用旋转矩形作为棒,则可以通过一条简单的线来模拟,该线的粗度与图形边缘的散点一样。

import matplotlib.pyplot as plt
import numpy as np

def plot_spec_line(src, dst, length=0.3, srctext="v", dsttext="w", 
                   ax=None, color="k", textcolor="k"):
    if not ax: ax=plt.gca()
    lend=src+(dst-src)*length
    ax.plot([src[0],lend[0]], [src[1],lend[1]], lw=24,solid_capstyle="butt", zorder=1, color=color )
    ax.plot([src[0],dst[0]], [src[1],dst[1]], lw=2,solid_capstyle="butt", zorder=0, color=color)
    ax.scatter([src[0],dst[0]], [src[1],dst[1]], s=24**2, marker="o", lw=2, edgecolors=color, c="w", zorder=2)
    ax.text(src[0],src[1], srctext, fontsize=12, ha="center", va="center", zorder=3, color=textcolor)
    ax.text(dst[0],dst[1], dsttext, fontsize=12, ha="center", va="center", zorder=3, color=textcolor)

s = np.array([1,1])
d = np.array([3,2])    
plot_spec_line(s, d, length=0.3, srctext="v", dsttext="w")

s = np.array([1.5,0.9])
d = np.array([2.8,1.2])    
plot_spec_line(s, d, length=0.2, srctext="a", dsttext="b", color="gray")

s = np.array([1,2])
d = np.array([2,1.9])    
plot_spec_line(s, d, length=0.7, srctext="X", dsttext="Y", textcolor="limegreen", color="limegreen")

plt.margins(0.2)
plt.show()

enter image description here

<小时/> 要获得一个可以填充或不填充的矩形,更重要的是,它有一个边缘,您可以使用 Rectangle。 您可以rotate a rectangle通过设置适当的变换。 然后,使用相等纵横比的绘图是有意义的,这样矩形和圆形就不会倾斜。

import matplotlib.pyplot as plt
import matplotlib.transforms
import numpy as np

def plot_spec_line(src, dst, length=0.3, radius=0.1, srctext="v", dsttext="w", 
                   ax=None, color="k", textcolor="k", reccolor="w", lw=1 ):
    if not ax: ax=plt.gca()
    lend=np.sqrt(np.sum(((dst-src)*length)**2))
    s = dst-src
    angle = np.rad2deg(np.arctan2(s[1],s[0]))
    delta = np.array([0,radius])
    tr = matplotlib.transforms.Affine2D().rotate_deg_around(src[0],src[1], angle)
    t = tr + ax.transData
    rec = plt.Rectangle(src-delta, width=lend, height=radius*2, ec=color,facecolor=reccolor, transform=t, linewidth=lw)
    ax.add_patch(rec)
    ax.plot([src[0],dst[0]], [src[1],dst[1]], lw=lw,solid_capstyle="butt", zorder=0, color=color)
    circ1= plt.Circle(src, radius=radius, fill=True, facecolor="w", edgecolor=color, lw=lw)
    circ2= plt.Circle(dst, radius=radius, fill=True, facecolor="w", edgecolor=color, lw=lw)
    ax.add_patch(circ1)
    ax.add_patch(circ2)
    ax.text(src[0],src[1], srctext, fontsize=12, ha="center", va="center", zorder=3, color=textcolor)
    ax.text(dst[0],dst[1], dsttext, fontsize=12, ha="center", va="center", zorder=3, color=textcolor)

s = np.array([1,1])
d = np.array([3,2])    
plot_spec_line(s, d, length=0.3, srctext="v", dsttext="w", radius=0.06,
               reccolor="plum", )

s = np.array([1.5,0.9])
d = np.array([2.8,1.2])    
plot_spec_line(s, d, length=0.2, srctext="a", dsttext="b", color="gray", lw=2.5)

s = np.array([1,2])
d = np.array([2,1.9])    
plot_spec_line(s, d, length=0.7, srctext="X", dsttext="Y", textcolor="limegreen",
               reccolor="palegreen", color="limegreen")

plt.margins(0.2)
plt.gca().set_aspect("equal")
plt.show()

enter image description here

关于python - 如何在matplotlib中正确绘制带有旋转矩形的图形棒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44910868/

相关文章:

python - 正则表达式捕获给定字符串 anchor 之间的字符串

python - 正确的多处理以在 Python 中处理 UDP

algorithm - 如何检查矩形和多边形的交集?

coordinates - 如何在 OpenLayers 中获取绘制框的坐标?

python - Pandas 获得列平均值/平均值

python - 重新排列点列表以达到它们之间的最短距离

python - 查找峰的半峰全宽

python - 绘制最近质心的 ROC 曲线

python - 使用 Pyinstaller 从 py 文件创建 exe 时找不到 Matplotlib 目录

python - 最大矩形算法