【问题标题】:Drawing elliptical lines between two circles without intersecting matplotlib在两个圆之间绘制椭圆线而不相交matplotlib
【发布时间】:2019-06-28 19:57:38
【问题描述】:

我正在尝试使用 matplotlib 绘制椭圆线来连接两个圆,但我想这样做,这样椭圆线不会与任何一个圆相交。

目前我的设计是这样的:

如您所见,它的线穿过圆 A 和 B。 我决定使用matplotlib.patches.Arc,因为我不想填充它,它允许我绘制左右部分。这是我所拥有的:

from matplotlib import pyplot
from matplotlib.patches import Arc
import math

def calculate_perimeter(a, b):
    perimeter = math.pi * (3*(a+b) - math.sqrt( (3*a + b) * (a + 3*b) ))
    return perimeter

def draw_circle(xy, radius, text):
    circle = pyplot.Circle(xy, radius=radius,     fill=False)
    pyplot.gca().add_patch(circle)
    pyplot.gca().annotate(text, xy=xy,     fontsize=10, ha='center', va='center')

def draw_arc(xy1, xy2, a, b, theta1, theta2):
    # Calculate center of the elliptical arc
    center = (xy1[0], (xy1[1] + xy2[1])/2.0)
    arc = Arc(center, a, b, theta1=theta1, theta2=theta2)
    pyplot.gca().add_patch(arc)

if __name__ == '__main__':
    pyplot.figure()
    center_circle1 = (5, 5)
    center_circle2 = (5, 20)
    dist_y = center_circle2[1] - center_circle1[1]
    adjustment = 5.3 # @TODO: How do I calculate what this needs to be?
    # Circles
    draw_circle(center_circle1, 1, 'A')
    draw_circle(center_circle2, 1, 'B')
    # Draw right side of arc
    theta1 = 270.0 + adjustment
    theta2 = 90.0 - adjustment
    draw_arc(center_circle1, center_circle2, 3, dist_y, theta1, theta2)
    # Draw left side of arc
    theta1 = 90.0 + adjustment
    theta2 = 270.0 - adjustment
    draw_arc(center_circle1, center_circle2, 3, dist_y, theta1, theta2)
    pyplot.axis('scaled')
    pyplot.axis('off')
    pyplot.show()

例如,当我输入 adjustment = 5.3 时,我得到:

如果我放大这个区域,虽然很容易看到它并没有对齐:

然后我的问题变成了,我如何计算 adjustment 应该是什么?

我认为如果我认为它是一个完整的椭圆并减去其中一个圆圈中重叠的量并使用它来获得adjustment,我将能够计算周长,但我不确定这是否会工作或如何计算内部有多少重叠。对此的任何帮助将不胜感激。

【问题讨论】:

    标签: python python-3.x python-2.7 matplotlib ellipse


    【解决方案1】:

    与其手动调整数字,不如考虑在Patch 构造函数中使用zorder

    一个情节中的各个艺术家垂直堆叠在一起,最高的zorder 位于顶部。因此,通过设置zorder,您将在椭圆上绘制圆,使其模糊。

    示例代码:

    from matplotlib import pyplot as plt
    from matplotlib.patches import Circle, Arc
    
    fig, ax = plt.subplots(figsize=(6, 6))
    
    ax.add_patch(Circle((0.5, 0.75), 0.05, edgecolor='black', facecolor='white', zorder=3))
    ax.add_patch(Circle((0.5, 0.25), 0.05, edgecolor='black', facecolor='white', zorder=3))
    ax.add_patch(Arc((0.5, 0.5), 0.1, 0.5))
    

    生成

    .

    【讨论】:

    • 这可能是 OP 要求的答案。否则,这更像是一道数学题,问如何计算圆与椭圆的交点所在的角度。这并不难,因为椭圆和圆的方程是已知的,它们可以相互求解以获得交点的坐标(然后只需要一些三角函数来获得所需的角度)。但这是一个属于math.stackexchange.com的问题
    • 谢谢。我想我不必总是尝试用数学来解决问题;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2013-11-14
    相关资源
    最近更新 更多