Search code examples
numpymatplotlibscatter-plot

Is there a way to connect the points of the outer ring to the inner ring in matplotlib?


I have a code that is able to plot the points of two circles with different radii on a plot, but I am having issues connecting the outer and inner circles with individual lines. I need to connect the ith outer point to the ith corresponding inner point which are contained in a single list and on a range to n. I'm just stuck on how to do this and would appreciate any guidance.

import numpy as np
import matplotlib.pyplot as plt
def circle_points(r, n):
    points = []
    for r, n in zip(r, n):
        t = np.linspace(0, 2*np.pi, n, endpoint=True) #finding evenly spaced points from 0 to n 
        h = (2*np.pi*r)/n #space between each point in meters
        x = r * np.sin(t) #converting to circle coordinate on x-axis
        y = r * np.cos(t)
        for i in range(n):
            points.append([x[i],y[i]])
    return points
r = [0.1944,0.1444] #radius in meters
n = [30,30] #amount of points in circle
x,y=zip(*circle_points(r,n))
plt.scatter(x,y)
plt.plot(x,y)
plt.axis('equal')
plt.show()

Current result:

Current Result


Solution

  • The coordinates x and y of the double circle have already been created. Since the first half of the array is the outer circle and the second half is the inner circle, we will draw a line segment by combining the first data of the outer circle with the first data of the inner circle.

    import numpy as np
    import matplotlib.pyplot as plt
    
    def circle_points(r, n):
        points = []
        for r, n in zip(r, n):
            t = np.linspace(0, 2*np.pi, n, endpoint=True) #finding evenly spaced points from 0 to n 
            h = (2*np.pi*r)/n #space between each point in meters
            x = r * np.sin(t) #converting to circle coordinate on x-axis
            y = r * np.cos(t)
            for i in range(n):
                points.append([x[i],y[i]])
            print(points)
        return points
    
    r = [0.1944,0.1444] #radius in meters
    n = [30,30] #amount of points in circle
    x,y=zip(*circle_points(r,n))
    plt.scatter(x,y)
    plt.plot(x,y)
    # add line
    for i in range(30):
        plt.plot([x[i], x[30+i]], [y[i], y[30+i]], color='red')
    
    plt.axis('equal')
    
    plt.show()
    

    enter image description here