Skip to main content
 首页 » 编程设计

python之在 python basemap 上绘制点会产生运行时错误

2025年02月15日27dyllove98

我尝试(在 python 3.6 中)根据纬度和经度坐标在全局 map 上绘制来自数据库(sqlite 和 basemap 模块)的点(机场)。但是,我的代码将这些点绘制为独立于 map 的图形和一个运行时错误:RuntimeError: Can not put single artist in more than one figure. 我不确定我做错了什么:

from mpl_toolkits.basemap import Basemap  
import matplotlib.pyplot as plt 
import sqlite3 
conn = sqlite3.connect("flights.db") 
cur = conn.cursor() 
cur.execute("select * from airlines limit 5;") 
results = cur.fetchall() 
print(results) 
coords = cur.execute(""" select cast(longitude as float), \ 
                     cast(latitude as float) from airports;""" \ 
                     ).fetchall() 
 
m = Basemap(projection = 'merc', llcrnrlat =-80, urcrnrlat = 80, \ 
            llcrnrlon = -180, urcrnrlon = 180, lat_ts = 20, \ 
            resolution = 'c') 
 
m.drawcoastlines() 
m.drawmapboundary() 
 
x, y = m([l[0] for l in coords], [l[1] for l in coords]) 
m.scatter(x, y, 1, marker='o', color='red') 

我得到的错误如下:

RuntimeError: Can not put single artist in more than one figure 

请您参考如下方法:

将最后一行重写为

lons = [l[0] for l in coords] 
lats = [l[1] for l in coords] 
x, y = m(lons, lats) 
 
m.scatter(x, y, 1, marker='o', color='red') 
plt.show()