import cartopy.crs as ccrs import matplotlib.pyplot as plt from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER import matplotlib.ticker as mticker import numpy as np import pandas as pd import requests from bs4 import BeautifulSoup def get_catalog(): url = 'http://news.ceic.ac.cn/index.html' web_data = requests.get(url) web_data.encoding = web_data.apparent_encoding web_data = web_data.text f = open('earthquake.csv', 'w') Date = [] Latitude = [] Longitude = [] Magnitude = [] soup = BeautifulSoup(web_data, 'lxml') rows = soup.find_all('tr') for i in rows: cols = i.find_all('td') if len(cols) != 0: Magnitude.append(cols[0].text) Date.append(cols[1].text.split()[0]) Latitude.append(cols[2].text) Longitude.append(cols[3].text) f.write('Date,Latitude,Longitude,Magnitude\n') for i in range(len(Date)): line = Date[i] + ',' + Latitude[i] + ',' + Longitude[i] + ',' + Magnitude[i] print(Date[i] + ',' + Latitude[i] + ',' + Longitude[i] + ',' + Magnitude[i]) f.write(line + '\n') f.close() def plot_map(): plt.figure(figsize=(12, 8)) plt.rcParams['font.sans-serif'] = 'FangSong' # 設置中文字體為仿宋 plt.rcParams['axes.unicode_minus'] = False # 正常顯示坐標軸上的鈾號 ax = plt.axes(projection=ccrs.PlateCarree()) ax.coastlines() ax.stock_img() # 將參考底圖圖像添加到地圖,如果沒有這條命令,底圖是沒有背景色的 # 畫經緯度網格 gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=False, linewidth=1.2, color='k', alpha=0.3, linestyle='--') gl.xlabels_top = False # 關閉頂端的經緯度標簽 gl.ylabels_right = False # 關閉右側的經緯度標簽 gl.xformatter = LONGITUDE_FORMATTER # x軸設為經度的格式 gl.yformatter = LATITUDE_FORMATTER # y軸設為緯度的格式 #設置經緯度網格的間隔 gl.xlocator = mticker.FixedLocator(np.arange(-180, 180, 30)) gl.ylocator = mticker.FixedLocator(np.arange(-90, 90, 30)) # 設置顯示范圍 ax.set_extent([-180, 180, -90, 90],crs=ccrs.PlateCarree()) #設置坐標標簽 ax.set_xticks(list(range(-180,180,60)), crs=ccrs.PlateCarree()) ax.set_yticks(list(range(-90,90,30)), crs=ccrs.PlateCarree()) plt.xticks(fontsize = 20) plt.yticks(fontsize = 20) # 填加大地測量座標系下的線條 # ny_lon, ny_lat = -75, 43 # delhi_lon, delhi_lat = 77.23, 28.61 # plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat], # color='blue', linewidth=2, marker='o', # transform=ccrs.Geodetic(), # ) # 填加直角座標系下的線條 # plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat], # color='red', linestyle='--', # transform=ccrs.PlateCarree(), # ) # 填加文字 # plt.text(ny_lon - 3, ny_lat - 12, '紐約', # horizontalalignment='right', color = 'red', # transform=ccrs.Geodetic()) # plt.text(delhi_lon + 3, delhi_lat - 12, 'Delhi', # horizontalalignment='left', color = 'red', # transform=ccrs.Geodetic()) # 畫震中分布 data = pd.read_csv('earthquake.csv') scatter = ax.scatter(data.Longitude, data.Latitude, s= (0.2* 2 ** data.Magnitude)**2, c='red', alpha=0.8, # c=data.depth / data.depth.max(), alpha=0.8, transform=ccrs.PlateCarree()) # 填加圖例 kw = dict(prop="sizes", num=5, color='red', fmt="M {x:.1f}", func=lambda s: np.log2(np.sqrt(s)/0.2)) legend2 = ax.legend(*scatter.legend_elements(**kw), loc="lower left", title="Mag") ax.add_artist(legend2) plt.tight_layout() plt.savefig('world.png',dpi = 600) # plt.show() #################主程序################### get_catalog() plot_map()