仅对未知边界(墙)内的表应用更改

2024-05-19 02:55:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望我的循环只将“墙”内的表单元格从0更改为5。 “墙”是用户定义的,可以是基于坐标的任何形状。 这个情节只是为了形象化。你知道吗

import matplotlib.pyplot as plt 
import pandas as pd

wallPointsX = [5,5,30,30,55,55,5]
wallPointsY = [5,30,30,55,55,5,5]

df = pd.DataFrame(0, index=range(60), columns=range(60))

for x in range(0, 60):
    for y in range(0, 60):
        df[x][y] = 5 #Should only apply inside "walls"

plt.plot(wallPointsX, wallPointsY)
plt.pcolor(df)
plt.show()

Result plot


Tags: 用户inimportdffor定义plotas
1条回答
网友
1楼 · 发布于 2024-05-19 02:55:44

好吧,花了我一些时间,但做起来很有趣。这里的想法是首先从定义墙的坐标中创建一个连续的path。接下来,创建一个对象Path。现在循环遍历数据帧中的每个点,然后使用contains_point查看所创建的Path是否包含该(x,y)点。此外,我还必须在if语句中使用条件x==55 and (5<y<=55),以便包含与 最右边的墙。你知道吗

import matplotlib.path as mplPath
import numpy as np

wallPointsX = [5,5,30,30,55,55,5]
wallPointsY = [5,30,30,55,55,5,5]

# Create a continuous path across the wall coordinates
path = np.array([list(i) for i in zip(wallPointsX, wallPointsY)])
Path = mplPath.Path(path)

df = pd.DataFrame(0, index=range(60), columns=range(60))

for x in range(0, 60):
    for y in range(0, 60):
        if Path.contains_point((x,y)) or (x==55 and (5<y<=55)):
            df[x-1][y-1] = 5 #Should only apply inside "walls"

plt.plot(wallPointsX, wallPointsY)
plt.pcolor(df)

输出

enter image description here

相关问题 更多 >

    热门问题