How to "translate" the Midpoint Circle Algorithm into matplotlib?
<p>I need to implement the <a href="https://en.wikipedia.org/wiki/Midpoint_circle_algorithm" rel="nofollow noreferrer">Midpoint Circle Algorithm</a> in <code>matplotlib</code>, so that a circle is rasterized on a square grid of <code>200x200</code> cells. Please also refer to my <a href="https://stackoverflow.com/questions/35522530/python-how-to-create-a-submatrix-discretizing-a-circle">other question</a> on the subject.</p>

<p>Thanks to <a href="https://stackoverflow.com/questions/31519302/circle-in-pixel-grid?answertab=active#tab-top">this answer</a>, I was able to find some code which I assume works flawlessly. My only problem is that I wouldn't know how to integrate it and amend it to make sure <code>matplotlib</code> draws a <strong>filled circle</strong> with <code>1</code> inside and <code>0</code> outside.</p>

<p>This is how I implement the script with <code>matplotlib</code>:</p>

<pre><code>import numpy
import matplotlib.pyplot as plt    
n=200 #Grid size, 4 times my visualized output in order to be able to truncate some circles
empty_lattice=numpy.zeros((n,n)) #The empty 2D grid
radius=int(numpy.random.uniform(30,90)) #Radius
xc=int(numpy.random.uniform(0,n-radius)) #X center
yc=int(numpy.random.uniform(0,n-radius)) #Y center
x=0
y=radius
d=3-2*radius
while (x&lt;=y):
    for hor in range(0,x): #This loop is my unfortunate attempt to fill the circle with 1s
        for ver in range(0,y):
            empty_lattice[xc+x][yc+y]=1 #1st octant
            empty_lattice[xc-x][yc+y]=1 #2nd octant
            empty_lattice[xc+x][yc-y]=1 #3rd octant
            empty_lattice[xc-x][yc-y]=1 #4th octant
            empty_lattice[xc+y][yc+x]=1 #5th octant
            empty_lattice[xc-y][yc+x]=1 #6th octant
            empty_lattice[xc+y][yc-x]=1 #7th octant
            empty_lattice[xc-y][yc-x]=1 #8th octant
    if (d&lt;0):
        d=d+4*x+6
    else:
        d=d+4*(x-y)+10
        y=y-1
    x=x+1           
</code></pre>

<p>Now, this is what I obtain, but you see that my circle is empty and that there is a "gap" in the horizontal line at the bottom of it. This gap occurs at each octant intersection. <strong>How can I amend my code to fill the circle and the gap?</strong></p>

<p><a href="https://i.stack.imgur.com/FJKuN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FJKuN.png" alt="enter image description here"></a></p>

<p><strong>EDIT</strong></p>

<p>After having adopted <a href="https://stackoverflow.com/questions/35539471/how-to-translate-the-midpoint-circle-algorithm-into-matplotlib?answertab=active#tab-top">the answer provided below</a>, I have realized that two mirrored circles are drawn in the image below. I think this bug came with my original script and not with the answer. I only want one circle in each image. <strong>How can I get rid of this feature?</strong></p>

<p><a href="https://i.stack.imgur.com/78ego.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/78ego.png" alt="enter image description here"></a></p>