python - 在 Python 中绘制连接点的 xy 网格
问题描述
这里是 Python 新手!
我希望你能帮忙。我正在尝试创建一个包含以下列的数据框:
- 索引(唯一的序号)
- 原点坐标(格式为 (x, y))
- 目的地坐标(相同格式)
- connected (binary 0/1) - 表示起点和终点是否连接
我这样做是因为我正在尝试使用这些坐标创建一个网格,将每个坐标显示为一个点,并将每个连接显示为一条线段。
这是我的模拟尝试:
import pandas as pd
origin = [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2),
(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2),
(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2),
(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2),
(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2),
(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2),
(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2),
(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2),
(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2)]
destination = [(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),
(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),
(0,2),(0,2),(0,2),(0,2),(0,2),(0,2),(0,2),(0,2),(0,2),
(1,0),(1,0),(1,0),(1,0),(1,0),(1,0),(1,0),(1,0),(1,0),
(1,1),(1,1),(1,1),(1,1),(1,1),(1,1),(1,1),(1,1),(1,1),
(1,2),(1,2),(1,2),(1,2),(1,2),(1,2),(1,2),(1,2),(1,2),
(2,0),(2,0),(2,0),(2,0),(2,0),(2,0),(2,0),(2,0),(2,0),
(2,1),(2,1),(2,1),(2,1),(2,1),(2,1),(2,1),(2,1),(2,1),
(2,2),(2,2),(2,2),(2,2),(2,2),(2,2),(2,2),(2,2),(2,2)]
connected = np.zeros(81)
network = pd.DataFrame({'origin': origin, 'destination': destination, 'connected': connected})
for ind in network.index:
if network['origin'][ind] == network['destination'][ind]:
network['connected'][ind] = 0
else:
network['connected'][ind] = np.random.randint(0,2)
我有几个问题:
- 如何使用 pyplot 绘制这些点和连接?
- 我的代码随机连接点,无论它们是否相邻,但我需要它们仅在对角线或正交相邻的情况下随机连接(例如,(0,0)无法连接到(2,1))。我怎么做?
- 有没有更好/更优雅的方式来呈现这些数据,而不是我在这里共享的 3 列数据框?
- 我的网格将是一个 25x25 的网格,因此坐标列表会更长,我怎样才能自动生成这些坐标而不是手动写出来?
在此先感谢您,非常感谢您的帮助!
解决方案
推荐阅读
- c - 编译时未使用的参数和控件到达非空函数错误的末尾 - CS50 pset3 - 音乐
- ruby-on-rails - Rails 路由:as-block 和 device_for 块
- python - 如何在 SQLAlchemy 的连接表中聚合不同的值?
- sql - SAS中的nodupkey和SQL中的SELECT * DISTINCT FROM table_name之间的区别
- php - 是否有内置函数可以为 Oracle 查询转义字符串?(如 MySQL 的 mysql_real_escape_string)
- javascript - javascript 承诺定义和使用
- javascript - 如何从 Firebase 管理员获取 Facebook ID?
- angular - Angular 2 CLI 在现有目录树中插入组件(向下移动其他组件)
- mysql - 在插入mysql时在列中添加当前时间
- php - 在 Laravel 中使用 User 有什么区别?和/用户