在Python中绘制数组 [英] Plotting an array in Python

查看:72
本文介绍了在Python中绘制数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Python中绘制生成的数组.数组是从maze.txt生成的,如下所示.结果是一个仅由0和1组成的数组,其中每个0和1代表地图上的一个网格正方形. 0是边界,可以是黑色,也可以是任何颜色,而1是已导航的路径.

I'm trying to plot a generated array in Python. The array is generated from maze.txt as you can see below. The result is an array composed of only 0's and 1's where each 0 and 1 represents one grid square on the map. The 0's are boundaries, this can be black or any color for that matter, and the 1's are the path that has been navigated.

四处搜寻,我认为matplotlib可以完成我想做的事情,但是我不太确定如何实现它.我对Python缺乏经验.谢谢您的任何建议/帮助.

Searching around I thought that matplotlib could accomplish what I'm trying to do but I'm not quite sure how to implement it. I'm very inexperienced with Python. Thank you for any suggestions/help.

file = open("maze.txt", 'r')
arr = []
for line in file.readlines():
    #print();
    arr1 = []
    for c in line:
        if(c.isspace() and (c!="\n")):
            arr1.append(1)
            #print("1",end="")
        elif(c!="\n"):
            arr1.append(0)
            #print("0",end="")

    arr.append(arr1)
#print()
#print(arr)
for row in arr:
    print(row)

以下是"maze.txt"的示例

Below is an example of 'maze.txt'

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                               |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
| |                             |
+ + +-+-+-+-+-+-+-+-+-+-+-+-+-+ +
| | |       |   |   |         | |
+ + + +-+ +-+ + + + + +-+-+-+ + +
| | | | |     | | | | |   |   | |
+ + + + +-+-+-+ + + + +-+ + +-+ +
| | | |         | | | |     |   |
+ + + + +-+-+-+ + + + +-+ +-+ + +
| | | | |   | | | |   |   | | | |
+ + + + + + + +-+ +-+-+ +-+ + + +
| | | |   | |     |       |   | |
+ + + +-+-+ + +-+-+-+ +-+-+ + + +
| | | |     | |   |     | | | | |
+ + + + + +-+ + + + +-+-+ + + + +
| | | | | |   |         |   | | |
+ + + + +-+ +-+-+-+-+-+ + + + + +
| | | | |     |       | | | | | |
+ + + + + +-+-+-+-+ + + + +-+-+ +
| | | | |   |       | | |     | |
+ + + + + + + +-+ +-+ + +-+-+ + +
| | | |   | |       | |     | | |
+ + + +-+-+ +-+ +-+ + +-+-+ + + +
| |   |     |       | |   | | | |
+ +-+ + +-+-+ +-+ +-+ + + + + + +
|   | |             |   |   | | |
+ + + +-+-+-+-+-+-+-+-+-+-+-+ + +
| | |                         | |
+ + +-+-+-+-+-+-+-+-+-+-+-+-+-+ +
| |                             |
.-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

转换为:

[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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 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, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 0, 1, 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, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0]
[0, 1, 0, 1, 0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, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0]
[0, 1, 0, 1, 0, 1, 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, 1, 0]
[0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0]
[0, 1, 0, 1, 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, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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]

@Craig 错误

推荐答案

您可以使用

You can use the pcolormesh() command in matplotlib to plot an array of values.

import matplotlib.pyplot as plt

plt.pcolormesh(arr)
plt.axes().set_aspect('equal') #set the x and y axes to the same scale
plt.xticks([]) # remove the tick marks by setting to an empty list
plt.yticks([]) # remove the tick marks by setting to an empty list
plt.axes().invert_yaxis() #invert the y-axis so the first row of data is at the top
plt.show()

通过将x和y轴比例设置为相等(使其成正方形)并删除x和y刻度线,附加的行有助于改善迷宫的外观.

The additional lines help improve the appearance of the maze by setting the axes x and y scales equal (to make it square) and by removing the x an y tick marks.

编辑

添加了plt.axes().invert_yaxis()行以反转绘图,使其以与数组相同的方向显示(顶部的第0行).按照惯例,matplotlib在左下角绘制arr [0,0].反转y轴,将其移到左上角.

Added the line plt.axes().invert_yaxis() to invert the plot so that it appears in the same orientation as the array (with row 0 at the top). By convention, matplotlib plots arr[0,0] in the bottom left corner. Inverting the y-axis move this to the top left corner.

此代码产生以下图像:

这篇关于在Python中绘制数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆