如何在2d列表上绘制元组(python)
嗨,我正在读一个文本文件,它给了我坐标,我需要在2d列表上绘制坐标。我的文本文件很简单,已经包含了每行x,y的绘图。这就是它所包含的内容: 3,2 3,3 3,4 4,4 4,5 4,6 到目前为止,我已经能够从文件中提取坐标,但我一直在研究如何绘制元组。这是我的密码:如何在2d列表上绘制元组(python),python,python-3.x,tuples,Python,Python 3.x,Tuples,嗨,我正在读一个文本文件,它给了我坐标,我需要在2d列表上绘制坐标。我的文本文件很简单,已经包含了每行x,y的绘图。这就是它所包含的内容: 3,2 3,3 3,4 4,4 4,5 4,6 到目前为止,我已经能够从文件中提取坐标,但我一直在研究如何绘制元组。这是我的密码: fnhandle = open(file_name) lines = fnhandle.readlines() lines = [item.rstrip("\n") for item in
fnhandle = open(file_name)
lines = fnhandle.readlines()
lines = [item.rstrip("\n") for item in lines]
r_c_coordinates = list()
for item in lines:
item = item.split(",")
item = tuple(int(items) for items in item)
r_c_coordinates.append(item)
fnhandle.close()
编辑:“绘图”是指我有一个包含0的初始化2d列表。我必须回到元组坐标处的2d列表,并将其更改为1,如果通过“绘图”,您的意思是在2d图形上,这可能是最简单的方法:
import matplotlib.pyplot as plt
x_coords = [coord[0] for coord in r_c_coordinates]
y_coords = [coord[1] for coord in r_c_coordinates]
plt.plot(x_coords, y_coords, "k.", lw=0)
“绘图”是指我有一个包含0的初始化2d列表。
我必须回到元组坐标处的2d列表
把这些换成1
在内存中的栅格中打印点的示例:
file_name = "points.txt"
my_grid = [[0] * 10 for _ in range(10)] # 10 by 10 grid of zeros
def print_grid(grid):
for row in grid:
print(*row)
print_grid(my_grid)
r_c_coordinates = list()
with open(file_name) as file:
for line in file:
coordinate = [int(n) for n in line.rstrip().split(',')]
r_c_coordinates.append(tuple(coordinate))
for row, column in r_c_coordinates:
my_grid[column][row] = 1
print('- ' * len(my_grid[0]))
print_grid(my_grid)
我假设坐标为零
输出(带注释)
你的意思是,像在2d图形上一样绘制?看看matplotlib-你可以绘制多种样式的图形。它确实有一点僵硬的初始学习曲线,但它会做你需要的。不幸的是,对于作业,我们在2d列表/数组上做了它。如果我使用matplot等,将扣分。
> python3 test.py
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
- - - - - - - - - -
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 # 3,2
0 0 0 1 0 0 0 0 0 0 # 3,3
0 0 0 1 1 0 0 0 0 0 # 3,4 & 4,4
0 0 0 0 1 0 0 0 0 0 # 4,5
0 0 0 0 1 0 0 0 0 0 # 4,6
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
>