我正在尝试使用 basemap 在 matplotlib 中的 2 个不同的 X 和 Y 点之间绘制多条线。
import matplotlib.pyplot as plt
import numpy as np
Map=pd.read_excel(r'file.xlsx')
lat=Map['Start Latitude']
long=Map['Start Longitude']
lat1=Map['Fin Latitude']
long2=Map['Fin Longitude']
x,y=m(lon.values,lat.values)
plt=m.scatter(x,y, marker="o", latlon=False)
例如:
Line1 would be between 1x,1y and 1x1,1y1
Line3 would be between 2x,2y and 2x1,2y1
Line3 would be between 3x,3y and 3x1,3y1
etc
其中“x”、“y”、“x1”和“y1”位于单独的列中(“起始纬度”、“起始经度”、“鳍纬度”、“鳍经度”)。
会有 100 多条不同的线路
我可以在上面绘制“起始纬度”和“起始经度”(使用x,y=m(lon.values,lat.values)
和plt=m。 scatter(x,y,marker="o", latlon=False)
,但无法绘制第二个点并将线连接到它。
如有任何建议,我们将不胜感激!谢谢。
最佳答案
由于有“100+”行,我建议在这里使用 LineCollection
。当然,您首先需要将坐标投影到 basemap 系统中。然后,您可以再次将它们堆叠到 numpy 数组中,并为 LineCollection
创建段数组。
import numpy as np; np.random.seed(42)
import pandas as pd
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
df = pd.DataFrame({"lon1" : np.random.randint(-15,30,10),
"lat1" : np.random.randint(33,66,10),
"lon2" : np.random.randint(-15,30,10),
"lat2" : np.random.randint(33,66,10)})
m = Basemap(llcrnrlon=-12,llcrnrlat=30,urcrnrlon=50,urcrnrlat=69.,
resolution='i', projection='tmerc', lat_0 = 48.9, lon_0 = 15.3)
m.drawcoastlines(linewidth=0.72, color='gray')
m.drawcountries(zorder=0, color='gray')
lon1, lat1 = m(df.lon1.values, df.lat1.values)
lon2, lat2 = m(df.lon2.values, df.lat2.values)
pts = np.c_[lon1, lat1, lon2, lat2].reshape(len(lon1), 2, 2)
plt.gca().add_collection(LineCollection(pts, color="crimson", label="Lines"))
m.plot(lon1, lat1, marker="o", ls="", label="Start")
m.plot(lon2, lat2, marker="o", ls="", label="Fin")
plt.legend()
plt.show()
关于python - 如何在 matplotlib/basemap 中绘制多个 x,y 点之间的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54488720/