我正在尝试使用底图在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)
,但是不能绘制第二个点并在其上连接一条线。
任何建议将不胜感激!谢谢。
答案 0 :(得分:0)
由于“ 100+”行,我建议在此处使用LineCollection
。当然,您首先需要在底图系统中投影坐标。然后,您可以将它们再次堆叠到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()