import numpy as np
import matplotlib.pyplot as plt
radius=1
num_points=50
theta=np.linspace(0,2*np.pi,num_points)
x=radius*np.cos(theta)
y=radius*np.sin(theta)
fig,ax=plt.subplots()
ax.scatter(x,y,color='b',s=50)
ax.set_xlim(-1.2,1.2)
ax.set_ylim(-1.2,1.2)
ax.set_aspect('equal')
ax.axis('off')
plt.title("Circle pattern plot")
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Importing Required Libraries
import numpy as np
import matplotlib.pyplot as plt
numpy is used to generate numerical data, such as evenly spaced angles for plotting points.
matplotlib.pyplot is used to create the visualization.
2. Defining Circle Parameters
radius = 1
num_points = 50 # Number of dots
theta = np.linspace(0, 2 * np.pi, num_points) # Angles for the circle
radius = 1: The radius of the circle.
num_points = 50: The number of dots to be placed around the circle.
np.linspace(0, 2 * np.pi, num_points):
This creates num_points evenly spaced values from 0 to 2π.
2π radians correspond to a full circle (360°).
3. Computing Circle Coordinates
x = radius * np.cos(theta)
y = radius * np.sin(theta)
np.cos(theta): Computes the x-coordinates of the points along the circle.
np.sin(theta): Computes the y-coordinates of the points along the circle.
Multiplying by radius ensures the points are scaled correctly.
4. Creating the Plot
fig, ax = plt.subplots()
ax.scatter(x, y, color='b', s=50) # Scatter plot for dots
fig, ax = plt.subplots(): Creates a figure (fig) and an axis (ax) to plot on.
ax.scatter(x, y, color='b', s=50):
Uses scatter() to plot individual dots at (x, y).
color='b': Dots are blue (b for blue).
s=50: Size of each dot.
5. Setting Limits and Aspect Ratio
ax.set_xlim(-1.2, 1.2)
ax.set_ylim(-1.2, 1.2)
ax.set_aspect('equal')
ax.axis('off') # Hide axes
ax.set_xlim(-1.2, 1.2): Sets x-axis limits slightly beyond the circle's radius for better visibility.
ax.set_ylim(-1.2, 1.2): Sets y-axis limits similarly.
ax.set_aspect('equal'): Ensures the aspect ratio is equal so the circle does not appear distorted.
ax.axis('off'): Hides the axes for a cleaner look.
6. Displaying the Plot
plt.show()
Displays the generated dotted circle.
0 Comments:
Post a Comment