簡體   English   中英

connectionstyle arc3(FancyArrowPatch)和路徑CURVE3-兩個二次貝塞爾曲線,結果不同嗎?

[英]connectionstyle arc3 (FancyArrowPatch) and path CURVE3 - two quadratic Bezier curves, different outcomes?

我從這里提到的類似問題開始: 在matplotlib中用中點箭頭繪制圓形的fancyarrowpatch

只是我想在箭頭上添加標簽,最好在可變的地方。 我試圖用路徑頂點重建坐標,但是標簽仍然到處都是。 然后,我嘗試遵循connection3 arc3的描述

https://matplotlib.org/api/_as_gen/matplotlib.patches.ConnectionStyle.html?highlight=connectionstyle

類Arc3(rad = 0.0)

在兩點之間創建簡單的二次貝塞爾曲線。 創建曲線以使中間控制點(C1)距起點(C0)和終點(C2)的距離相同,並且C1到連接C0-C2的線的距離為rad乘以距離C0-C2。

我對此進行了重構,並注意到FancyArrowPatch繪制的曲線與具有指定點的二次Bezier曲線不同。

最小示例:

import numpy
import matplotlib
from matplotlib import pyplot as plt
import matplotlib.path as mpath
import matplotlib.patches as mpatches
from matplotlib.patches import FancyArrowPatch, Circle

fig = plt.figure(frameon=False)
ax = fig.add_subplot(111, frame_on=False)

rad=0.3
shrink=0.3

# Setting up circles as start and end points
size = 0.1
n1 = Circle([0,0], radius=size, alpha=0., fill=False, linewidth=0.1)
n2 = Circle([1,1], radius=size, alpha=0., fill=False, linewidth=0.1)
ax.add_patch(n1)
ax.add_patch(n2)

# Create a fancy arrow between start and end point
e = FancyArrowPatch(n1.center, n2.center,
    connectionstyle='arc3,rad=%s' % rad,
    arrowstyle='simple',
    clip_on=False,
    linewidth=2.,
    shrinkA=shrink,shrinkB=shrink
    )

ax.add_patch(e)

# Start point
vs=numpy.asarray(n1.center)
# End point
ve=numpy.asarray(n2.center)
# Connection vector start->end
vD=ve-vs
# Perpendicular vector to vD
vp=numpy.asarray([vD[1],-vD[0]]) 
# Control point: same distance from start and end point, and rad*|vD| from the connection line between start and end
vc=vs+0.5*vD+rad*vp 

Path=mpath.Path
pp1 = mpatches.PathPatch(
    Path([vs, vc, ve],
     [Path.MOVETO, Path.CURVE3, Path.CURVE3]), color="red", transform=ax.transData,fc='None')

ax.add_patch(pp1)

# Putting labels on quadratic Bezier curve at various points
# Uses start point, control point and end point from above
for tt in numpy.arange(0,1.1,0.1):
    vl=((1-tt)**2)*vs+(2*(1-tt)*tt)*vc+(tt**2)*ve
    string = str(tt)
    ax.text(vl[0], vl[1], string,
        fontsize=10,bbox={'alpha':0.5, 'pad':2},
        verticalalignment='center',
        horizontalalignment='center')

plt.show()

這使

https://i.imgur.com/6XD8txW.png

黑線:FancyArrowPatch(不知道為什么沒有箭頭)

紅線:PathPatch

藍色標簽:(0,0)和(1,1)之間的二次貝塞爾曲線上的點,控制點距起點和終點的距離相同,而rad *(起點和終點的距離)距起點和終點之間的連接線。 (如connectionstyle arc3中所述)。

(不幸的是,我不能只給FancyArrowPatch構造路徑,因為我需要它的收縮/補丁選項。)

所以問題是我如何可靠地在箭頭上放置標簽,以及FancyArrowPatch實際上是如何構建其路徑的。

(我知道我可以返回頂點,但是我對不同的箭頭長度使用相同的代碼(只有起點和終點不同),並且這些路徑看起來彼此非常不同)。

您創建為Path的曲線位於數據空間中。 自動生成的FancyArrowPatch曲線位於顯示空間中。 這意味着僅當數據空間中的點之間的距離與顯示空間中的點之間的距離成比例時,它們才相等。 通常只有軸長寬比相等時才是這種情況。

這在某種程度上類似於在數據坐標中定義的圓的更直觀(相反)的情況。 在這種情況下,文檔會說“畫一個圓,即到任何地方到給定中心點的距離相同的線”。 但是,如果繪制圓,則除非將軸的長寬比設置為1,否則它看起來像是橢圓形。

確實,在設置ax.set_aspect("equal") ,在上述情況下

在此處輸入圖片說明

由於Arc3本身對使用的坐標系Arc3 ,因此Arc3文檔是完全正確的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM