Home > Enterprise >  Circles in matplotlib overlapping when they should not
Circles in matplotlib overlapping when they should not

Time:12-15

I am trying to plot two circles in python using matplotlib. I chose their positions such that they should not overlap. However, in the plot it looks like they do. Can anyone tell me where my mistake is?

overlapping circles

    import numpy as np
    import matplotlib.pyplot as plt
    plt.rcParams['figure.dpi'] = 5000

    radius = 0.0025
    positions = np.array([[0.5, 0.05], [0.5   2*radius   0.001, 0.05]])

    fig, ax = plt.subplots()
    ax.cla() 
    ax.set_xlim((0, 1.0))
    ax.set_ylim((0, 0.1))
    ax.set_aspect('equal')
    for n in range(len(positions)):
        circle = plt.Circle((positions[n, 0], positions[n, 1]), radius=radius,                              color='r')
        ax.add_patch(circle)
    plt.show()
    fig.savefig("circles.png")

many thanks in advance!

CodePudding user response:

The reason is the Circle patch is drawn as a face and an edge - the edge has a thickness, which is what is overlapping.

We can resolve this by setting the facecolor to red, and the edgecolor to None, so it doesn't get plotted.

For example:

circle = plt.Circle((positions[n, 0], positions[n, 1]), radius=radius,
                    facecolor='r', edgecolor='None')

enter image description here

  • Related