Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:用np.array数据覆盖shapefile_Python_Matplotlib_Shapefile - Fatal编程技术网

Python:用np.array数据覆盖shapefile

Python:用np.array数据覆盖shapefile,python,matplotlib,shapefile,Python,Matplotlib,Shapefile,我有一个美国的形状文件,我有一个m x n笛卡尔数据数组,表示每个像素的温度。我可以加载shapefile并对其进行打印: import shapefile as shp import matplotlib.pyplot as plt sf = shp.Reader("/path/to/USA.shp") plt.figure() for shape in sf.shapeRecords(): for i in range(len(shape.shape.parts)):

我有一个美国的形状文件,我有一个m x n笛卡尔数据数组,表示每个像素的温度。我可以加载shapefile并对其进行打印:

import shapefile as shp
import matplotlib.pyplot as plt

sf = shp.Reader("/path/to/USA.shp")

plt.figure()
for shape in sf.shapeRecords():
    for i in range(len(shape.shape.parts)):
        i_start = shape.shape.parts[i]
        if i==len(shape.shape.parts)-1:
            i_end = len(shape.shape.points)
        else:
            i_end = shape.shape.parts[i+1]
        x = [i[0] for i in shape.shape.points[i_start:i_end]]
        y = [i[1] for i in shape.shape.points[i_start:i_end]]
        plt.plot(x,y, color = 'black')
plt.show()
import pickle
from matplotlib import pyplot as mp
Tfile = '/path/to/file.pkl'
with open(Tfile) as f:
    reshapeT = pickle.load(f)
mp.matshow(reshapeT)
我能够读入我的数据并绘制它:

import shapefile as shp
import matplotlib.pyplot as plt

sf = shp.Reader("/path/to/USA.shp")

plt.figure()
for shape in sf.shapeRecords():
    for i in range(len(shape.shape.parts)):
        i_start = shape.shape.parts[i]
        if i==len(shape.shape.parts)-1:
            i_end = len(shape.shape.points)
        else:
            i_end = shape.shape.parts[i+1]
        x = [i[0] for i in shape.shape.points[i_start:i_end]]
        y = [i[1] for i in shape.shape.points[i_start:i_end]]
        plt.plot(x,y, color = 'black')
plt.show()
import pickle
from matplotlib import pyplot as mp
Tfile = '/path/to/file.pkl'
with open(Tfile) as f:
    reshapeT = pickle.load(f)
mp.matshow(reshapeT)
问题是,Reformet的维数为536 x 592,是美国的子域。但是,我有关于重塑栅格lat/long的左上角以及每个像素之间的间距0.01的信息


我的问题是:如何将整形数据覆盖到shapefile域的顶部?

如果我理解正确,您希望将536x592 numpy数组覆盖到打印的shapefile的特定部分上。我建议您使用Matplotlib的imwshow方法,并使用extent参数,这允许您将图像放置在绘图中

绘制形状文件的方法很好,但是,如果可以使用geopandas,它将大大简化事情。打印形状文件将减少到以下行:

import geopandas as gpd
sf = gpd.read_file("/path/to/USA.shp")
ax1 = sf.plot(edgecolor='black', facecolor='none')
如前所述,现在让我们加载阵列数据:

import pickle
Tfile = '/path/to/file.pkl'
with open(Tfile) as f:
    reshapeT = pickle.load(f)
现在为了能够在正确的位置绘制numpy数组,我们首先需要计算它的范围,它将覆盖的区域,用坐标表示。您提到您有关于左上角和分辨率0.01的信息-这就是我们所需要的。在下文中,我假设关于左上角的lat/lon信息保存在top_left_lat和top_left_lon变量中。数据块需要以元组的形式传递,每个边的值按左、右、下、上的顺序排列

因此,我们的范围可以计算如下:

extent_mat = (top_left_lon, top_left_lon + reshapeT.shape[1] * 0.01, top_left_lat - reshapeT.shape[0] * 0.01, top_left_lat)
最后,我们将矩阵绘制到同一个Axis对象ax1上,在该对象上我们已经绘制了计算范围内的形状文件:

# Let's turn off autoscale first. This prevents
# the view of the plot to be limited to the image
# dimensions (instead of the entire shapefile). If you prefer
# that behaviour, just remove the following line
ax1.autoscale(False)

# Finally, let's plot!
ax1.imshow(reshapeT, extent=extent_mat)

这正是我想要的!