python连接mysql数据库,筛选前五十的数,使用地图库(如Basemap、GeoPandas等)和matplotlib,将每个起始地或目的地(基于经纬度信息)绘制到地图上。可以使用不同的颜色表示不同的国家或航空公司等。
时间: 2024-01-22 20:21:06 浏览: 46
首先,需要安装mysql-connector-python库来连接mysql数据库。可以使用以下命令进行安装:
```
pip install mysql-connector-python
```
接下来,可以使用以下代码连接mysql数据库并查询前50条数据:
```python
import mysql.connector
# 连接数据库
cnx = mysql.connector.connect(user='username', password='password',
host='host', database='database_name')
cursor = cnx.cursor()
# 执行查询
query = ("SELECT start_latitude, start_longitude, end_latitude, end_longitude, country, airline "
"FROM flights "
"LIMIT 50")
cursor.execute(query)
# 获取查询结果
results = cursor.fetchall()
# 关闭数据库连接
cursor.close()
cnx.close()
```
接下来,需要使用Basemap或GeoPandas来绘制地图并将起始地或目的地绘制到地图上。这里以Basemap为例,可以使用以下代码绘制地图:
```python
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
# 创建地图
m = Basemap(projection='mill', llcrnrlat=-90, urcrnrlat=90, llcrnrlon=-180, urcrnrlon=180)
# 绘制海岸线、国家边界线等
m.drawcoastlines()
m.drawcountries()
m.fillcontinents(color='coral', lake_color='aqua')
# 绘制起始地和目的地
for result in results:
start_lat, start_lon, end_lat, end_lon, country, airline = result
x, y = m(start_lon, start_lat)
m.plot(x, y, 'bo', markersize=10)
x, y = m(end_lon, end_lat)
m.plot(x, y, 'ro', markersize=10)
# 显示地图
plt.show()
```
最后,可以根据需要使用不同的颜色表示不同的国家或航空公司等。可以在绘制起始地和目的地时根据需要设置不同的颜色。
相关推荐
![ipynb](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)