def zoommap(vals, title, label): fig = plt.figure(figsize=(12,8)) plt.subplots_adjust(left=0.05,right=0.95,top=0.90,bottom=0.05, wspace=0.15,hspace=0.05) ax = plt.subplot(211) m = Basemap(projection = 'mill', llcrnrlat = -45, llcrnrlon = -160, urcrnrlat= 82, urcrnrlon = 170, resolution = 'c') m.drawcoastlines(linewidth = 0.5) plt.subplots_adjust(left=0.05,right=0.95,top=0.90,bottom=0.05, wspace=0.15,hspace=0.05) for row in vals: x,y = m(vals.lon.values, vals.lat.values) m.scatter(x,y, s = 20, color = vals.color.values) ax.text(0.03,0.95, label, size = 12, horizontalalignment='center', verticalalignment='center', transform=ax.transAxes) plt.title(title, fontsize = 12) #Zoom Europe axins_1 = zoomed_inset_axes(ax, 2, loc=2, bbox_to_anchor=(0.42, 0.48), bbox_transform=ax.figure.transFigure) axins_1.scatter(x, y, s = 20, c = vals.color.values) m.drawcoastlines(linewidth = 0.5) x2,y2 = m(-12,35) x3,y3 = m(40,65) axins_1.set_xlim(x2,x3) axins_1.set_ylim(y2,y3) axes = mark_inset(ax, axins_1, loc1=1, loc2=2, linewidth=1) #Zoom Australia axins_2 = zoomed_inset_axes(ax, 2.2, loc=3, bbox_to_anchor=(0.61, 0.255), bbox_transform=ax.figure.transFigure) axins_2.scatter(x, y, s = 20, c = vals.color.values) m.drawcoastlines(linewidth = 0.5) x2,y2 = m(110,-43) x3,y3 = m(155,-10) axins_2.set_xlim(x2,x3) axins_2.set_ylim(y2,y3) axes = mark_inset(ax, axins_2, loc1=1, loc2=2,linewidth=1) #Zoom US axins_3 = zoomed_inset_axes(ax, 1.6, loc=3, bbox_to_anchor=(0.21, 0.25), bbox_transform=ax.figure.transFigure) axins_3.scatter(x, y, s = 20, c = vals.color.values) m.drawcoastlines(linewidth = 0.5) x2,y2 = m(-130,22) x3,y3 = m(-60,63) axins_3.set_xlim(x2,x3) axins_3.set_ylim(y2,y3) axes = mark_inset(ax, axins_3, loc1=1, loc2=2, linewidth=1) return(fig, axes)
figure() d = NormalDistr(1, 1) * NormalDistr(1, 1) demo_distr(d) #show() figure() d = NormalDistr(2, 1) * NormalDistr(2, 1) demo_distr(d) #show() figure() d = NormalDistr(3, 1) * NormalDistr(3, 1) d.plot() d.hist() ax = gca() axins = zoomed_inset_axes(ax, 6, loc=1) d.plot(xmin=-1.5, xmax=1.5) axins.set_xlim(-1.5, 1.5) xticks(rotation="vertical") axins.set_ylim(0, 0.01) mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") #show() figure() d = NormalDistr(4, 1) * NormalDistr(4, 1) d.plot() d.hist() ax = gca() axins = zoomed_inset_axes(ax, 12000, loc=1) d.plot(xmin=-.001, xmax=.001) axins.set_xlim(-.001, .001)
return z, (-3, 4, -4, 3) fig = plt.figure(1, [5, 4]) ax = fig.add_subplot(111) # prepare the demo image Z, extent = get_demo_image() Z2 = np.zeros([150, 150], dtype="d") ny, nx = Z.shape Z2[30:30 + ny, 30:30 + nx] = Z # extent = [-3, 4, -4, 3] ax.imshow(Z2, extent=extent, interpolation="nearest", origin="lower") axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6 axins.imshow(Z2, extent=extent, interpolation="nearest", origin="lower") # sub region of the original image x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9 axins.set_xlim(x1, x2) axins.set_ylim(y1, y2) plt.xticks(visible=False) plt.yticks(visible=False) # draw a bbox of the region of the inset axes in the parent axes and # connecting lines between the bbox and the inset axes area mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") plt.draw()
grid = AxesGrid(fig, 111, nrows_ncols=(3, 2), axes_pad=0.0, share_all=True, ) # loop on each map for i, fname in enumerate(fname_ext): data = np.flipud(pyfits.fitsopen(fname)[0].data.T) if i == 0: data /= 8. data = data ** .25 data[np.isnan(data)] = 0 extent = [0., 192., 0., 192.] im = grid[i].imshow(data, extent=extent, interpolation="nearest") grid[i].text(10, 170, letters[i], fontsize=20, color="white") data_zoom = data[135:155, 80:100,] axins = zoomed_inset_axes(grid[i], 2, loc=3) # zoom = 6 extent = [80., 100., 192. - 155., 192. - 135, ] im2 = axins.imshow(data_zoom, extent=extent, interpolation="nearest") im2.set_clim([data.min(), data.max()]) plt.xticks(visible=False) plt.yticks(visible=False) im.set_clim([0., .3]) im2.set_clim([0., .15]) mark_inset(grid[i], axins, loc1=2, loc2=4, fc="none", ec="0.5") fontsize=20 posx = 10 posy = 170 grid[-1].text(posx, posy, "(a): PL", fontsize=fontsize, color="black") posy -= 25 grid[-1].text(posx, posy, "(b): PLI", fontsize=fontsize, color="black")
for j, c in enumerate(r): axis.text(i + subextent[0] + 0.5, j + subextent[2] + 0.5, c, weight='demibold', horizontalalignment='center', verticalalignment='center', color=_determine_ideal_value(c, ma=array.max())) if __name__ == '__main__': tux = skimage.io.imread('./tux_icon-33px.png') fig, ax = plt.subplots(figsize=(5, 5)) extent0 = [0, 33, 0, 36] ax.imshow(tux, extent=extent0, interpolation='nearest') axins = zoomed_inset_axes(ax, 3.4, loc=3) extent1 = [18, 23, 18, 23] overlay_numbers(axins, tux, extent0, extent1, interpolation='none') axins.set_xlim(*extent1[:2]) axins.set_ylim(*extent1[2:]) plt.xticks(visible=False) plt.yticks(visible=False) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") ax.axis('off') fig.tight_layout() fig.savefig('./tux_inset.png', dpi=150) # plt.show()
out_fname = fname + '.png' print('displaying ' + fname) title_str = fname.split(os.sep)[-1] t = np.flipud(pyfits.fitsopen(fname_ext)[0].data.T) fig = plt.figure(1, [5,4]) ax = fig.add_subplot(111) #imshow(t , interpolation="nearest") #imshow((t - t.min())) ** .25, interpolation="nearest") tt = t ** .25 tt[np.isnan(tt)] = 0 extent = [0., 192., 0., 192.] ax.imshow(tt, extent=extent, interpolation="nearest") tzoom = tt[135:155, 80:100,] axins = zoomed_inset_axes(ax, 2, loc=3) # zoom = 6 extent = [80., 100., 192. - 155., 192. - 135, ] im = axins.imshow(tzoom, extent=extent, interpolation="nearest") im.set_clim([tt.min(), tt.max()]) plt.xticks(visible=False) plt.yticks(visible=False) #x1, x2, y1, y2 = 80., 100., 135., 155., #axins.set_xlim(x1, x2) #axins.set_ylim(y1, y2) mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") #plt.title(title_str) #plt.colorbar() #plt.xlabel('Right Ascension') #plt.ylabel('Declination') plt.show()
import matplotlib.pyplot as plt from mpl_toolkits.axes_grid.inset_locator import zoomed_inset_axes from mpl_toolkits.axes_grid.inset_locator import mark_inset import numpy as np def get_demo_image(): from matplotlib.cbook import get_sample_data import numpy as np f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) z = np.load(f) return z, (-3,4,-4,3) fig = plt.figure(1, [5,4]) ax = fig.add_subplot(111) Z, extent = get_demo_image() Z2 = np.zeros([150, 150], dtype="d") ny, nx = Z.shape Z2[30:30+ny, 30:30+nx] = Z ax.imshow(Z2, extent=extent, interpolation="nearest", origin="lower") axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6 axins.imshow(Z2, extent=extent, interpolation="nearest", origin="lower") x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9 axins.set_xlim(x1, x2) axins.set_ylim(y1, y2) plt.xticks(visible=False) plt.yticks(visible=False) mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") plt.draw() plt.show()
def zoommap(vals, title, label): fig = plt.figure(figsize=(12, 8)) plt.subplots_adjust(left=0.05, right=0.95, top=0.90, bottom=0.05, wspace=0.15, hspace=0.05) ax = plt.subplot(211) m = Basemap(projection='mill', llcrnrlat=-45, llcrnrlon=-160, urcrnrlat=82, urcrnrlon=170, resolution='c') x, y = m(lons, lats) m.drawcoastlines(linewidth=0.5) plt.subplots_adjust(left=0.05, right=0.95, top=0.90, bottom=0.05, wspace=0.15, hspace=0.05) cmap = matplotlib.colors.ListedColormap( ['#d73027', '#fc8d59', '#fee090', '#e0f3f8', '#91bfdb', '#4575b4']) bounds = [0, 0.3, 0.5, 0.7, 0.8, 0.9, 1.0] norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N) m.scatter(x, y, s=40, c=vals, cmap=cmap, norm=norm) plt.colorbar(orientation="vertical", boundaries=bounds, spacing='proportional', ticks=bounds) ax.text(0.03, 0.95, label, size=12, horizontalalignment='center', verticalalignment='center', transform=ax.transAxes) plt.title(title, fontsize=14) #Zoom Europe axins_1 = zoomed_inset_axes(ax, 2, loc=2, bbox_to_anchor=(0.396, 0.48), bbox_transform=ax.figure.transFigure) axins_1.scatter(x, y, s=20, c=vals, cmap=cmap, norm=norm) m.drawcoastlines(linewidth=0.5) x2, y2 = m(-12, 35) x3, y3 = m(40, 65) axins_1.set_xlim(x2, x3) axins_1.set_ylim(y2, y3) axes = mark_inset(ax, axins_1, loc1=1, loc2=2, linewidth=1) #Zoom Australia axins_2 = zoomed_inset_axes(ax, 2.2, loc=3, bbox_to_anchor=(0.59, 0.255), bbox_transform=ax.figure.transFigure) axins_2.scatter(x, y, s=20, c=vals, cmap=cmap, norm=norm) m.drawcoastlines(linewidth=0.5) x2, y2 = m(110, -43) x3, y3 = m(155, -10) axins_2.set_xlim(x2, x3) axins_2.set_ylim(y2, y3) axes = mark_inset(ax, axins_2, loc1=1, loc2=2, linewidth=1) #Zoom US axins_3 = zoomed_inset_axes(ax, 1.6, loc=3, bbox_to_anchor=(0.19, 0.25), bbox_transform=ax.figure.transFigure) axins_3.scatter(x, y, s=20, c=vals, cmap=cmap, norm=norm) m.drawcoastlines(linewidth=0.5) x2, y2 = m(-130, 22) x3, y3 = m(-60, 63) axins_3.set_xlim(x2, x3) axins_3.set_ylim(y2, y3) axes = mark_inset(ax, axins_3, loc1=1, loc2=2, linewidth=1) return (fig, axes)
j + subextent[2] + 0.5, c, weight='demibold', horizontalalignment='center', verticalalignment='center', color=_determine_ideal_value(c, ma=array.max())) if __name__ == '__main__': tux = skimage.io.imread('./tux_icon-33px.png') fig, ax = plt.subplots(figsize=(5, 5)) extent0 = [0, 33, 0, 36] ax.imshow(tux, extent=extent0, interpolation='nearest') axins = zoomed_inset_axes(ax, 3.4, loc=3) extent1 = [18, 23, 18, 23] overlay_numbers(axins, tux, extent0, extent1, interpolation='none') axins.set_xlim(*extent1[:2]) axins.set_ylim(*extent1[2:]) plt.xticks(visible=False) plt.yticks(visible=False) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") ax.axis('off') fig.tight_layout() fig.savefig('./tux_inset.png', dpi=150) # plt.show()
nrows_ncols=(3, 2), axes_pad=0.0, share_all=True, ) # loop on each map for i, fname in enumerate(fname_ext): data = np.flipud(pyfits.fitsopen(fname)[0].data.T) if i == 0: data /= 8. data = data**.25 data[np.isnan(data)] = 0 extent = [0., 192., 0., 192.] im = grid[i].imshow(data, extent=extent, interpolation="nearest") grid[i].text(10, 170, letters[i], fontsize=20, color="white") data_zoom = data[135:155, 80:100, ] axins = zoomed_inset_axes(grid[i], 2, loc=3) # zoom = 6 extent = [ 80., 100., 192. - 155., 192. - 135, ] im2 = axins.imshow(data_zoom, extent=extent, interpolation="nearest") im2.set_clim([data.min(), data.max()]) plt.xticks(visible=False) plt.yticks(visible=False) im.set_clim([0., .3]) im2.set_clim([0., .3]) mark_inset(grid[i], axins, loc1=2, loc2=4, fc="none", ec="0.5") grid[0].set_xticks(())
fig = plt.figure(1, [5.5, 3]) # first subplot ax = fig.add_subplot(1,2,1) ax.set_aspect(1.) axins = inset_axes(ax, width="30%", # width = 30% of parent_bbox height=1., # height : 1 inch loc=3) plt.xticks(visible=False) plt.yticks(visible=False) # second subplot ax = fig.add_subplot(1,2,2) ax.set_aspect(1.) axins = zoomed_inset_axes(ax, 0.5, loc=1) # zoom = 0.5 plt.xticks(visible=False) plt.yticks(visible=False) add_sizebar(ax, 0.5) add_sizebar(axins, 0.5) plt.draw() plt.show()
pl.yticks(fontsize=15) pl.ylim(0, 1) pl.xlim(0, 90) pl.grid() # inset plot # akuederle.com/matplotlib-zoomed-up-inset # 'upper right' : 1, # 'upper left' : 2, # 'lower left' : 3, # 'lower right' : 4 fig, ax = plt.subplots() # create a new figure with a default 111 subplot ax.plot(overview_data_x, overview_data_y) # zoom-factor: 2.5, location: upper-left axins = zoomed_inset_axes(ax, 2.5, loc=2) locate the position of inset # left down width hight fraction of main axix ip = InsetPosition(ax, [0.1, 0.05, 0.4, 0.3]) axins.set_axes_locator(ip) axins.plot(overview_data_x, overview_data_y) x1, x2, y1, y2 = 47, 60, 3.7, 4.6 # specify the limits axins.set_xlim(x1, x2) # apply the x-limits axins.set_ylim(y1, y2) # apply the y-limits plt.yticks(visible=False) plt.xticks(visible=False) mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") # Error bar:
fig = plt.figure(1, [5.5, 3]) # first subplot ax = fig.add_subplot(1, 2, 1) ax.set_aspect(1.) axins = inset_axes( ax, width="30%", # width = 30% of parent_bbox height=1., # height : 1 inch loc=3) plt.xticks(visible=False) plt.yticks(visible=False) # second subplot ax = fig.add_subplot(1, 2, 2) ax.set_aspect(1.) axins = zoomed_inset_axes(ax, 0.5, loc=1) # zoom = 0.5 plt.xticks(visible=False) plt.yticks(visible=False) add_sizebar(ax, 0.5) add_sizebar(axins, 0.5) plt.draw() plt.show()
# formatting the plot ax.xaxis.set_minor_locator(pylab.MultipleLocator(5)) ax.yaxis.set_minor_locator(pylab.MultipleLocator(0.1)) ax.grid(b=True,which="minor",axis='x') ax.grid(b=True,which="minor",axis='y') ax.set_xticks(range(1850,2030,10)) ax.set_xlim(1843,2021) ax.set_ylim(-1.03,0.89) ax.set_xlabel("Year") legend = ax.legend(loc="upper left",fontsize=14) frame = legend.get_frame() frame.set_facecolor('1.0') ax.set_ylabel("Anomaly") # adding an inset axis to view the downturn at the end better inset_axes = zoomed_inset_axes(ax, 3, loc=4) inset_axes.scatter(df2.Date,df2.Anomaly,s=15,marker='o',facecolor="1.0",lw=0.5,edgecolor="0.0") inset_axes.plot(df2.Date[s12:-e12],yr1LP2,'-y',label='Annual LP') inset_axes.plot(df2.Date[s152:-e152],yr15LP2,'-g',label='>15 yr LP') inset_axes.plot(df2.Date[s752:-e752],yr75LP2,'-b',label='>30 yr LP') inset_axes.plot(df2.Date,yr15SG2,'--r',label='S-G 15 yr') x1, x2, y1, y2 = 2000, 2015, 0.3, 0.6 inset_axes.set_xlim(x1, x2) inset_axes.set_ylim(y1, y2) inset_axes.set_xticks([]) inset_axes.set_yticks([]) inset_axes.set_axis_bgcolor("1.0") ax.set_title("HadCrut4 Monthly Anomaly Smoothing by CTRM and Savitsky-Golay") mark_inset(ax, inset_axes, loc1=1, loc2=2, fc="none", ec="0.0"); pylab.show()
figure() d = NormalDistr(1,1) * NormalDistr(1,1) demo_distr(d) #show() figure() d = NormalDistr(2,1) * NormalDistr(2,1) demo_distr(d) #show() figure() d = NormalDistr(3,1) * NormalDistr(3,1) d.plot() d.hist() ax=gca() axins = zoomed_inset_axes(ax, 6, loc=1) d.plot(xmin=-1.5, xmax=1.5) axins.set_xlim(-1.5, 1.5) xticks(rotation="vertical") axins.set_ylim(0, 0.01) mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") #show() figure() d = NormalDistr(4,1) * NormalDistr(4,1) d.plot() d.hist() ax=gca() axins = zoomed_inset_axes(ax, 12000, loc=1) d.plot(xmin=-.001, xmax=.001) axins.set_xlim(-.001, .001)
z = np.load(f) # z is a numpy array of 15x15 return z, (-3,4,-4,3) fig = plt.figure(1, [5,4]) ax = fig.add_subplot(111) Z, extent = get_demo_image() ax.set(aspect=1, xlim=(-15, 15), ylim=(-20, 5)) axins = zoomed_inset_axes(ax, 2, loc=2) # zoom = 6 im = axins.imshow(Z, extent=extent, interpolation="nearest", origin="lower") plt.xticks(visible=False) plt.yticks(visible=False) # colorbar cax = inset_axes(axins, width="5%", # width = 10% of parent_bbox width height="100%", # height : 50% loc=3, bbox_to_anchor=(1.05, 0., 1, 1), bbox_transform=axins.transAxes, borderpad=0,
import matplotlib.pyplot as plt from mpl_toolkits.axes_grid.inset_locator import inset_axes, zoomed_inset_axes fig, ax1 = plt.subplots() axins = inset_axes(ax1, 1, 1, loc=2, bbox_to_anchor=(0.2, 0.55), bbox_transform=ax1.figure.transFigure) axins_zoomed = zoomed_inset_axes(ax1, 3.5, loc=2, bbox_to_anchor=(0.2, 0.55), bbox_transform=ax1.figure.transFigure) data_gelf = range(10) ax1.plot(data_gelf, color='red') axins.set_xlim(3, 4) axins.set_ylim(3, 4) axins.plot(data_gelf, color='red') axins_zoomed.set_xlim(3, 4) axins_zoomed.set_ylim(3, 4) axins_zoomed.plot(data_gelf, color='red') plt.show()