Home > other >  How to stop plotting when newly iterated scatter point is plotted on aleardy plotted scatter point?
How to stop plotting when newly iterated scatter point is plotted on aleardy plotted scatter point?

Time:09-20

I am writing a code where I want the scatter plot to stop as soon as my code tries to plot a scatter point on an already existing scatter point.

This is the code I wrote that works for a divisor of 360,

r = 2
pi = np.pi
i = 0
theta = 10
for i in range(100):
    x = r*np.cos(theta  i*theta )
    y = r*np.sin(theta  i*theta )
    plt.scatter(x,y, label = i 1)
    if (theta  i*theta == 360):
        print(i)
        break

when theta = 10, the plotting process stops at i = 35

Plot_1

but rather than having my if condition as "stop code when theta is equal to 360", I need a code when code stops when a scatter point overlaps another, since this condition does not work for other integers.

CodePudding user response:

I think the following code

x = r*np.cos(theta  i*theta ) 
y = r*np.sin(theta  i*theta ) 

should be written as

x = r*np.cos(np.deg2rad(theta  i*theta )) 
y = r*np.sin(np.deg2rad(theta  i*theta )) 

then you can change theta to any other value when using if (theta i*theta == 360) to avoid scatters overlapping.

In the following code, I changed theta from 10 to 5, and used red text to show the overlapping points.

import numpy as np
import matplotlib.pyplot as plt
r = 2
pi = np.pi
i = 0
theta = 5
nloop = 100

for i in range(nloop):
#     x = r*np.cos(theta  i*theta )
#     y = r*np.sin(theta  i*theta )
    x = r*np.cos(np.deg2rad(theta  i*theta ))
    y = r*np.sin(np.deg2rad(theta  i*theta ))
    plt.scatter(x,y, label = i 1)
    if (theta  i*theta == 360):
        break


ax = plt.gca()
fig = plt.gcf()
fig.set_size_inches(12,12)
ax.set_aspect(1) 
#### the code below only for debug
for i in range(nloop):
    x = r*np.cos(np.deg2rad(theta  i*theta ))
    y = r*np.sin(np.deg2rad(theta  i*theta ))
    
    if (theta  i*theta >= 360 theta):
        ax.text(x,y, f'{i}',ha="right",fontsize=12,color='r')
    else:
        ax.text(x,y ,f'{i}',ha="left",fontsize=12)

enter image description here

  • Related