绘制两个圆圈之间的椭圆线不交叉matplotlib

问题描述 投票:4回答:1

我想提请使用matplotlib连接两个圆的椭圆线,但想使它这样的椭圆形线没有相交要么圈。

目前我的设计导致了这一点:enter image description here

正如你可以看到有行通过两个圆圈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得到:enter image description here

如果我放大这一领域,虽然它很容易看到它不排队:enter image description here

我的问题就变成了,我应该怎么计算adjustment应该是什么?

我想我可以,如果我认为这是一个完整的椭圆计算周长和减去圆圈中的一个重叠量,并用它来获得adjustment,但我不知道是否会工作或如何计算多少内重叠。任何帮助,将不胜感激。

python python-3.x python-2.7 matplotlib ellipse
1个回答
4
投票

而不是手动调节数字,可以考虑使用zorderPatch构造。

在一块土地上的群星都在彼此垂直堆叠,与那些在顶部最高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))

这产生

.

© www.soinside.com 2019 - 2024. All rights reserved.