本项目是利用五年左右的世界地震数据,通过python库的熊猫,matplotlib库、基础图库等进行数据可视化,绘制出地震散点图,主要代码如下所示
从__future__进口部门 熊猫作为pd导入 从熊猫进口系列,DataFrame 进口numpy np 从matplotlib。补丁进口多边形 chi_provinces=['北京”、“天津”,“上海”,“重庆”, “河北”、“山西”,“辽宁”,“吉林”, “黑龙江”、“江苏”、“浙江”,“安徽”, “福建”、“江西”,“山东”、“河南”, “湖北”、“湖南”、“广东”,“海南”, “四川”、“贵州”,“云南”、“陕西”, “甘肃”、“青海”、“台湾”,“内蒙古”, “广西”、“西藏”,“宁夏”、“新疆”, “香港,澳门']#中国各省的列表 def is_in_china (str): 如果在chi_provinces str (2):: 还真 其他: 返回假 def convert_data_2014 (x): 试一试: 返回浮点数(x.strip ()) 除了ValueError: 返回x 除了AttributeError: 返回x def format_lat_lon (x): 试一试: 返回x/100 (TypeError除外): 返回np.nan df=pd.read_excel (r 'C:/用户/GGWS/桌面/shuju/201601 - 12. - xls”) df=df.append (pd.read_excel (r 'C:/用户/GGWS/桌面/shuju/201201 - 12. - xls”), ignore_index=True) df=df.append (pd.read_excel (r 'C:/用户/GGWS/桌面/shuju/shuju.xls’), ignore_index=True) df=df.append (pd.read_excel (r 'C:/用户/GGWS/桌面/shuju/201501 - 12. - xls”), ignore_index=True) df_2014=pd.read_excel (r 'C:/用户/GGWS/桌面/shuju/201401 - 12. - xls”) 2014 #要介绍静力学独立因为特定的格式和类型的数据列在这个数据集不同于其他人 df(经度的)=df(经度的)苹果(convert_data_2014) df(“纬度”)=df(“纬度”)苹果(convert_data_2014) df_2014[经度的]=df_2014[经度的]苹果(convert_data_2014) df_2014[“纬度”]=df_2014(“纬度”)苹果(convert_data_2014) df=df。追加(df_2014 ignore_index=True) df=df[[纬度,经度,“大小”,“引用的地方”,“时间”]]#只有四列保存为静力学的价值 df[[经度,纬度的]]=df[[经度,纬度的]].applymap (format_lat_lon) #使用功能“applymap”将经度和纬度静力学的格式 df=df.dropna(轴=0='任何')#所有行有NaN值下降 format_magnitude=λx:浮动(str (x) .strip (ML)) df[‘级’]=df(“级”)苹果(format_magnitude) # df=df (df('引用的地方']苹果(is_in_china)] lon_mean=(df(“经度”)。groupby (df['引用的地方'])).mean () lat_mean=(df(“纬度”)。groupby (df['引用的地方'])).mean () group_counts=(df(“级”)。groupby (df['引用的地方'])).count () after_agg_data=https://www.yisu.com/zixun/pd.concat ([lon_mean、lat_mean group_counts],轴=1) after_agg_data。重命名(列={“大小”:“计数”},原地=True) #聚合后分组数据 after_sorted_data=https://www.yisu.com/zixun/after_agg_data.sort_values(=凹剖?提升=False) new_index=np.arange (len (after_sorted_data.index)) after_sorted_data。指数=new_index paint_data=https://www.yisu.com/zixun/after_sorted_data [after_sorted_data['重要']祝辞=after_sorted_data[“计数”][80]] 进口matplotlib。pyplot作为plt 从mpl_toolkits。技术进口技术 plt.figure (figsize=(16日8)) m=基础图() m。readshapefile (r 'C:/用户/GGWS/桌面/jb/gadm36_CHN_1”、“州”,drawbounds=True) ax=plt.gca () “‘ nshape,赛格列举(m.states): 保利=多边形(赛格,facecolor=' r ') ax.add_patch(聚) “‘ m.drawcoastlines(线宽=0.5) m.drawcountries(线宽=0.5) m.shadedrelief () 在df.index指标: lon2 lat2=df.loc(指标). values [1] df.loc(指标). values [0] x, y=m (lon2 lat2) m。情节(x, y,“罗”,markersize=0.5) #获取经度值 “‘ 在after_sorted_data.index指标[80]: 经度,纬度=after_sorted_data.loc(指标). values [0] after_sorted_data.loc(指标). values [1] x, y=m(经度、纬度) m。情节(x, y,‘我们’,markersize=10 * (after_sorted_data.loc(指标). values [2]/after_sorted_data.loc [0] . values [2])) “‘ plt。标题(“全球地震”) plt.show () # indexs-len (df.index) + 80
效果如下