python - 如何在 Quadtree [Python] 中读取和使用 CSV 文件中的坐标?
问题描述
我正在使用 python 构建四叉树,并设法为随机生成的点创建解决方案。主类是 QTree 类:
class QTree():
def __init__(self, treshold, customerCount):
self.threshold = treshold
self.points = [Point(random.uniform(0, 100), random.uniform(0, 100)) for x in range(customerCount)]
self.root = Node(0, 0, 100, 100, self.points)
def add_point(x, y):
self.points.append(Point(x, y))
def get_points(self):
return self.points
def subdivide(self):
recursive_subdivide(self.root, self.threshold)
def graph(self):
fig = plt.figure(figsize=(12, 8))
x = [point.x for point in self.points]
y = [point.y for point in self.points]
ax = fig.add_subplot(111)
c = find_children(self.root)
print("\n\nNumber of segments: %d" % len(c))
areas = set()
for el in c:
areas.add(el.width * el.height)
print("Minimum segment area: %.3f units" % min(areas))
for n in c:
ax.add_patch(patches.Rectangle((n.x0, n.y0), n.width, n.height, fill=False))
plt.title("Quadtree")
plt.plot(x, y, 'ro', markersize=3, color='b')
plt.savefig('QuadtreeDiagram.png', dpi=1000)
plt.show()
return
我通过调用 Qtree 类和其他各种 def 函数来获取我的四叉树图( https://i.stack.imgur.com/ojYHO.png ):
def test(treshold, customerCount):
qt = QTree(treshold, customerCount)
qt.subdivide()
qt.graph()
# Tests
test(1, 50)
我的问题是:如何更改随机点并使用我自己的带有坐标的 CSV 文件?谢谢 :D
解决方案
您可以添加一个新函数来从文件加载数据并设置点:
def load_points():
Df1 = pd.read_csv("D:\\test.txt", sep='\t' )
self.points=[Point(Df1["Col0"],Df1["Col1"]) for x in range(len(Df1))]
注意1:您必须获得新数据的len与customerCount之间的关系注意2:我假设该文件例如:
Col0 Col1
1 2
2 4
3 5
推荐阅读
- google-apps-script - 谷歌脚本:如何在不同的电子表格中找到活动单元格
- r - 为什么 Boxplot 出现在 RStudio 而不是我的 Shiny 应用程序中?
- c# - 如何使 ScrollView 在 UWP 应用中支持鼠标滚动?
- macos - 在 Travis 上存储证书文件
- python - 在 pandas 中按 id 填充缺失值
- ios - 替换 App 中每个 UILabel 的字体
- python-3.x - TypeError:“NoneType”对象不可下标。Kivy包问题?
- python - Python 正则表达式:检查匹配和捕获组
- r - 如何在ggplot中用相同的x绘制不同的y?
- linux - .gitlab-ci.yml 中描述 SSH 命令的最清晰简洁的方式