Generate Random Circle with Matplotlib and Numpy

  • Share this:

Code introduction


This function uses the matplotlib and numpy libraries to draw a circle with the center at (x_center, y_center) and a radius of radius.


Technology Stack : matplotlib library for plotting, numpy library for mathematical calculations.

Code Type : Function

Code Difficulty : Intermediate


                
                    
def generate_random_circle(x_center, y_center, radius):
    import matplotlib.pyplot as plt
    import numpy as np

    fig, ax = plt.subplots()
    t = np.linspace(0, 2 * np.pi, 100)
    x = x_center + radius * np.cos(t)
    y = y_center + radius * np.sin(t)

    ax.plot(x, y)
    ax.set_xlim(x_center - radius - 1, x_center + radius + 1)
    ax.set_ylim(y_center - radius - 1, y_center + radius + 1)
    ax.set_aspect('equal', adjustable='box')
    plt.show()