def plotGsrResidue(theta, phi, residue, optTheta, optPhi, mvabTheta=None, mvabPhi=None, mvubTheta=None, mvubPhi=None): fig = figure() fig.clf() # some matplotlib setup stuff which I don't fully understand but it works tr = Affine2D().scale(pi/180., 1.) + PolarAxes.PolarTransform() extreme_finder = angle_helper.ExtremeFinderCycle(20, 20, lon_cycle = 360, lat_cycle = None, lon_minmax = None, lat_minmax = (0, inf), ) grid_locator1 = angle_helper.LocatorDMS(12) tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1 ) ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper) fig.add_subplot(ax1) ax1.axis["right"].major_ticklabels.set_visible(True) ax1.axis["top"].major_ticklabels.set_visible(True) ax1.axis["right"].get_helper().nth_coord_ticks=0 ax1.axis["bottom"].get_helper().nth_coord_ticks=1 # draw the filled contoured map in polar coordinates ax1.contour(transpose(mat(theta))*mat(cos(phi*pi/180)), transpose(mat(theta))*mat(sin(phi*pi/180)), 1/transpose(reshape(residue, (phi.size,-1))), 100, lw=0.1) cc = ax1.contourf(transpose(mat(theta))*mat(cos(phi*pi/180)), transpose(mat(theta))*mat(sin(phi*pi/180)), 1/transpose(reshape(residue, (phi.size,-1))), 100) # remove gaps between the contour lines for c in cc.collections: c.set_antialiased(False) # show the MVAB direction if mvabTheta is not None and mvabPhi is not None: ax1.plot(mvabTheta*cos(mvabPhi*pi/180), mvabTheta*sin(mvabPhi*pi/180), 'sk', markersize=8) # show the MVUB direction if mvubTheta is not None and mvubPhi is not None: ax1.plot(mvubTheta*cos(mvubPhi*pi/180), mvubTheta*sin(mvubPhi*pi/180), 'dk', markersize=8) # show the optimal direction ax1.plot(optTheta*cos(optPhi*pi/180), optTheta*sin(optPhi*pi/180), '.k', markersize=15) # aspect and initial axes limits ax1.set_aspect(1.) ax1.set_xlim(-90, 90) ax1.set_ylim(-90, 90) # add grid ax1.grid(True) # add colobar cb = colorbar(cc, pad=0.07) cb.locator = MaxNLocator(14) cb.update_ticks() cb.set_label(r"$1/\tilde{\mathcal{R}}$") # save if toSave: savefig(resultsDir+'/eps/gsr_ResidualMap.eps', format='eps') savefig(resultsDir+'/png/gsr_ResidualMap.png', format='png')
def plotGsrResidue(theta, phi, residue, optTheta, optPhi, mvabTheta=None, mvabPhi=None, mvubTheta=None, mvubPhi=None): fig = figure() fig.clf() # some matplotlib setup stuff which I don't fully understand but it works tr = Affine2D().scale(pi / 180., 1.) + PolarAxes.PolarTransform() extreme_finder = angle_helper.ExtremeFinderCycle( 20, 20, lon_cycle=360, lat_cycle=None, lon_minmax=None, lat_minmax=(0, inf), ) grid_locator1 = angle_helper.LocatorDMS(12) tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1) ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper) fig.add_subplot(ax1) ax1.axis["right"].major_ticklabels.set_visible(True) ax1.axis["top"].major_ticklabels.set_visible(True) ax1.axis["right"].get_helper().nth_coord_ticks = 0 ax1.axis["bottom"].get_helper().nth_coord_ticks = 1 # draw the filled contoured map in polar coordinates ax1.contour(transpose(mat(theta)) * mat(cos(phi * pi / 180)), transpose(mat(theta)) * mat(sin(phi * pi / 180)), 1 / transpose(reshape(residue, (phi.size, -1))), 100, lw=0.1) cc = ax1.contourf( transpose(mat(theta)) * mat(cos(phi * pi / 180)), transpose(mat(theta)) * mat(sin(phi * pi / 180)), 1 / transpose(reshape(residue, (phi.size, -1))), 100) # remove gaps between the contour lines for c in cc.collections: c.set_antialiased(False) # show the MVAB direction if mvabTheta is not None and mvabPhi is not None: ax1.plot(mvabTheta * cos(mvabPhi * pi / 180), mvabTheta * sin(mvabPhi * pi / 180), 'sk', markersize=8) # show the MVUB direction if mvubTheta is not None and mvubPhi is not None: ax1.plot(mvubTheta * cos(mvubPhi * pi / 180), mvubTheta * sin(mvubPhi * pi / 180), 'dk', markersize=8) # show the optimal direction ax1.plot(optTheta * cos(optPhi * pi / 180), optTheta * sin(optPhi * pi / 180), '.k', markersize=15) # aspect and initial axes limits ax1.set_aspect(1.) ax1.set_xlim(-90, 90) ax1.set_ylim(-90, 90) # add grid ax1.grid(True) # add colobar cb = colorbar(cc, pad=0.07) cb.locator = MaxNLocator(14) cb.update_ticks() cb.set_label(r"$1/\tilde{\mathcal{R}}$") # save if toSave: savefig(resultsDir + '/eps/gsr_ResidualMap.eps', format='eps') savefig(resultsDir + '/png/gsr_ResidualMap.png', format='png')
class PolarPlot: ''' plots heading angle and signal strength in polar coor in 2d''' def __init__(self): plt.ion() self.fig = plt.figure(num=2, figsize=(10,7)) tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform() # 20, 20 : number of sampling points along x, y direction extreme_finder = angle_helper.ExtremeFinderCycle(50, 50, lon_cycle = 360, lat_cycle = None, lon_minmax = None, lat_minmax = (0, np.inf), ) grid_locator1 = angle_helper.LocatorDMS(15) tick_formatter1 = angle_helper.FormatterDMS() # And also uses an appropriate formatter. Note that,the # acceptable Locator and Formatter class is a bit different than # that of mpl's, and you cannot directly use mpl's Locator and # Formatter here (but may be possible in the future). grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1 ) self.ax1 = SubplotHost(self.fig, 1, 1, 1, grid_helper=grid_helper) # make ticklabels of right and top axis visible. self.ax1.axis["right"].major_ticklabels.set_visible(True) self.ax1.axis["top"].major_ticklabels.set_visible(True) self.ax1.axis["left"].major_ticklabels.set_visible(True) # let right axis shows ticklabels for 1st coordinate (angle) self.ax1.axis["right"].get_helper().nth_coord_ticks=0 # let bottom axis shows ticklabels for 1st coordinate (angle) self.ax1.axis["bottom"].get_helper().nth_coord_ticks=0 self.ax1.axis["left"].get_helper().nth_coord_ticks=0 temp = self.ax1.set_title('Signal strength & heading polar plots') temp.set_y(1.05) self.ax1.grid(True) # insert x and y axises self.ax = self.fig.add_subplot(self.ax1) self.ax1.spines['left'].set_position('center') self.ax1.spines['right'].set_color('red') self.ax1.spines['bottom'].set_position('center') self.ax1.spines['top'].set_color('none') self.ax1.spines['left'].set_smart_bounds(True) self.ax1.spines['bottom'].set_smart_bounds(True) self.ax1.xaxis.set_ticks_position('bottom') self.ax1.yaxis.set_ticks_position('left') self.ax1.axhline(linewidth=2, color='blue') self.ax1.axvline(linewidth=2, color='blue') # label x and y axises manually ticks = np.linspace(0, 255, 6) offset = np.zeros([1,255]) for i in range(1,5): self.ax1.annotate(str(ticks[i]),size=10, xy=(ticks[i], -15)) blah = self.ax1.plot(ticks[i],0, 'bo') self.ax1.annotate(str(ticks[i]),size=10, xy=(5, ticks[i])) blah = self.ax1.plot(0,ticks[i], 'bo') # annotate figure bbox_props = dict(boxstyle="round", fc="w", ec="0.5", alpha=0.9) # self.annotation = self.ax1.annotate('init',size=20, xy=(100, 100), bbox = bbox_props) self.annotation = plt.figtext(0.02, 0.9, 'rssi = ', size=20, alpha = 0.9, bbox = bbox_props) self.Freq = plt.figtext(0.85, 0.85, 'freq = ???', size=10, alpha = 0.9, bbox = bbox_props) self.Freq = plt.figtext(0.85, 0.9, 'Horizontal Plane', size=10, alpha = 0.9, bbox = bbox_props) # initialize arrow self.quiverLine = self.ax1.quiver(0,0,50,50,angles='xy',scale_units='xy',scale=1) self.ax1.set_aspect(1.) self.ax1.set_xlim(-255, 255) self.ax1.set_ylim(-255, 255) # initialize mesh plot self.xdata = [] self.ydata = [] self.polarline, = self.ax1.plot(self.xdata,self.ydata) def update(self, signalStrength, yaw): U = signalStrength*cos(yaw*pi/180) V = signalStrength*sin(yaw*pi/180) self.xdata.append(U) self.ydata.append(V) self.polarline.set_data(self.xdata,self.ydata) self.quiverLine.set_UVC(U,V) self.annotation.set_text('rssi = ' + str(signalStrength)) plt.draw()
def get_smith(fig, rect=111, plot_impedance=True, plot_ticks=False, plot_admittance=False, plot_labels=False): '''Function which returns an axis with a blank smith chart, provide a figure and optional rect coords''' #Example use: # fig3 = plt.figure(3) # ax31 = pySmith.get_smith(fig3, 221) # ax31.plot(np.real(filtsmatrix[0,:,0,0]),np.imag(filtsmatrix[0,:,0,0])) # ax32= pySmith.get_smith(fig3, 222) # ax32.plot(np.real(filtsmatrix[0,:,0,1]),np.imag(filtsmatrix[0,:,0,1])) # ax33 = pySmith.get_smith(fig3, 223) # ax33.plot(np.real(filtsmatrix[0,:,1,0]),np.imag(filtsmatrix[0,:,1,0])) # ax34 = pySmith.get_smith(fig3, 224) # ax34.plot(np.real(filtsmatrix[0,:,1,1]),np.imag(filtsmatrix[0,:,1,1])) try: #font definition font = { 'family': 'sans-serif', 'color': 'black', 'weight': 'normal', 'size': 16, } #plot radial tick marks tr = PolarAxes.PolarTransform() num_thetas = 8 #*3 #12 gives in 30 deg intervals, 8 in 45 deg, 24 in 15deg thetas = np.linspace(0.0, math.pi * (1 - 2.0 / num_thetas), num_thetas // 2) angle_ticks = [] #(0, r"$0$"), for theta in thetas: angle_info = [] angle_info.append(theta) deg = int(round(180.0 * theta / math.pi)) angle_info.append(r'%d$^{\circ}$' % deg) angle_ticks.append(angle_info) grid_locator1 = FixedLocator([v for v, s in angle_ticks]) tick_formatter1 = DictFormatter(dict(angle_ticks)) thetas2 = np.linspace(math.pi, 2 * math.pi * (1 - 1.0 / num_thetas), num_thetas // 2) angle_ticks2 = [] #(0, r"$0$"), for theta in thetas2: angle_info = [] angle_info.append(theta) deg = int(round(180.0 * theta / math.pi)) angle_info.append(r'%d$^{\circ}$' % deg) angle_ticks2.append(angle_info) grid_locator2 = FixedLocator([v for v, s in angle_ticks2]) tick_formatter2 = DictFormatter(dict(angle_ticks2)) grid_helper1 = floating_axes.GridHelperCurveLinear( tr, extremes=(math.pi, 0, 1, 0), grid_locator1=grid_locator1, #grid_locator2=grid_locator2, tick_formatter1=tick_formatter1 #, #tick_formatter2=None, ) grid_helper2 = floating_axes.GridHelperCurveLinear( tr, extremes=(2 * math.pi, math.pi, 1, 0), grid_locator1=grid_locator2, #grid_locator2=grid_locator2, tick_formatter1=tick_formatter2 #, #tick_formatter2=None, ) r1 = int(math.floor(rect / 100)) r2 = int(math.floor((rect - 100 * r1) / 10)) r3 = int(math.floor((rect - 100 * r1 - 10 * r2))) ax = SubplotHost(fig, r1, r2, r3, grid_helper=grid_helper1) ax2 = SubplotHost(fig, r1, r2, r3, grid_helper=grid_helper2) #ax.set_aspect(math.pi/180.0,'datalim') fig.add_subplot(ax) fig.add_subplot(ax2) ax.axis["bottom"].major_ticklabels.set_axis_direction("top") ax.axis["bottom"].major_ticklabels.set_fontsize(13) ax.axis["left"].set_visible(False) ax.axis["left"].toggle(all=False) ax.axis["right"].set_visible(False) ax.axis["right"].toggle(all=False) ax.axis["top"].set_visible(False) ax.axis["top"].toggle(all=False) ax.patch.set_visible(False) ax2.axis["bottom"].major_ticklabels.set_fontsize(13) ax2.axis["left"].set_visible(False) ax2.axis["left"].toggle(all=False) ax2.axis["right"].set_visible(False) ax2.axis["right"].toggle(all=False) ax2.axis["top"].set_visible(False) ax2.axis["top"].toggle(all=False) #ax = fig.add_subplot(rect) #remove axis labels ax.axis('off') #set aspect ratio to 1 ax.set_aspect(1) #, 'datalim') #set limits ax.set_xlim([-1.02, 1.02]) ax.set_ylim([-1.02, 1.02]) #remove axis labels ax2.axis('off') #set aspect ratio to 1 ax2.set_aspect(1) #,'datalim') #set limits ax2.set_xlim([-1.02, 1.02]) ax2.set_ylim([-1.02, 1.02]) ax2.patch.set_visible(False) if plot_impedance: #make lines of constant resistance res_log = np.linspace(-4, 4, 9) react_log = np.linspace(-5, 5, 2001) res = 2**res_log react = 10**react_log react2 = np.append(-1.0 * react[::-1], np.array([0])) react = np.append(react2, react) for r in res: z = 1j * react + r gam = (z - 1) / (z + 1) x = np.real(gam) y = np.imag(gam) if abs(r - 1) > 1e-6: ax.plot(x, y, c='k', linewidth=0.75, alpha=0.25) else: ax.plot(x, y, c='k', linewidth=1.0, alpha=0.4) #make lines of constant reactance react_log = np.linspace(-3, 3, 7) res_log = np.linspace(-5, 5, 2001) res = 10**res_log react = 2**react_log react2 = np.append(-1.0 * react[::-1], np.array([0])) react = np.append(react2, react) for chi in react: z = 1j * chi + res gam = (z - 1) / (z + 1) x = np.real(gam) y = np.imag(gam) if abs(chi - 1) > 1e-6 and abs(chi + 1) > 1e-6 and abs(chi) > 1e-6: ax.plot(x, y, c='k', linewidth=0.75, alpha=0.25) else: ax.plot(x, y, c='k', linewidth=1.0, alpha=0.4) if plot_admittance: #make lines of constant conductance res_log = np.linspace(-4, 4, 9) react_log = np.linspace(-5, 5, 2001) res = 2**res_log react = 10**react_log react = np.append(-1.0 * react[::-1], react) for r in res: y = 1.0 / r + 1.0 / (1j * react) gam = (1.0 / y - 1) / (1.0 / y + 1) x = np.real(gam) y = np.imag(gam) if abs(r - 1) > 1e-6: ax.plot(x, y, c='k', linewidth=0.75, alpha=0.25) else: ax.plot(x, y, c='k', linewidth=1.0, alpha=0.4) #make lines of constant susceptance react_log = np.linspace(-3, 3, 7) res_log = np.linspace(-5, 5, 2001) res = 10**res_log react = 2**react_log react = np.append(-1.0 * react[::-1], react) for chi in react: y = 1.0 / (1j * chi) + 1.0 / res gam = (1.0 / y - 1) / (1.0 / y + 1) x = np.real(gam) y = np.imag(gam) if abs(chi - 1) > 1e-6 and abs(chi + 1) > 1e-6: ax.plot(x, y, c='k', linewidth=0.75, alpha=0.25) else: ax.plot(x, y, c='k', linewidth=1.0, alpha=0.4) y = 1.0 / res gam = (1.0 / y - 1) / (1.0 / y + 1) x = np.real(gam) y = np.imag(gam) ax.plot(x, y, c='k', linewidth=1.0, alpha=0.75) if plot_labels: #naive text placement only works for default python figure size with 1 subplot ax.text(-0.15, 1.04, r'$\Gamma$ = 1j', fontdict=font) ax.text(-1.4, -0.035, r'$\Gamma$ = -1', fontdict=font) ax.text(-0.17, -1.11, r'$\Gamma$ = -1j', fontdict=font) ax.text(1.04, -0.035, r'$\Gamma$ = 1', fontdict=font) if plot_ticks: num_minorticks = 3 num_thetas = num_thetas * (num_minorticks + 1) thetas = np.linspace(0, 2.0 * math.pi * (1.0 - 1.0 / num_thetas), num_thetas) r_inner = 0.985 r_outer = 1.0 rads = np.linspace(r_inner, r_outer, 2) ticknum = 0 for theta in thetas: x = rads * np.cos(theta) y = rads * np.sin(theta) if ticknum % (num_minorticks + 1) != 0: ax.plot(x, y, c='k', linewidth=1.0, alpha=1.0) ticknum = ticknum + 1 return ax except Exception as e: print('\nError in %s' % inspect.stack()[0][3]) print(e) exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print(exc_type, fname, exc_tb.tb_lineno)