def draw_two_attribute_scatter(shp_file,field_name_1,field_name_2, output,color='grey',hatch=""): """ draw a scatter of two attributes Args: shp_file: shape file path field_name_1: x field_name_2: y output: save file path color: hatch: Returns:True if successful, False otherwise """ x_values = read_attribute(shp_file,field_name_1) y_values = read_attribute(shp_file,field_name_2) fig_obj = plt.figure() # create a new figure ax = Subplot(fig_obj, 111) fig_obj.add_subplot(ax) # n, bins, patches = plt.hist(values, bins="auto", alpha=0.75,ec="black") # ec means edge color # n, bins, patches = ax.hist(values, bins="auto", alpha=0.75, ec="black",linewidth='3',color=color,hatch=hatch) plt.scatter(x_values, y_values,marker='^',color=color) # print(n,bins,patches) # n_label = [str(i) for i in n] # plt.hist(values, bins="auto", alpha=0.75, ec="black",label=n_label) # plt.gcf().subplots_adjust(bottom=0.15) # reserve space for label # plt.xlabel(attribute,fontsize=15) # # plt.ylabel("Frequency") # plt.ylabel("Number",fontsize=15) # # # plt.title('Histogram of '+attribute) # # plt.text(60, .025, r'$\mu=100,\ \sigma=15$') # # plt.axis([40, 160, 0, 0.03]) # marked area values area_range = [1500] for area in area_range: ax.axvline(x=area,color='k',linewidth=0.8,linestyle='--') ax.text(area+100, 0.55, '%d $\mathrm{m^2}$'%area, rotation=90,fontsize=20) # hide the right and top boxed axis # ax.axis["right"].set_visible(False) # ax.axis["top"].set_visible(False) # plt.grid(True) plt.savefig(output) basic.outputlogMessage("Output figures to %s"%os.path.abspath(output)) # basic.outputlogMessage("ncount: " + str(n)) # basic.outputlogMessage("bins: "+ str(bins)) # plt.show() pass
def write_legend(data, figsize, mind, maxd, redblue, num_yrs, ylabel, xlabel, facecolor='0.8', linewidth=1.): """Write tiny legend to be plotted on map """ from matplotlib import pyplot as plt figleg = plt.figure(figsize=figsize) axleg = Subplot(figleg, 111) figleg.add_subplot(axleg) axleg.axis['top'].set_visible(False) axleg.axis['bottom'].set_visible(False) axleg.axis['right'].set_visible(False) yloc = plt.MaxNLocator(5) axleg.yaxis.set_major_locator(yloc) plt.ylim((mind, maxd)) plt.fill_between(range(num_yrs), 0., data, where=data >= 0., facecolor=redblue[0], lw=0, alpha=0.95) plt.fill_between(range(num_yrs), 0., data, where=data <= 0., facecolor=redblue[1], lw=0, alpha=0.95) font = {'size': '6'} mpl.rc('font', **font) plt.ylabel(ylabel, fontsize=6) axleg.text(-30., mind * 1.5, xlabel, fontsize=6, verticalalignment='top') plt.savefig('tinyfig' + '12' + '.png', format="png", dpi=300, bbox_inches='tight', transparent=True) plt.close() mpl.rcdefaults() return
def PlotVollmer(self, event): #initialize the plot areas self.dataFigure.clf() axes = Subplot(self.dataFigure, 111, clip_on='True',xlim=(-0.1,1.05), ylim=(-0.1,1.05),autoscale_on='True',label='vollmer',aspect='equal', adjustable='box',anchor='SW') self.dataFigure.add_subplot(axes) axes.axis["right"].set_visible(False) axes.axis["top"].set_visible(False) axes.axis["bottom"].set_visible(False) axes.axis["left"].set_visible(False) try: sqrt3_2 = 0.866025 #m_sqrt(3)/2 tr1 = Line2D((0,1),(0,0),c='black') axes.add_line(tr1) tr2 = Line2D((0,0.5),(0,sqrt3_2),c='black') axes.add_line(tr2) tr3 = Line2D((1,0.5),(0,sqrt3_2),c='black') axes.add_line(tr3) for i in [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]: diag = Line2D((i/2,1.0-i/2),(sqrt3_2*i,sqrt3_2*i),c='grey',lw=0.5) axes.add_line(diag) diag2 = Line2D((i/2,i),(sqrt3_2*i,0),c='grey',lw=0.5) axes.add_line(diag2) diag3 = Line2D((i,i+(1-i)/2),(0,sqrt3_2-sqrt3_2*i),c='grey',lw=0.5) axes.add_line(diag3) axes.text(-0.08,-0.05,'Point',family='sans-serif',size=self.fontSize,horizontalalignment='left' ) axes.text(0.97,-0.05,'Girdle',family='sans-serif',size=self.fontSize,horizontalalignment='left' ) axes.text(0.5,0.88,'Random',family='sans-serif',size=self.fontSize,horizontalalignment='center' ) # label axes values for i in [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]: axes.text((1-i)/2, sqrt3_2*(1-i)-0.01, '%d' % (i*100), family='sans-serif', size=self.fontSize, horizontalalignment='right', color='grey', rotation='60') axes.text(i, -0.02,'%d' % (i*100), family='sans-serif', size=self.fontSize, horizontalalignment='center', verticalalignment='top', color='grey') axes.text(1.0-i/2, sqrt3_2*i-0.01,'%d' % (i*100) , family='sans-serif', size=self.fontSize, horizontalalignment='left', color='grey', rotation='-60') # ternary plot (from wikipedia) # P = (0,0) # G = (1,0) # R = (1/2, sqrt(3)/2) # given (P,G,R): # x = G + R/2 # y = (sqrt(3)/2) * R if len(self.idxPlan) == 0 and len(self.idxLin) == 0: # in case we have only one one opened file but it is not checked raise AttributeError else: for i in range(len(self.idxPlan)): x = self.PeigenList[i]["G"] + (self.PeigenList[i]["R"] / 2) y = self.PeigenList[i]["R"] * sqrt3_2 axes.plot(x,y, self.PProps[i]["PoleSymb"], c=self.PProps[i]["PolColor"], ms=self.PProps[i]["polespin"],label='%s n=%d' % (self.Pname[i],self.Pndata[i])) for j in range(len(self.idxLin)): x = self.LeigenList[j]["G"] + (self.LeigenList[j]["R"] / 2) y = self.LeigenList[j]["R"] * sqrt3_2 axes.plot(x,y, self.LProps[j]["LineSymb"], c=self.LProps[j]["LinColor"], ms=self.LProps[j]["linespin"],label='%s n=%d' % (self.Lname[j],self.Lndata[j])) axes.legend(bbox_to_anchor=(0.97, 0.8), loc=2, prop=FontProperties(size=self.fontSize),numpoints=1) axes.set_xlim(-0.1,1.05) axes.set_ylim(-0.1,1.05) self.dataCanvas.draw() except AttributeError: self.dataFigure.clf() dlg = wx.MessageDialog(None, _('No file(s) selected (checked).\n\n'), _('Oooops!'), wx.OK|wx.ICON_ERROR) dlg.ShowModal() dlg.Destroy() pass
def PlotFlinn(self, event): #initialize the plot areas self.dataFigure.clf() axes = Subplot(self.dataFigure, 111, clip_on='True',xlim=(-0.2,7.2), ylim=(-0.2,7.2),autoscale_on='True',xlabel='ln(S2/S3)',ylabel='ln(S1/S2)',label='flinn',aspect='equal', adjustable='box',anchor='W') self.dataFigure.add_subplot(axes) axes.axis["right"].set_visible(False) axes.axis["top"].set_visible(False) try: # plot lines of K for i in [0.2, 0.5, 1.0, 2.0, 5.0]: if i <= 1.0: diag = Line2D((0,7.0),(0,(i*7.0)),c='grey',lw=0.5) axes.add_line(diag) else: diag = Line2D((0,(7.0/i)),(0,7.0),c='grey',lw=0.5) axes.add_line(diag) # plot lines of C for j in [2,4,6]: diag2 = Line2D((0,j),(j,0),c='grey',lw=0.5) axes.add_line(diag2) # texts axes.text(6.25,0.05,'K = 0',family='sans-serif',size=self.fontSize,horizontalalignment='left',color='grey') axes.text(0.15,6.1,'K = inf.',family='sans-serif',size=self.fontSize,horizontalalignment='left',color='grey',rotation='vertical') axes.text(6.45,6.4,'K = 1',family='sans-serif',size=self.fontSize,horizontalalignment='center',color='grey',rotation='45') axes.text(3.2,6.4,'K = 2',family='sans-serif',size=self.fontSize,horizontalalignment='center',color='grey',rotation='63.5') axes.text(1.2,6.4,'K = 5',family='sans-serif',size=self.fontSize,horizontalalignment='center',color='grey',rotation='78.7') axes.text(6.4,3.1,'K = 0.5',family='sans-serif',size=self.fontSize,horizontalalignment='center',color='grey',rotation='26.6') axes.text(6.5,1.3,'K = 0.2',family='sans-serif',size=self.fontSize,horizontalalignment='center',color='grey',rotation='11.3') axes.text(2.6,3.35,'C = 6',family='sans-serif',size=self.fontSize,horizontalalignment='center',color='grey',rotation='-45') axes.text(1.75,2.2,'C = 4',family='sans-serif',size=self.fontSize,horizontalalignment='center',color='grey',rotation='-45') axes.text(3.5,3.75,'Girdle/Cluster Transition',family='sans-serif',size=self.fontSize,horizontalalignment='left',verticalalignment='bottom',color='grey',rotation='45') axes.text(6.5,7.2,'CLUSTERS',family='sans-serif',size=self.fontSize,horizontalalignment='right',verticalalignment='bottom',color='grey') axes.text(7.2,6.5,'GIRDLES',family='sans-serif',size=self.fontSize,horizontalalignment='left',verticalalignment='top',color='grey',rotation='-90') # plot the selected (checked) files # propsPList = [pdata, itemName, PolColor, symbPoles, polespin,... # propsLList = [pdata, itemName, LinColor, LineSymb, linespin,... if len(self.idxPlan) == 0 and len(self.idxLin) == 0: # in case we have only one one opened file but it is not checked raise AttributeError else: for i in range(len(self.idxPlan)): axes.plot(self.PeigenList[i]["K_x"],self.PeigenList[i]["K_y"], self.PProps[i]["PoleSymb"], c=self.PProps[i]["PolColor"], ms=self.PProps[i]["polespin"], label='%s n=%d' % (self.Pname[i],self.Pndata[i])) for j in range(len(self.idxLin)): axes.plot(self.LeigenList[j]["K_x"],self.LeigenList[j]["K_y"], self.LProps[j]["LineSymb"], c=self.LProps[j]["LinColor"], ms=self.LProps[j]["linespin"], label='%s n=%d' % (self.Lname[j],self.Lndata[j])) axes.legend(bbox_to_anchor=(1.1, 1), loc=2, prop=FontProperties(size='small'),numpoints=1) #set the axes limits and draws the stuff axes.set_xlim(0.0,7.2) axes.set_ylim(0.0,7.2) self.dataCanvas.draw() except AttributeError: self.dataFigure.clf() dlg = wx.MessageDialog(None, 'No file(s) selected (checked).\n\n', 'Oooops!', wx.OK|wx.ICON_ERROR) dlg.ShowModal() dlg.Destroy() pass
def write_legend2(data, upper, lower, figsize, mind, maxd, redblue, num_yrs, ylabel, xlabel, facecolor='0.8', linewidth=1., which_legend='subbasins'): """Write tiny legend to be plotted on map """ from matplotlib import pyplot as plt figleg = plt.figure(figsize=figsize) axleg = Subplot(figleg, 111) figleg.add_subplot(axleg) axleg.axis['top'].set_visible(False) axleg.axis['bottom'].set_visible(False) axleg.axis['right'].set_visible(False) yloc = plt.MaxNLocator(5) axleg.yaxis.set_major_locator(yloc) plt.ylim((mind, maxd)) plt.xlim(0, num_yrs) if redblue[0] == 'blue': cmap = ListedColormap(['r', 'b']) else: cmap = ListedColormap(['b', 'r']) norm = BoundaryNorm([-1e-3, 0, 1.e-3], cmap.N) # cmap.N is number of items in the colormap x, y = interp(range(num_yrs), data, 0., float(num_yrs - 1), 3 * num_yrs) plt.fill_between(range(num_yrs), upper, lower, facecolor='0.6', lw=0, alpha=1.) lc = colorline(x, y, z=y, cmap=cmap, norm=norm, linewidth=linewidth, maxd=maxd, mind=mind) plt.gca().add_collection(lc) font = {'size': '9'} mpl.rc('font', **font) plt.ylabel(ylabel, fontsize=6) axleg.text(-30., mind * 1.5, xlabel, fontsize=6, verticalalignment='top') if which_legend == 'subbasins': plt.savefig('tinyfig' + '12' + '.png', format="png", dpi=300, bbox_inches='tight', transparent=True) elif which_legend == 'EFs': plt.savefig('tinyfig' + '8' + '.png', format="png", dpi=300, bbox_inches='tight', transparent=True) plt.close() mpl.rcdefaults() return