在 python 底图上绘制点会产生运行时错误
Plotting points on python basemap yields runtime error
我尝试(在 python 3.6 中)根据纬度和经度坐标在全球地图上绘制来自数据库(sqlite 和底图模块)的点(机场)。但是,我的代码将这些点绘制为独立于地图的图形和一个运行时错误,上面写着: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()
我尝试(在 python 3.6 中)根据纬度和经度坐标在全球地图上绘制来自数据库(sqlite 和底图模块)的点(机场)。但是,我的代码将这些点绘制为独立于地图的图形和一个运行时错误,上面写着: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()