gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:07:58 25 4
gpt4 key购买 nike

我尝试(在 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()

enter image description here

关于python - 在 python basemap 上绘制点会产生运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43835529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com