How to resolve TypeError:s objext can not be iterpreted as an int
How to resolve TypeError:s objext can not be iterpreted as an int
每当我单击鼠标时,我都尝试使用以下代码来计算距离,但我收到此错误:TypeError: 'numpy.float64' object cannot be interpreted as an integer
我曾尝试遵循此解决方案但没有与我合作:sudo pip install -U numpy==1.11.0
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor
from numpy import random
import numpy as np
#import pyautogui
import mplcursors
from math import sqrt
x1, y1=random.rand(2,100)
x2, y2=random.rand(2,100)
fig, ax= plt.subplots()
p, = plt.plot(x1,y1, 'o')
p2, = plt.plot( x2,y2, 'o')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
cursor=Cursor(ax, horizOn=True, vertOn=True, color='blue', linewidth=1.0)
def onclick(event):
z1, r1 = event.xdata, event.ydata
print(z1, r1)
#return z1,r1
a1= int(z1,r1)
p1 = a1.split(",")
p2 = a1.split(",")
distance = sqrt( ((int(p1[0])-int(p2[0]))**2)+((int(p1[1])-int(p2[1]))**2) )
print("distance between ",p1,"and", p2, "is",distance)
fig.canvas.mpl_connect('button_press_event', onclick)
mplcursors.cursor(hover=True)
plt.show()
你的代码似乎对我有用,或者到底是什么问题?
这不是你想要达到的输出吗?
我假设您希望能够单击两个点,并在单击第二个点后计算它们之间的距离。我修改了您的代码,使您能够存储 class 变量,您需要这些变量来跟踪第一次和第二次点击事件。
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor
from numpy import random
import mplcursors
from math import sqrt
class DistancePlot:
def __init__(self):
self.x1, self.y1 = random.rand(2,100)
self.x2, self.y2 = random.rand(2,100)
self.fig, self.ax = plt.subplots()
self.p = plt.plot(self.x1, self.y1, 'o')
self.p2 = plt.plot(self.x2, self.y2, 'o')
self.ax.set_xlabel('X-axis')
self.ax.set_ylabel('Y-axis')
self.d1 = (0.0, 0.0)
self.d2 = (0.0, 0.0)
self.first_click = True
cursor=Cursor(self.ax, horizOn=True, vertOn=True, color='blue', linewidth=1.0)
self.fig.canvas.mpl_connect('button_press_event', self.onclick)
mplcursors.cursor(hover=True)
plt.show()
def onclick(self, event):
z1, r1 = event.xdata, event.ydata
print(z1, r1)
if self.first_click:
self.first_click = False
self.d1 = (z1, r1)
else:
self.first_click = True
self.d2 = (z1, r1)
distance = sqrt((((self.d1[0]) - (self.d2[0])) ** 2) + (((self.d1[1]) - (self.d2[1])) ** 2))
print("distance between ", self.d1, "and", self.d2, "is", distance)
dp = DistancePlot()
这可以大大清理,但为了清楚地了解您的原始问题,我使用了您的大部分原始代码。
输出:
0.428774046828733 0.7279552011765266
0.8336888592710245 0.383512252192596
distance between (0.428774046828733, 0.7279552011765266) and (0.8336888592710245, 0.383512252192596) is 0.5315984861151531
0.18538262951369436 0.6425804531377746
0.3933716588556365 0.40117599316613084
distance between (0.18538262951369436, 0.6425804531377746) and (0.3933716588556365, 0.40117599316613084) is 0.3186464335604656
每当我单击鼠标时,我都尝试使用以下代码来计算距离,但我收到此错误:TypeError: 'numpy.float64' object cannot be interpreted as an integer 我曾尝试遵循此解决方案但没有与我合作:sudo pip install -U numpy==1.11.0
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor
from numpy import random
import numpy as np
#import pyautogui
import mplcursors
from math import sqrt
x1, y1=random.rand(2,100)
x2, y2=random.rand(2,100)
fig, ax= plt.subplots()
p, = plt.plot(x1,y1, 'o')
p2, = plt.plot( x2,y2, 'o')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
cursor=Cursor(ax, horizOn=True, vertOn=True, color='blue', linewidth=1.0)
def onclick(event):
z1, r1 = event.xdata, event.ydata
print(z1, r1)
#return z1,r1
a1= int(z1,r1)
p1 = a1.split(",")
p2 = a1.split(",")
distance = sqrt( ((int(p1[0])-int(p2[0]))**2)+((int(p1[1])-int(p2[1]))**2) )
print("distance between ",p1,"and", p2, "is",distance)
fig.canvas.mpl_connect('button_press_event', onclick)
mplcursors.cursor(hover=True)
plt.show()
你的代码似乎对我有用,或者到底是什么问题?
这不是你想要达到的输出吗?
我假设您希望能够单击两个点,并在单击第二个点后计算它们之间的距离。我修改了您的代码,使您能够存储 class 变量,您需要这些变量来跟踪第一次和第二次点击事件。
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor
from numpy import random
import mplcursors
from math import sqrt
class DistancePlot:
def __init__(self):
self.x1, self.y1 = random.rand(2,100)
self.x2, self.y2 = random.rand(2,100)
self.fig, self.ax = plt.subplots()
self.p = plt.plot(self.x1, self.y1, 'o')
self.p2 = plt.plot(self.x2, self.y2, 'o')
self.ax.set_xlabel('X-axis')
self.ax.set_ylabel('Y-axis')
self.d1 = (0.0, 0.0)
self.d2 = (0.0, 0.0)
self.first_click = True
cursor=Cursor(self.ax, horizOn=True, vertOn=True, color='blue', linewidth=1.0)
self.fig.canvas.mpl_connect('button_press_event', self.onclick)
mplcursors.cursor(hover=True)
plt.show()
def onclick(self, event):
z1, r1 = event.xdata, event.ydata
print(z1, r1)
if self.first_click:
self.first_click = False
self.d1 = (z1, r1)
else:
self.first_click = True
self.d2 = (z1, r1)
distance = sqrt((((self.d1[0]) - (self.d2[0])) ** 2) + (((self.d1[1]) - (self.d2[1])) ** 2))
print("distance between ", self.d1, "and", self.d2, "is", distance)
dp = DistancePlot()
这可以大大清理,但为了清楚地了解您的原始问题,我使用了您的大部分原始代码。
输出:
0.428774046828733 0.7279552011765266
0.8336888592710245 0.383512252192596
distance between (0.428774046828733, 0.7279552011765266) and (0.8336888592710245, 0.383512252192596) is 0.5315984861151531
0.18538262951369436 0.6425804531377746
0.3933716588556365 0.40117599316613084
distance between (0.18538262951369436, 0.6425804531377746) and (0.3933716588556365, 0.40117599316613084) is 0.3186464335604656