python - 在两个圆之间绘制椭圆线而不相交matplotlib
问题描述
我正在尝试使用 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
应该是什么?
我想如果我认为它是一个完整的椭圆并减去在其中一个圆圈中重叠的量并用它来得到 ,我将能够计算周长adjustment
,但我不确定这是否可行或如何计算里面有很多重叠。对此的任何帮助将不胜感激。
解决方案
与其手动调整图形,不如考虑zorder
在Patch
构造函数中使用。
一个情节上的各种艺术家垂直堆叠在一起,最高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))
这会产生
.
推荐阅读
- python - 安装公共 python 包时避免使用私有辅助 pypi 存储库的密码
- vba - VBA webscraper 因意外原因停止工作
- huggingface-transformers - 有没有办法只保存带有拥抱脸训练器的模型?
- python - Django 在many-2-many 中间表中创建了一个额外的不需要的条目
- python - 无法将项目添加到字典
- php - 如何在 HTML 表格中显示 JSON 数据
- r - 从 ggplot2 中的 scale_y_time() 格式化轴标签
- windows - 在 MASM 中编写 Hello World
- node.js - 如何限制对我的heroku应用原始域的访问(即app.herokudomain.com)
- apache-poi - Apache POI 迁移