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

标签 python sqlite matplotlib matplotlib-basemap

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

相关文章:

python - 将两个图合并为一个图

python - 如何在 Pandas 中将日期舍入到周开始

python - 使用 pyCairo 填充带有多个孔的多边形

c# - 共享数据库 XML vs SQLite vs SQL Server CE

sqlite - 数据类型/存储类在 SQLite 中是否重要?

Python 极坐标图 : Plot values corresponding to the angle

python - 如何打印每个输出数据的时间

python - 如何仅验证扩展名为 .xlsx 的文件名?

java - 将 Java 连接到 SQLite

python - 如何在绘图/图表中显示同一列的不同值