如何更改 matplotlib 中选定点的颜色?
How do I change color of the selected points in matplotlib?
我编写了这段代码,允许用户在图表上 select 4 个点,当这 4 个点被 select 编辑时,绘制了两条线。
但我希望用户能够通过更改点的颜色或在其周围画一个小圆圈来查看她 selected 了哪些点。
我尝试阅读 matplotlib 文档,但无法理解我的代码中 add/change 的内容。
我只想更改 selected 点的颜色或在 selected 点周围画一个小圆圈。
我该怎么做?
#ONCLICK Event which lets user select 4 points on the graph and stores the coordinates of the
points in coords
coords=[]
def onclick(self,event):
global ix, iy, fig
ix, iy = event.xdata, event.ydata
print ('x = %d, y = %d'%(ix, it))
#I am not able to plot this circle
#plt.Circle((ix,iy), radius=0.07, color='g')
event.canvas.draw()
self.coords.append(ix)
self.coords.append(iy)
print(len(self.coords))
if len(self.coords) == 8:
fig.canvas.mpl_disconnect(cid)
plt.close(1)
s=self.coords[0]
t=self.coords[1]
u=self.coords[2]
v=self.coords[3]
w=self.coords[4]
x=self.coords[5]
y=self.coords[6]
z=self.coords[7]
line1= '(' + str("{:.2f}".format(s))+',' + str("{:.2f}".format(t)) + ')' + ',' + '(' +str("{:.2f}".format(u)) +',' + str("{:.2f}".format(v)) + ')'
line2= '(' + str("{:.2f}".format(w))+',' + str("{:.2f}".format(x)) + ')' + ',' + '(' +str("{:.2f}".format(y)) +',' + str("{:.2f}".format(z)) + ')'
df['Parameter']=df['Parameter'].astype('float')
df['Step']=df['Step'].astype('float')
a = df['Step']
b = df['Parameter']
#LINE EQUATION
m1=(v-t)/(u-s)
c1=t-m1*s
x1=np.linspace(df.iloc[0]["Step"],df.iloc[samples-1]["Step"],100)
y1=m1*x1+c1
m2=(z-x)/(y-w)
c2=x-m2*w
x2=np.linspace(df.iloc[1]["Step"],df.iloc[samples-1]["Step"],100)
y2=m2*x2 +c2
#INTERSECTION
det=m2-m1
if det==0:
print("Lines are parallel\n")
else:
pointx=(c1-c2)/det
pointy=((c1*m2)-(m1*c2))/det
#PLOTTING
fig = plt.figure()
ax = plt.axes()
plt.plot(a, b, '.',color='yellow')
plt.plot(x1,y1,color='red',label=line1)
plt.plot(x2,y2,color='black',label=line2)
plt.xlabel('Volume')
plt.ylabel('Conductivity')
ax.set_ylim(bottom=0)
ax.set_xlim(left=0.)
idx = np.argwhere(np.diff(np.sign(y2 - y1))).flatten()
plt.title("END POINT")
plt.plot(x2[idx], y2[idx], 'ro',color='Purple')
plt.annotate("Intersecting Point", (pointx,pointy),color='Blue')
plt.plot((pointx,pointx),(pointy,0),'--',color='purple')
plt.annotate("(%s,0) Volume" %pointx, (pointx,0),color='Blue')
mplcursors.cursor(hover=True)
plt.legend()
plt.show()
return
#Main graph which appears when a button in the interface is clicked.
def manual(self):
df['Parameter']=df['Parameter'].astype('float')
df['Step']=df['Step'].astype('float')
a = df['Step']
b = df['Parameter']
df['Parameter']=df['Parameter'].astype('float')
df['Step']=df['Step'].astype('float')
a=df['Step']
b=df['Parameter']
global fig
fig=plt.figure()
plt.xlabel('Volume')
plt.ylabel('Conductivity')
plt.title('SCATTER PLOT')
plt.scatter(a, b,color='r', label=self.Name)
mplcursors.cursor(hover=True)
global cid
cid = fig.canvas.mpl_connect('button_press_event', self.onclick)
plt.legend()
plt.show()
您的问题的答案已在您的示例中说明。您所要做的就是抓住当前上下文中的轴,并在其上绘制事件发出的点。这是一个简单的例子,与您自己的例子相差无几。
from matplotlib import pyplot as plt
i = 0
def onclick(event):
global i
ix, iy = event.xdata, event.ydata
ax = plt.gca()
ax.plot(ix, iy, 'b*')
ax.annotate(f"Clicked Point {i}", (ix,iy),color='Black')
i += 1
print(f'x = {ix}, y = {iy}')
event.canvas.draw()
fig, ax = plt.subplots()
ax.set_xlim([0,10])
ax.set_ylim([0,10])
fig.canvas.callbacks.connect('button_press_event', onclick)
plt.show()
我编写了这段代码,允许用户在图表上 select 4 个点,当这 4 个点被 select 编辑时,绘制了两条线。 但我希望用户能够通过更改点的颜色或在其周围画一个小圆圈来查看她 selected 了哪些点。 我尝试阅读 matplotlib 文档,但无法理解我的代码中 add/change 的内容。
我只想更改 selected 点的颜色或在 selected 点周围画一个小圆圈。 我该怎么做?
#ONCLICK Event which lets user select 4 points on the graph and stores the coordinates of the
points in coords
coords=[]
def onclick(self,event):
global ix, iy, fig
ix, iy = event.xdata, event.ydata
print ('x = %d, y = %d'%(ix, it))
#I am not able to plot this circle
#plt.Circle((ix,iy), radius=0.07, color='g')
event.canvas.draw()
self.coords.append(ix)
self.coords.append(iy)
print(len(self.coords))
if len(self.coords) == 8:
fig.canvas.mpl_disconnect(cid)
plt.close(1)
s=self.coords[0]
t=self.coords[1]
u=self.coords[2]
v=self.coords[3]
w=self.coords[4]
x=self.coords[5]
y=self.coords[6]
z=self.coords[7]
line1= '(' + str("{:.2f}".format(s))+',' + str("{:.2f}".format(t)) + ')' + ',' + '(' +str("{:.2f}".format(u)) +',' + str("{:.2f}".format(v)) + ')'
line2= '(' + str("{:.2f}".format(w))+',' + str("{:.2f}".format(x)) + ')' + ',' + '(' +str("{:.2f}".format(y)) +',' + str("{:.2f}".format(z)) + ')'
df['Parameter']=df['Parameter'].astype('float')
df['Step']=df['Step'].astype('float')
a = df['Step']
b = df['Parameter']
#LINE EQUATION
m1=(v-t)/(u-s)
c1=t-m1*s
x1=np.linspace(df.iloc[0]["Step"],df.iloc[samples-1]["Step"],100)
y1=m1*x1+c1
m2=(z-x)/(y-w)
c2=x-m2*w
x2=np.linspace(df.iloc[1]["Step"],df.iloc[samples-1]["Step"],100)
y2=m2*x2 +c2
#INTERSECTION
det=m2-m1
if det==0:
print("Lines are parallel\n")
else:
pointx=(c1-c2)/det
pointy=((c1*m2)-(m1*c2))/det
#PLOTTING
fig = plt.figure()
ax = plt.axes()
plt.plot(a, b, '.',color='yellow')
plt.plot(x1,y1,color='red',label=line1)
plt.plot(x2,y2,color='black',label=line2)
plt.xlabel('Volume')
plt.ylabel('Conductivity')
ax.set_ylim(bottom=0)
ax.set_xlim(left=0.)
idx = np.argwhere(np.diff(np.sign(y2 - y1))).flatten()
plt.title("END POINT")
plt.plot(x2[idx], y2[idx], 'ro',color='Purple')
plt.annotate("Intersecting Point", (pointx,pointy),color='Blue')
plt.plot((pointx,pointx),(pointy,0),'--',color='purple')
plt.annotate("(%s,0) Volume" %pointx, (pointx,0),color='Blue')
mplcursors.cursor(hover=True)
plt.legend()
plt.show()
return
#Main graph which appears when a button in the interface is clicked.
def manual(self):
df['Parameter']=df['Parameter'].astype('float')
df['Step']=df['Step'].astype('float')
a = df['Step']
b = df['Parameter']
df['Parameter']=df['Parameter'].astype('float')
df['Step']=df['Step'].astype('float')
a=df['Step']
b=df['Parameter']
global fig
fig=plt.figure()
plt.xlabel('Volume')
plt.ylabel('Conductivity')
plt.title('SCATTER PLOT')
plt.scatter(a, b,color='r', label=self.Name)
mplcursors.cursor(hover=True)
global cid
cid = fig.canvas.mpl_connect('button_press_event', self.onclick)
plt.legend()
plt.show()
您的问题的答案已在您的示例中说明。您所要做的就是抓住当前上下文中的轴,并在其上绘制事件发出的点。这是一个简单的例子,与您自己的例子相差无几。
from matplotlib import pyplot as plt
i = 0
def onclick(event):
global i
ix, iy = event.xdata, event.ydata
ax = plt.gca()
ax.plot(ix, iy, 'b*')
ax.annotate(f"Clicked Point {i}", (ix,iy),color='Black')
i += 1
print(f'x = {ix}, y = {iy}')
event.canvas.draw()
fig, ax = plt.subplots()
ax.set_xlim([0,10])
ax.set_ylim([0,10])
fig.canvas.callbacks.connect('button_press_event', onclick)
plt.show()