代码之家  ›  专栏  ›  技术社区  ›  Simd

如何绘制环形而不是整个圆形部分?

  •  0
  • Simd  · 技术社区  · 2 年前

    此代码绘制一个网格,然后在网格上绘制两个不同大小的圆。

    from math import sqrt
    
    # Calculate the center coordinates and add blue dots
    for x in np.arange(0.5, 10, 1):
        for y in np.arange(0.5, 10, 1):
            plt.scatter(x, y, color='blue', s=10)  # Adjust the size (s) as needed
            
    # Draw a circle with center in the top left and radius to touch one of the blue dots
    d = 3
    
    circle1 = plt.Circle((0, 0), radius=sqrt(2) * (d + 0.5), color='red', alpha=0.5)
    plt.gca().add_patch(circle1)
    
    # Draw the second circle with a different radius
    circle2 = plt.Circle((0, 0), radius=sqrt(2) * (d + 1 + 0.5), color='blue', alpha=0.5)
    plt.gca().add_patch(circle2)
    print(f"radius of smaller circle is {sqrt(2) * (d +  0.5)}")
    print(f"radius of larger circle is {sqrt(2) * (d + 1 + 0.5)}")
    # Draw a square at (4, 4) with a really thick edge
    #square = patches.Rectangle((4, 4), 1, 1, fill=None, edgecolor='green', linewidth=5)
    #plt.gca().add_patch(square)
    plt.yticks(np.arange(0, 10.01, 1))
    plt.xticks(np.arange(0, 10.01, 1))
    plt.xlim(0,10)
    plt.ylim(0,10)
    # Manually set tick positions and labels
    
    plt.gca().invert_yaxis()
    # Set aspect ratio to be equal
    plt.gca().set_aspect('equal', adjustable='box')
    plt.grid() 
    

    这提供了:

    enter image description here

    我只想显示较大圆圈中的部分,而不是较小的部分。也就是说,蓝色的部分。我该怎么做?

    1 回复  |  直到 2 年前
        1
  •  1
  •   jared    2 年前

    您想要的是一个环面,可以使用 matplotlib.patches.Annulus .

    from matplotlib.patches import Annulus
    
    # other code but I removed the circles
    
    r1 = sqrt(2) * (d + 0.5)
    r2 = sqrt(2) * (d + 1 + 0.5)
    annulus1 = Annulus((0, 0), r2, r2 - r1, color="blue", alpha=0.5)
    plt.gca().add_patch(annulus1)
    
    # other code for setting ticks and showing the plot