Skip to main content
 首页 » 编程设计

python之带有 python 和 matplotlib 中线条的地理数据图/ map

2025年05月04日31itcoder

我记得在一篇博客文章中看到了一种可视化地理数据的好方法。它只是代表纬度的线和要显示的变量的线的高点。我试着在下面的图片上画出它:

你们中的一些人还记得解释如何生成这些 map 的图书馆甚至博客文章吗? (我依稀记得它是 matplotlib & python,但我很可能是错的)

请您参考如下方法:

我认为这就是您想要的东西 - 在 3d 轴上绘制恒定纬度线。我已经在评论中解释了每个部分的作用<​​/p>

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
import numpy as np 
import itertools 
 
#read in data from csv organised in columns labelled 'lat','lon','elevation' 
data = np.recfromcsv('elevation-sample.csv', delimiter=',') 
 
# create a 3d axis on a figure 
fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
 
# Find unique (i.e. constant) latitude points 
id_list = np.unique(data['lat']) 
 
# stride is how many lines to miss.  set to 1 to get every line 
# higher to miss more 
stride = 5 
 
# Extract each line from the dataset and plot it on the axes 
for id in id_list[::stride]: 
    this_line_data = data[np.where(data['lat'] == id)] 
    lat,lon,ele = zip(*this_line_data) 
    ax.plot(lon,lat,ele, color='black') 
 
# set the viewpoint so we're looking straight at the longitude (x) axis 
ax.view_init(elev=45., azim=90) 
 
ax.set_xlabel('Longitude') 
ax.set_ylabel('Latitude') 
ax.set_zlabel('Elevation') 
ax.set_zlim([0,1500]) 
 
plt.show() 

我用来测试的数据集不是我的,是在github here上找到的.

输出如下:

注意 - 如果我误解了您草图中的轴标签,您可以交换纬度和经度。