import xarray import plotly.offline as py_offline from plotly.graph_objs import * import numpy as np import pandas as pd import plotly.offline as py_offline import plotly.graph_objs as go from plotly.offline import init_notebook_mode import cartopy.crs as ccrs import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['figure.figsize']=(16,9) init_notebook_mode(connected=True)
# Make shortcut to Basemap object, # not specifying projection type for this example m = Basemap()
# Make trace-generating function (return a Scatter object) defmake_scatter(x,y): return Scatter( x=x, y=y, mode='lines', line=Line(color="black"), name=' '# no name on hover )
# Functions converting coastline/country polygons to lon/lat traces defpolygons_to_traces(poly_paths, N_poly): ''' pos arg 1. (poly_paths): paths to polygons pos arg 2. (N_poly): number of polygon to convert ''' # init. plotting list data = dict( x=[], y=[], mode='lines', line=dict(color="black"), name=' ' )
for i_poly inrange(N_poly): poly_path = poly_paths[i_poly] # get the Basemap coordinates of each segment coords_cc = np.array( [(vertex[0],vertex[1]) for (vertex,code) in poly_path.iter_segments(simplify=False)] ) # convert coordinates to lon/lat by 'inverting' the Basemap projection lon_cc, lat_cc = m(coords_cc[:,0],coords_cc[:,1], inverse=True) # add plot.ly plotting options data['x'] = data['x'] + lon_cc.tolist() + [np.nan] data['y'] = data['y'] + lat_cc.tolist() + [np.nan] # traces.append(make_scatter(lon_cc,lat_cc)) return [data]
# Function generating coastline lon/lat traces defget_coastline_traces(): poly_paths = m.drawcoastlines().get_paths() # coastline polygon paths N_poly = 91# use only the 91st biggest coastlines (i.e. no rivers) return polygons_to_traces(poly_paths, N_poly)
# Function generating country lon/lat traces defget_country_traces(): poly_paths = m.drawcountries().get_paths() # country polygon paths N_poly = len(poly_paths) # use all countries return polygons_to_traces(poly_paths, N_poly)
# Get list of of coastline, country, and state lon/lat traces traces_cc = get_coastline_traces()
# Functions converting coastline/country polygons to lon/lat traces defpolygons_to_traces_cartopy(poly_paths, N_poly, target_crs, orig_crs): ''' pos arg 1. (poly_paths): paths to polygons pos arg 2. (N_poly): number of polygon to convert ''' # init. plotting list data = dict( x=[], y=[], hovertext=[], hoverinfo='text', mode='lines', line=Line(color="black"), name=' ' )
for i_poly inrange(N_poly): poly_path = poly_paths[i_poly] # get the Basemap coordinates of each segment coords_cc = np.array( [(vertex[0],vertex[1]) for (vertex,code) in poly_path.iter_segments(simplify=False)] )
defdegree2radians(degree): # convert degress to radians return degree * np.pi/180
defmapping_to_sphere(lon, lat, radius=1): #this function maps the points of coords (lon, lat) to points onto the sphere of radius radius lon=np.array(lon, dtype=np.float64) lat=np.array(lat, dtype=np.float64) lon=degree2radians(lon) lat=degree2radians(lat) xs=radius*np.cos(lon)*np.cos(lat) ys=radius*np.sin(lon)*np.cos(lat) zs=radius*np.sin(lat) return xs, ys, zs
# here the radius is slightly greater than 1 # to ensure lines visibility; xs, ys, zs = mapping_to_sphere(traces_cc[0]['x'], traces_cc[0]['y'], radius = 1.01)
traces_3d = dict(type='scatter3d', x=xs, y=ys, z=zs, mode='lines', line=dict(color='black', width=1), name=' ', # no name on hover hoverinfo='skip'# disable hover information ) xs_grat, ys_grat, zs_grat = mapping_to_sphere(traces_graticules[0]['x'], traces_graticules[0]['y'], radius = 1.01) traces_grat_3d = dict(type='scatter3d', x=xs_grat, y=ys_grat, z=zs_grat, mode='lines', line=dict(color='rgb(127,127,127)', width=1), name=' ', # no name on hover hoverinfo='skip'# disable hover information )
# could not show correctly if list is adopted directly text=np.asarray([['lon: '+'{:.2f}'.format(clons[i,j])+'<br>lat: '+'{:.2f}'.format(clats[i, j]) for j inrange(ncolumns)] for i inrange(nrows)])