I have my data and codes for scatter plot as below:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# define X and y
np.random.seed(10)
x = np.arange(0,100)
y = np.random.randint(0,100,100)
df = pd.DataFrame(dict(x=x, y=y))
def dist(x,y):
p1=np.array([0,0])
p2=np.array([100,100])
p3=np.array([x,y])
return abs(np.cross(p2-p1,p3-p1)/np.linalg.norm(p2-p1))
max_dist = dist(0,10)
df["within_boundary"] = df.apply(lambda row: dist(row["x"], row["y"])<=max_dist, axis=1)
fig = plt.figure(dpi=100,figsize=(8,8))
ax = fig.subplots(1)
p0 = ax.scatter(x,y,c=df["within_boundary"]) # points
p1 = ax.plot([0,100],[0,100],'-',color="red") # middle line
p2, = ax.plot([0,90],[10,100],'--',color="black") # upper line
p3, = ax.plot([10,100],[0,90],'--',color="black") # lower line
plt.xticks(np.arange(0,101,10))
plt.yticks(np.arange(0,101,10))
plt.grid(True)
plt.xlim([0,100])
plt.ylim([0,100])
percentage =df['within_boundary'].sum() / len(x)
plt.figtext(0.5,0.01, f"{percentage:.1%} in Spec", ha="center", va="center", fontsize=18, bbox={"facecolor":"grey", "alpha":0.5})
plt.show() # show the window"
This I use to produce the scatter plot shown as:
The Tolerance region are in the +-10 and the In Spec of 17% is the number of points within the 10 region divided by the number of total points in the plot.
However I want to have a slicer that I can use to just filter on the plot to change from the 10 region to either 15, 20, 25, etc and even back to 10 and it will still automatically calculate the In Spec percentage when the region is adjusted on the slicer to 10, 15, 20, or whatever I want and also make the region lines to run from 15, 20 and so on automatically if I am having the slicer to 15, or 20 and so on.