[英]Voronoi not plotting real time data tuple index out of range
I'm trying to plot a voronoi diagram with real time data, but get the error:我正在尝试 plot 带有实时数据的 voronoi 图,但得到错误:
IndexError: tuple index out of range
the code:编码:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
data = str(data, "utf-8") #convert bytes into string and fix the 'b'
#data.decode("utf-8", errors="ignore")
data = data.strip(" ").split(".")
x = data[0]
y = data[1]
x = float(x.replace( ',', '.'))
y = float(y.replace( ',', '.'))
vor = Voronoi(x,y)
The value of the data
variable is like this: [b' 0,2036377.2,04291.', b' 0,2027879.2,040747.']
. data
变量的值是这样的: [b' 0,2036377.2,04291.', b' 0,2027879.2,040747.']
。
Any idea how to fix this?知道如何解决这个问题吗?
As mentioned in the comments, the error you are getting is because you are passing the wrong input to Voronoi
, please read the documentation如评论中所述,您收到的错误是因为您将错误的输入传递给
Voronoi
,请阅读文档
Regarding what you are trying to do and assuming that the data
you get from data, addr = sock.recvfrom(1024)
is like this [b' 0,2036377.2,04291.', b' 0,2027879.2,040747.']
you then have to do address the following points:关于你想要做什么,并假设你从
data
中得到的data, addr = sock.recvfrom(1024)
是这样[b' 0,2036377.2,04291.', b' 0,2027879.2,040747.']
你那么必须解决以下几点:
float
float
Voronoi
diagramVoronoi
图The code you have so far address most of these points but it does not structure the data as the input for the Voronoi
diagram.到目前为止,您拥有的代码解决了大多数这些问题,但它没有将数据结构化为
Voronoi
图的输入。
The code bellow addressed all the points and will create the Voronoi
diagram for you:下面的代码解决了所有问题,并将为您创建
Voronoi
图:
import re
from scipy.spatial import Voronoi, voronoi_plot_2d
import matplotlib.pyplot as plt
import doctest
doctest.testmod() # used to test the docstring
# compile the regular expression used to parse the input data
regex = re.compile(' (\d+,\d+)\.(\d+,\d+)\.')
def parse_data(data):
"""
parse a list of strings with x, y coordinates
>>> data = [b' 0,2036377.2,04291.', b' 0,2027879.2,040747.']
>>> parse_data(data)
[[0.2036377, 2.04291], [0.2027879, 2.040747]]
"""
proc_data = []
for i in data:
m = regex.match(i.decode('utf-8')) # decode the bytes and match the regex
if m:
# parse the coordinates and organise the data structure
proc_data += [[float(m.group(1).replace(',','.')),
float(m.group(2).replace(',','.'))]]
return proc_data
data = [b' 0,2036377.2,04291.', b' 0,2027879.2,040747.',
b' 0,2018921.2,037455.', b' 0,2010467.2,034439.',
b' 0,2004007.2,031721.', b' 0,1996321.2,027795.',
b' 0,1989551.2,023898.', b' 0,1983429.2,020666.',
b' 0,1978466.2,017263.']
data = parse_data(data)
vor = Voronoi(data) # create the Voronoi diagram with correct input data
voronoi_plot_2d(vor)
plt.show()
The result is the following:结果如下:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.