def draw_figures(self, history): problem = history.problem by_cluster = history.imodels_by_cluster( self.cluster_attribute) for icluster, percentage, imodels in by_cluster: misfits = history.misfits[imodels] models = history.models[imodels] mts = [] for ix, x in enumerate(models): source = problem.get_source(x) mts.append(source.pyrocko_moment_tensor()) best_mt = stats.get_best_source( problem, models, misfits).pyrocko_moment_tensor() fig = plt.figure(figsize=self.size_inch) fig.subplots_adjust(left=0., right=1., bottom=0., top=1.) axes = fig.add_subplot(1, 1, 1, aspect=1.0) if self.cluster_attribute is not None: color = cluster_color(icluster) else: color = 'black' beachball.plot_fuzzy_beachball_mpl_pixmap( mts, axes, best_mt, beachball_type='full', size=8.*math.sqrt(percentage/100.), position=(5., 5.), color_t=color, edgecolor='black', best_color=mpl_color('scarletred2')) if self.cluster_attribute is not None: axes.annotate( cluster_label(icluster, percentage), xy=(5., 0.), xycoords='data', xytext=(0., self.font_size/2.), textcoords='offset points', ha='center', va='bottom', color='black', fontsize=self.font_size) axes.set_xlim(0., 10.) axes.set_ylim(0., 10.) axes.set_axis_off() item = PlotItem( name=( 'cluster_%i' % icluster if icluster >= 0 else 'unclustered')) yield [item, fig]
def plot_pred_bayesian_mt_hist(pred_mts, best_mt=None, hist=True): fig = plt.figure(figsize=(4., 4.)) fig.subplots_adjust(left=0., right=1., bottom=0., top=1.) gs = GridSpec(3, 5) axes = fig.add_subplot(gs[0:2, :]) plot_kwargs = { 'beachball_type': 'full', 'size': 3, 'position': (5, 5), 'color_t': 'black', 'edgecolor': 'black' } beachball.plot_fuzzy_beachball_mpl_pixmap(pred_mts, axes, best_mt, **plot_kwargs) axes.set_xlim(0., 10.) axes.set_ylim(0., 10.) axes.set_axis_off() plt.axis('off') if hist is True: omegas = [] kagans = [] for mt in pred_mts: omega = omega_angle(mt.m6(), best_mt.m6()) kagan = mtm.kagan_angle(mt, best_mt) omegas.append(omega) kagans.append(kagan) ax = fig.add_subplot(gs[2, 1]) sns.distplot(omegas, kde=False) plt.xlabel('Omega angle (°)', fontsize=12) plt.ylabel('#', fontsize=12) xmin, xmax = ax.get_xlim() ax.set_xticks(np.round(np.linspace(xmin, xmax, 2), 2)) plt.xticks(fontsize=12) plt.yticks(fontsize=12) ax = fig.add_subplot(gs[2, 3]) sns.distplot(kagans, color="orange", kde=False) plt.xlabel('Kagan angle (°)', fontsize=12) plt.ylabel('#', fontsize=12) xmin, xmax = ax.get_xlim() ax.set_xticks(np.round(np.linspace(xmin, xmax, 2), 2)) plt.xticks(fontsize=12) plt.yticks(fontsize=12) plt.show() return fig
def draw_figures(self, history): problem = history.problem fig = plt.figure(figsize=self.size_inch) axes = fig.gca() gms = problem.combine_misfits(history.misfits) isort = num.argsort(gms)[::-1] gms = gms[isort] models = history.models[isort, :] kwargs = { 'beachball_type': 'full', 'size': 8, 'position': (5, 5), 'color_t': 'black', 'edgecolor': 'black' } mts = [] for ix, x in enumerate(models): if random.random() < 0.1: source = problem.get_source(x) mts.append(source.pyrocko_moment_tensor()) best_mt = mts[-1] beachball.plot_fuzzy_beachball_mpl_pixmap(mts, axes, best_mt, **kwargs) axes.set_xlim(0., 10.) axes.set_ylim(0., 10.) axes.set_axis_off() item = PlotItem( name='main', title='Fuzzy Moment Tensor', description='The opaqueness illustrates the propability' ' of all combined moment tensor solution.' 'The red lines indicate the global best solution.') return [[item, fig]]
def plot_pred_bayesian_mt(pred_mts, best_mt=None): fig = plt.figure(figsize=(4., 4.)) fig.subplots_adjust(left=0., right=1., bottom=0., top=1.) axes = fig.add_subplot(1, 1, 1) plot_kwargs = { 'beachball_type': 'full', 'size': 3, 'position': (5, 5), 'color_t': 'black', 'edgecolor': 'black' } beachball.plot_fuzzy_beachball_mpl_pixmap(pred_mts, axes, best_mt, **plot_kwargs) axes.set_xlim(0., 10.) axes.set_ylim(0., 10.) axes.set_axis_off() plt.show()
def test_fuzzy_beachball(self): from matplotlib import pyplot as plt def get_random_uniform(lower, upper, dimension=1): return (upper - lower) * num.random.rand(dimension) + lower fig = plt.figure(figsize=(4., 4.)) fig.subplots_adjust(left=0., right=1., bottom=0., top=1.) axes = fig.add_subplot(1, 1, 1) strike = 135. dip = 65. rake = 15. best_mt = mtm.MomentTensor.from_values((strike, dip, rake)) n_balls = 1000 mts = [] for i in range(n_balls): strike_dev = get_random_uniform(-15., 15.) mts.append( mtm.MomentTensor.from_values((strike + strike_dev, dip, rake))) kwargs = { 'beachball_type': 'full', 'size': 8, 'position': (5, 5), 'color_t': 'black', 'edgecolor': 'black' } beachball.plot_fuzzy_beachball_mpl_pixmap(mts, axes, best_mt, **kwargs) axes.set_xlim(0., 10.) axes.set_ylim(0., 10.) axes.set_axis_off()
def test_fuzzy_beachball(self): from matplotlib import pyplot as plt def get_random_uniform(lower, upper, dimension=1): return (upper - lower) * num.random.rand(dimension) + lower fig = plt.figure(figsize=(4., 4.)) fig.subplots_adjust(left=0., right=1., bottom=0., top=1.) axes = fig.add_subplot(1, 1, 1) strike = 135. dip = 65. rake = 15. best_mt = mtm.MomentTensor.from_values((strike, dip, rake)) n_balls = 1000 mts = [] for i in range(n_balls): strike_dev = get_random_uniform(-15., 15.) mts.append(mtm.MomentTensor.from_values( (strike + strike_dev, dip, rake))) kwargs = { 'beachball_type': 'full', 'size': 8, 'position': (5, 5), 'color_t': 'black', 'edgecolor': 'black'} beachball.plot_fuzzy_beachball_mpl_pixmap(mts, axes, best_mt, **kwargs) axes.set_xlim(0., 10.) axes.set_ylim(0., 10.) axes.set_axis_off()
def plot_map_basemap(stations=None, east_min=-119.2, east_max=-116, north_min=34.5, north_max=37.501, events=None, savename=None, preds=None, best_mts=None, pred_events=None, rect_lats=None, rect_lons=None, ticks=0.01, kmscale=5, add_grid=True, overview=False): try: from mpl_toolkits.basemap import Basemap use_basemap = True except: import cartopy.crs as ccrs import cartopy import cartopy.geodesic as cgeo from cartopy.io import srtm from cartopy.io import PostprocessedRasterSource, LocatedImage from cartopy.io.srtm import SRTM3Source, SRTM1Source use_basemap = False from matplotlib import pyplot as plt from pyrocko.plot import beachball from pyrocko import plot from obspy.imaging.beachball import beach fig, ax = plt.subplots(figsize=(12, 12)) map = Basemap(projection='merc', llcrnrlon=east_min, llcrnrlat=north_min, urcrnrlon=east_max, urcrnrlat=north_max, resolution='h', epsg=3395, ax=ax) xpixels = 1000 map.arcgisimage(service='World_Shaded_Relief', xpixels=xpixels, verbose=False, zorder=-3, colormap="gray", cmap="gray") if overview is True: map.drawmapscale(east_min + 0.35, north_min + 0.31, east_min + 0.65, north_min + 0.5, kmscale) else: map.drawmapscale(east_min + 0.05, north_min + 0.01, east_min + 0.55, north_min + 0.2, kmscale) parallels = np.arange(north_min, north_max, ticks) meridians = np.arange(east_min, east_max, ticks) if overview is True: map.drawparallels(np.round(parallels, 1), labels=[1, 0, 0, 0], fontsize=12) map.drawmeridians(np.round(meridians, 1), labels=[1, 1, 0, 1], fontsize=12, rotation=45) else: map.drawparallels(parallels, labels=[1, 0, 0, 0], fontsize=12) map.drawmeridians(meridians, labels=[1, 1, 0, 1], fontsize=12, rotation=45) if events is not None: for event in events: mt = event.moment_tensor if overview is False: if event.lat > 35.845: x, y = map(event.lon, event.lat) else: x, y = map(event.lon, event.lat) if overview is True: size = 12 else: size = 20 beachball.plot_beachball_mpl(mt, ax, beachball_type='full', size=size, position=(x, y), color_t=plot.mpl_color('scarletred2'), linewidth=1.0, zorder=1) if stations is not None: lats = [s.lat for s in stations] lons = [s.lon for s in stations] labels = ['.'.join(s.nsl()) for s in stations] x_station, y_station = map(lons, lats) map.scatter(x_station, y_station, marker="^", s=36, c="g", zorder=8) for k, label in enumerate(labels): plt.text(x_station[k], y_station[k], str(label), fontsize=12) if rect_lats is not None: import matplotlib.patches as patches if add_grid is True: for lat in rect_lats: for lon in rect_lons: x, y = map(lon, lat) rect = patches.Rectangle((x, y), 1800, 2200, linewidth=1, edgecolor='r', facecolor='none') # Add the patch to the Axes ax.add_patch(rect) max_lat_rect = np.max(rect_lats) min_lat_rect = np.min(rect_lats) max_lon_rect = np.max(rect_lons) min_lon_rect = np.min(rect_lons) xmin, ymin = map(min_lon_rect, max_lat_rect) xmax, ymax = map(max_lon_rect, min_lat_rect) width = xmax - xmin length = ymin - ymax if overview is True: rect = patches.Rectangle( (xmin, ymax), width, length, linewidth=5, edgecolor='k', facecolor='none', zorder=7, ) # Add the patch to the Axes ax.add_patch(rect) if preds is None and best_mts is not None: k = 1 for best_mt, ev in zip(best_mts, pred_events): mt = ev.moment_tensor x, y = map(ev.lon, ev.lat) plt.text(x, y + 0.02, str(k), fontsize=42, zorder=9, color="k") k = k + 1 beachball.plot_beachball_mpl(mt, ax, beachball_type='full', size=22., position=(x, y), color_t=plot.mpl_color('blue'), linewidth=1.0, zorder=3) if preds is not None: for pred_mts, best_mt, ev in zip(preds, best_mts, pred_events): x, y = map(ev.lon, ev.lat) plot_kwargs = { 'beachball_type': 'full', 'size': 500, 'position': (x, y), 'color_t': 'black', 'edgecolor': 'black', 'zorder': 3, } beachball.plot_fuzzy_beachball_mpl_pixmap(pred_mts, ax, best_mt, **plot_kwargs) plt.show()
best_mt = mtm.MomentTensor.from_values((strike, dip, rake)) def get_random_uniform(lower, upper, dimension=1): ''' Help function to pertubate the best solution ''' return (upper - lower) * num.random.rand(dimension) + lower mts = [] for i in range(n_balls): strike_dev = get_random_uniform(-15., 15.) mts.append(mtm.MomentTensor.from_values((strike + strike_dev, dip, rake))) plot_kwargs = { 'beachball_type': 'full', 'size': 8, 'position': (5, 5), 'color_t': 'black', 'edgecolor': 'black' } beachball.plot_fuzzy_beachball_mpl_pixmap(mts, axes, best_mt, **plot_kwargs) # Decorate the axes axes.set_xlim(0., 10.) axes.set_ylim(0., 10.) axes.set_axis_off() plt.show()
best_mt = mtm.MomentTensor.from_values((strike, dip, rake)) def get_random_uniform(lower, upper, dimension=1): ''' Help function to pertubate the best solution ''' return (upper - lower) * num.random.rand(dimension) + lower mts = [] for i in range(n_balls): strike_dev = get_random_uniform(-15., 15.) mts.append(mtm.MomentTensor.from_values( (strike + strike_dev, dip, rake))) plot_kwargs = { 'beachball_type': 'full', 'size': 8, 'position': (5, 5), 'color_t': 'black', 'edgecolor': 'black' } beachball.plot_fuzzy_beachball_mpl_pixmap(mts, axes, best_mt, **plot_kwargs) # Decorate the axes axes.set_xlim(0., 10.) axes.set_ylim(0., 10.) axes.set_axis_off() plt.show()