matplotlib 具有交替单元宽度的pcolormesh

of1yzvn4  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(146)

我想做一个matplotlib colourmesh,它被不规则地缩放,使一些行和列比其他的宽。
请记住,我对matplotlib不是很熟悉,很可能忽略了一个明显的解决方案。
假设我有以下代码:

plt.axes().set_aspect("equal")
maze = plt.pcolormesh(arr)
plt.show()

其中arr是一个2d位数组,生成一个colourmesh,如下所示:

我想减少每一行和每一列的宽度来压缩上面迷宫的墙壁。
任何帮助将不胜感激,谢谢。

编辑:这里是一个数据样本。它基本上只是一个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   
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   1   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   0   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   1   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   0   1   0   1   1   1   0   
0   1   0   1   0   1   0   1   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   
0   1   0   1   0   1   1   1   0   1   1   1   0   1   1   1   0   1   1   1   1   1   1   1   1   1   0   1   0   1   1   1   0   
0   1   0   1   0   0   0   0   0   1   0   1   0   1   0   1   0   0   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   
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   0   1   0   1   1   1   0   
0   1   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   1   0   1   0   1   0   
0   1   1   1   1   1   1   1   0   1   1   1   0   1   1   1   0   1   0   1   1   1   1   1   1   1   0   1   0   1   1   1   0   
0   1   0   1   0   1   0   1   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   
0   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   0   1   0   1   1   1   1   1   1   1   0   1   0   1   1   1   0   
0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   0   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   
0   1   1   1   1   1   1   1   1   1   1   1   1   1   0   1   1   1   0   1   1   1   1   1   1   1   0   1   0   1   1   1   0   
0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   
0   1   1   1   1   1   1   1   1   1   1   1   1   1   0   1   1   1   0   1   1   1   1   1   1   1   0   1   0   1   1   1   0   
0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   0   0   0   0   1   0   1   0   1   0   1   0   1   0   1   0   1   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   0   1   0   1   1   1   0   
0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   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   0   1   0   1   1   1   0   
0   1   0   1   0   1   0   0   0   0   0   0   0   0   0   1   0   1   0   0   0   0   0   0   0   0   0   1   0   1   0   1   0   
0   1   1   1   1   1   0   1   1   1   0   1   1   1   0   1   1   1   0   1   1   1   0   1   1   1   0   1   0   1   1   1   0   
0   0   0   0   0   0   0   1   0   1   0   1   0   1   0   0   0   0   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   
0   1   1   1   1   1   1   1   0   1   1   1   0   1   1   1   1   1   1   1   0   1   1   1   0   1   1   1   0   1   1   1   0   
0   1   0   0   0   0   0   1   0   1   0   1   0   1   0   0   0   0   0   1   0   1   0   1   0   1   0   0   0   1   0   1   0   
0   1   0   1   1   1   0   1   1   1   0   1   1   1   0   1   1   1   0   1   1   1   0   1   1   1   0   1   1   1   1   1   0   
0   1   0   1   0   1   0   0   0   0   0   0   0   0   0   1   0   1   0   0   0   0   0   0   0   0   0   1   0   1   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   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   0   1   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
shstlldc

shstlldc1#

plt.pcolormesh()可以用1个或3个未命名的参数调用。当只有1个参数时,该参数应该是网格数据。当有3个参数时,前2个参数或x和y方向上网格线的位置。
您可以通过平铺宽单元格和窄单元格的宽度来计算这些位置,然后计算cumulative sum。请注意,网格位置需要比单元格多一个。因此,33单元格的网格线(或2*17)。如果x和y方向上的单元格数量不相等,您可以单独计算网格位置。
这里有一个例子。宽度的选择使得宽单元格和窄单元格占用2个单位,这将使轴坐标与示例相似。如果这些坐标不重要,您可以使用任何宽度。

import matplotlib.pyplot as plt
import numpy as np

arr = np.array([[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,1,0,1,0],[0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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