python繪制地震散點(diǎn)圖
本項(xiàng)目是利用五年左右的世界地震數(shù)據(jù),通過python的pandas庫、matplotlib庫、basemap庫等進(jìn)行數(shù)據(jù)可視化,繪制出地震散點(diǎn)圖。主要代碼如下所示
from __future__ import division
import pandas as pd
from pandas import Series,DataFrame
import numpy as np
from matplotlib.patches import Polygon
chi_provinces = ['北京','天津','上海','重慶',
'河北','山西','遼寧','吉林',
'黑龍江','江蘇','浙江','安徽',
'福建','江西','山東','河南',
'湖北','湖南','廣東','海南',
'四川','貴州','云南','陜西',
'甘肅','青海','臺(tái)灣','內(nèi)蒙古',
'廣西','西藏','寧夏','新疆',
'香港','澳門'] #list of chinese provinces
def is_in_china(str):
if str[:2] in chi_provinces:
return True
else:
return False
def convert_data_2014(x):
try:
return float(x.strip())
except ValueError:
return x
except AttributeError:
return x
def format_lat_lon(x):
try:
return x/100
except(TypeError):
return np.nan
df = pd.read_excel(r'C:/Users/GGWS/Desktop/shuju/201601-12.xls')
df = df.append(pd.read_excel(r'C:/Users/GGWS/Desktop/shuju/201201-12.xls'),ignore_index = True)
df = df.append(pd.read_excel(r'C:/Users/GGWS/Desktop/shuju/shuju.xls'),ignore_index = True)
df = df.append(pd.read_excel(r'C:/Users/GGWS/Desktop/shuju/201501-12.xls'),ignore_index = True)
df_2014 = pd.read_excel(r'C:/Users/GGWS/Desktop/shuju/201401-12.xls') #have to introduce statics of 2014 independently because the format and the type of data of specific column in this data set are different from others
df['longitude'] = df['longitude'].apply(convert_data_2014)
df['latitude'] = df['latitude'].apply(convert_data_2014)
df_2014['longitude'] = df_2014['longitude'].apply(convert_data_2014)
df_2014['latitude'] = df_2014['latitude'].apply(convert_data_2014)
df = df.append(df_2014,ignore_index = True)
df = df[['latitude','longitude','magnitude','referenced place','time']] #only save four columns as valuable statics
df[['longitude','latitude']] = df[['longitude','latitude']].applymap(format_lat_lon) #use function "applymap" to convert the format of the longitude and latitude statics
df = df.dropna(axis=0,how='any') #drop all rows that have any NaN values
format_magnitude = lambda x: float(str(x).strip('ML'))
df['magnitude'] = df['magnitude'].apply(format_magnitude)
#df = df[df['referenced place'].apply(is_in_china)]
lon_mean = (df['longitude'].groupby(df['referenced place'])).mean()
lat_mean = (df['latitude'].groupby(df['referenced place'])).mean()
group_counts = (df['magnitude'].groupby(df['referenced place'])).count()
after_agg_data = pd.concat([lon_mean,lat_mean,group_counts], axis = 1 )
after_agg_data.rename(columns = {'magnitude':'counts'} , inplace = True)
#aggregate after grouping the data
after_sorted_data = after_agg_data.sort_values(by = 'counts',ascending = False)
new_index = np.arange(len(after_sorted_data.index))
after_sorted_data.index = new_index
paint_data = after_sorted_data[after_sorted_data['counts']>=after_sorted_data['counts'][80]]
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
plt.figure(figsize=(16,8))
m = Basemap()
m.readshapefile(r'C:/Users/GGWS/Desktop/jb/gadm36_CHN_1', 'states', drawbounds=True)
ax = plt.gca()
'''
for nshape,seg in enumerate (m.states):
poly = Polygon(seg,facecolor = 'r')
ax.add_patch(poly)
'''
m.drawcoastlines(linewidth=0.5)
m.drawcountries(linewidth=0.5)
m.shadedrelief()
for indexs in df.index:
lon2,lat2 = df.loc[indexs].values[1],df.loc[indexs].values[0]
x,y = m(lon2,lat2)
m.plot(x,y,'ro',markersize = 0.5) #獲取經(jīng)度值
'''
for indexs in after_sorted_data.index[:80]:
lon,lat = after_sorted_data.loc[indexs].values[0],after_sorted_data.loc[indexs].values[1]
x,y = m(lon,lat)
m.plot(x,y,'wo',markersize = 10*(after_sorted_data.loc[indexs].values[2]/after_sorted_data.loc[0].values[2]))
'''
plt.title("Worldwide Earthquake")
plt.show()
#indexs-len(df.index)+80
效果如下

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python決策樹預(yù)測(cè)學(xué)生成績等級(jí)實(shí)現(xiàn)詳情
這篇文章主要為介紹了python決策樹預(yù)測(cè)學(xué)生成績等級(jí),使用決策樹完成學(xué)生成績等級(jí)預(yù)測(cè),可選取部分或全部特征,分析參數(shù)對(duì)結(jié)果的影響,并進(jìn)行調(diào)參優(yōu)化,決策樹可視化進(jìn)行調(diào)參優(yōu)化分析2022-04-04
基于Python實(shí)現(xiàn)捕獲,播放和保存攝像頭視頻
這篇文章主要為大家分享一下Python操作視頻最基本的操作,包括讀取和播放視頻和保存視頻。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-04-04
Python實(shí)現(xiàn)將Sheet頁拆分成單獨(dú)的Excel文件
這篇文章主要為大家詳細(xì)介紹了如何使用 Python 將一個(gè) Excel 文件中的每個(gè)工作表(Sheet)保存成單獨(dú)的 Excel 文件,有需要的小伙伴可以了解下2025-02-02
C#中使用XPath定位HTML中的img標(biāo)簽的操作示例
隨著互聯(lián)網(wǎng)內(nèi)容的日益豐富,網(wǎng)頁數(shù)據(jù)的自動(dòng)化處理變得愈發(fā)重要,圖片作為網(wǎng)頁中的重要組成部分,其獲取和處理在許多應(yīng)用場(chǎng)景中都顯得至關(guān)重要,本文將詳細(xì)介紹如何在 C# 應(yīng)用程序中使用 XPath 定位 HTML 中的 img 標(biāo)簽,并實(shí)現(xiàn)圖片的下載,需要的朋友可以參考下2024-07-07
pytorch之torch_scatter.scatter_max()用法
這篇文章主要介紹了pytorch之torch_scatter.scatter_max()用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
pip安裝python庫時(shí)報(bào)錯(cuò)的問題解決
本文主要介紹了在Windows系統(tǒng)上解決pip命令找不到的問題的兩種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03

