def format_plot(axes, xlim=None, ylim=None, xlabel='', ylabel=''): '''format 2d-plot black and with with times legends ''' #------------------------------------------------------------------- # configure the style of the font to be used for labels and ticks #------------------------------------------------------------------- # from matplotlib.font_manager import FontProperties font = FontProperties() font.set_name('Script MT') font.set_family('serif') font.set_style('normal') # font.set_size('small') font.set_size('large') font.set_variant('normal') font.set_weight('medium') if xlim != None and ylim != None: axes.axis([0, xlim, 0., ylim], fontproperties=font) # format ticks for plot # locs, labels = axes.xticks() axes.xticks(locs, map(lambda x: "%.0f" % x, locs), fontproperties=font) axes.xlabel(xlabel, fontproperties=font) locs, labels = axes.yticks() axes.yticks(locs, map(lambda x: "%.0f" % x, locs), fontproperties=font) axes.ylabel(ylabel, fontproperties=font)
def ageGrade_hist(results,title=None,style='bmh'): ''' Draw an histogram of the Age Grade results with basic stats added in the corner ''' # reset style first (if style has been changed before running the script) plt.style.use('classic') plt.style.use(style) plt.style.use(r'.\large_font.mplstyle') fig, ax = plt.subplots(figsize=(10,8)) ax.hist(results['Age Grade']*100,bins=np.arange(0,100,5),color='#A60628') #ax.set_xlim(15,60) ax.set_xlabel('Age Grade %',size='x-large') #ax.set_ylim(0,40) ax.set_ylabel('Count',size='x-large') plt.title(title) # add stats in a box stats = results['Age Grade'].describe() stats.iloc[1:]=stats.iloc[1:]*100 stats_text = "Count = {:.0f}\nMean = {:.1f}%\nMedian = {:.1f}%" +\ "\nMin = {:.1f}%\nMax = {:.1f}%" stats_text = stats_text.format(stats['count'], stats['mean'],stats['50%'], stats['min'],stats['max']) font0 = FontProperties() font0.set_family('monospace') ax.text(0.72,0.75,stats_text,fontsize=14,fontproperties=font0, bbox=dict(facecolor='white'),transform=ax.transAxes)
def time_hist(results,title=None,style='bmh'): ''' Draw an histogram of the time results with basic stats added in the corner ''' # reset style first (if style has been changed before running the script) plt.style.use('classic') plt.style.use(style) plt.style.use(r'.\large_font.mplstyle') fig, ax = plt.subplots(figsize=(10,8)) ax.hist(results['Time'],bins=range(15,61)) ax.set_xlim(15,60) ax.set_xlabel('Time (min)',size='x-large') ax.set_ylim(0,40) ax.set_ylabel('Count',size='x-large') plt.title(title) # add stats in a box in the corner stats = results['Time'].describe() stats_text = "Count = {:.0f}\nMean = {}\nMedian = {}\nMin = {}\nMax = {}".format( stats['count'], minutes_to_timeString(stats['mean']), minutes_to_timeString(stats['50%']), minutes_to_timeString(stats['min']), minutes_to_timeString(stats['max'])) font0 = FontProperties() font0.set_family('monospace') ax.text(47,30,stats_text,fontsize=14,fontproperties=font0,bbox=dict(facecolor='white'))
def plot_triangle(self): font_ = FontProperties() font_.set_family('sans-serif') font_.set_weight('normal') font_.set_style('italic') self.fig = figure() alpha = 0.8 alphal = 0.5 third_range = np.linspace(-0.0277, 0.21, 10000) second_upper = 2*third_range + 2.0/9.0 plot(third_range, second_upper, color='k', alpha=alphal) second_right = 1.5*(abs(third_range)*4.0/3.0)**(2.0/3.0) plot(third_range, second_right, color='k', alpha=alphal) connectionstyle="arc3,rad=.1" lw = 0.5 plot(np.array([0.0, 0.0]), np.array([0.0, 2.0/9.0]), color='k', alpha=alphal) gca().annotate(r'Isotropic limit', xy=np.array([0, 0]), xytext=np.array([0.05, 0.02]), fontproperties=font_, rotation=0, alpha=alpha, arrowprops=dict(arrowstyle="->", connectionstyle=connectionstyle, lw=lw)) gca().annotate(r'Plane strain', xy=np.array([0, 1.0/9.0/2]), xytext=np.array([0.07, 0.07]), fontproperties=font_, rotation=0, alpha=alpha, arrowprops=dict(arrowstyle="->", connectionstyle=connectionstyle, lw=lw)) gca().annotate(r'One component limit', xy=np.array([third_range[-1], second_upper[-1]]), xytext=np.array([0.00, 0.6]), fontproperties=font_, rotation=0, alpha=alpha, arrowprops=dict(arrowstyle="->", connectionstyle=connectionstyle, lw=lw)) gca().annotate(r'Axisymmetric contraction', xy=np.array([third_range[1000/2], second_right[1000/2]]), xytext=np.array([0.09, 0.12]), fontproperties=font_, rotation=0, alpha=alpha, arrowprops=dict(arrowstyle="->", connectionstyle=connectionstyle, lw=lw)) gca().annotate(r'Two component axisymmetric', xy=np.array([third_range[0], second_right[0]]), xytext=np.array([0.11, 0.17]), fontproperties=font_, rotation=0, alpha=alpha, arrowprops=dict(arrowstyle="->", connectionstyle=connectionstyle, lw=lw)) gca().annotate(r'Two component plane strain', xy=np.array([0, 2.0/9.0]), xytext=np.array([0.13, 0.22]), fontproperties=font_, rotation=0, alpha=alpha, arrowprops=dict(arrowstyle="->", connectionstyle=connectionstyle, lw=lw)) gca().annotate(r'Axisymmetric expansion', xy=np.array([third_range[4000], second_right[4000]]), xytext=np.array([0.15, 0.27]), fontproperties=font_, rotation=0, alpha=alpha, arrowprops=dict(arrowstyle="->", connectionstyle=connectionstyle, lw=lw)) gca().annotate(r'Two component', xy=np.array([third_range[6000], second_upper[6000]]), xytext=np.array([0.05, 0.5]), fontproperties=font_, rotation=0, alpha=alpha, arrowprops=dict(arrowstyle="->", connectionstyle=connectionstyle, lw=lw)) self.ax = gca() xlim(-0.05, 0.3) ylim(0.0, 0.7) grid(False) gca().axis('off') gcf().patch.set_visible(False) tight_layout()
def main(): dataset = VOCBboxDataset(year='2007', split='test') models = [ ('Faster R-CNN', FasterRCNNVGG16(pretrained_model='voc07')), ('SSD300', SSD300(pretrained_model='voc0712')), ('SSD512', SSD512(pretrained_model='voc0712')), ] indices = [29, 301, 189, 229] fig = plot.figure(figsize=(30, 30)) for i, idx in enumerate(indices): for j, (name, model) in enumerate(models): img, _, _ = dataset[idx] bboxes, labels, scores = model.predict([img]) bbox, label, score = bboxes[0], labels[0], scores[0] ax = fig.add_subplot( len(indices), len(models), i * len(models) + j + 1) vis_bbox( img, bbox, label, score, label_names=voc_bbox_label_names, ax=ax ) # Set MatplotLib parameters ax.set_aspect('equal') if i == 0: font = FontProperties() font.set_family('serif') ax.set_title(name, fontsize=35, y=1.03, fontproperties=font) plot.axis('off') plot.tight_layout() plot.show()
def plot_markers(data, x_base, name, map, xoff_sgn, width, off_fac, markersize): maxD = 0 ds = {} used = [] font = FontProperties() font.set_family('sans-serif') font.set_size(10) for (kind, subkind, dimval) in data: try: symb, lab = map[kind][subkind] except KeyError: raise KeyError("Invalid key for %s symbols"%name) used.append((kind, subkind)) try: ds[dimval] += 1 x_off = xoff_sgn*width*off_fac*(ds[dimval]-1) except KeyError: ds[dimval] = 1 x_off = 0 plot([x_base+x_off], [dimval], symb, markersize=markersize) # hack tweaks ## if lab=='C': ## x_off -= width/15 if lab=='A': x_off += width/30 text(x_base+x_off-width*.15, dimval-width*2., lab, fontproperties=font) if dimval > maxD: maxD = dimval return ds, maxD, used
def make_legend(labels, colors, output_file): """ Hack to generate a legend as a separate image. A dummy plot is created in one figure, and its legend gets saved to an output file. Inputs: labels - text labels for the legend colors - list of colors for the legend (same length as labels) output_file - filename for the resulting output figure Outputs: (None - output figure is written to output_file) """ if len(labels) != len(colors): raise ValueError("Lists of labels and colors " " should have the same length") fig = plt.figure() font_prop = FontProperties() font_prop.set_size('xx-large') font_prop.set_family('sans-serif') seperate_legend = plt.figure(figsize=(5, 3.5)) ax = fig.add_subplot(111) N = len(labels) # Make a dummy pie chart so we can steal its legend wedges, texts = ax.pie([100/N]*N, colors=colors) seperate_legend.legend(wedges, labels, 'center', prop=font_prop, frameon=False) seperate_legend.savefig(output_file)
def plot_basics(data, data_inst, fig, units): ''' This function is the main plotting function. Adapted from Newman's powerlaw package. ''' import pylab pylab.rcParams['xtick.major.pad']='8' pylab.rcParams['ytick.major.pad']='8' pylab.rcParams['font.sans-serif']='Arial' from matplotlib import rc rc('font', family='sans-serif') rc('font', size=10.0) rc('text', usetex=False) from matplotlib.font_manager import FontProperties panel_label_font = FontProperties().copy() panel_label_font.set_weight("bold") panel_label_font.set_size(12.0) panel_label_font.set_family("sans-serif") n_data = 1 n_graphs = 4 from powerlaw import plot_pdf, Fit, pdf ax1 = fig.add_subplot(n_graphs,n_data,data_inst) x, y = pdf(data, linear_bins=True) ind = y>0 y = y[ind] x = x[:-1] x = x[ind] ax1.scatter(x, y, color='r', s=.5, label='data') plot_pdf(data[data>0], ax=ax1, color='b', linewidth=2, label='PDF') from pylab import setp setp( ax1.get_xticklabels(), visible=False) plt.legend(loc = 'bestloc') ax2 = fig.add_subplot(n_graphs,n_data,n_data+data_inst, sharex=ax1) plot_pdf(data[data>0], ax=ax2, color='b', linewidth=2, label='PDF') fit = Fit(data, discrete=True) fit.power_law.plot_pdf(ax=ax2, linestyle=':', color='g',label='w/o xmin') p = fit.power_law.pdf() ax2.set_xlim(ax1.get_xlim()) fit = Fit(data, discrete=True,xmin=3) fit.power_law.plot_pdf(ax=ax2, linestyle='--', color='g', label='w xmin') from pylab import setp setp(ax2.get_xticklabels(), visible=False) plt.legend(loc = 'bestloc') ax3 = fig.add_subplot(n_graphs,n_data,n_data*2+data_inst)#, sharex=ax1)#, sharey=ax2) fit.power_law.plot_pdf(ax=ax3, linestyle='--', color='g',label='powerlaw') fit.exponential.plot_pdf(ax=ax3, linestyle='--', color='r',label='exp') fit.plot_pdf(ax=ax3, color='b', linewidth=2) ax3.set_ylim(ax2.get_ylim()) ax3.set_xlim(ax1.get_xlim()) plt.legend(loc = 'bestloc') ax3.set_xlabel(units)
def darkcurrent_channel_analyse(directory): hfont = {'family':'serif', 'fontsize': 12} cal_dict = sort_ibsen_by_int(directory) for key, val in sorted(cal_dict.items()): plt.plot(val['darkcurrent']['wave'], val['darkcurrent']['mean'], label='%1.f' % key) plt.xlabel(r'Wavelength $\lambda$ [nm]', **hfont) plt.ylabel(r'Signal [DN]', **hfont) fontP = FontProperties() fontP.set_family('serif') fontP.set_size('small') legend = plt.legend(loc=0, ncol=1, prop = fontP,fancybox=True, shadow=False,title='Integration Times [ms]',bbox_to_anchor=(1.0, 1.0)) #plt.setp(legend.get_title(),fontsize='small') plt.tight_layout() plt.show() plt.plot(cal_dict[5]['darkcurrent']['wave'], cal_dict[5]['reference']['mean'], label='Mean') plt.plot(cal_dict[5]['darkcurrent']['wave'], cal_dict[5]['darkcurrent']['mean'], label='Mean') plt.plot(cal_dict[5]['darkcurrent']['wave'], cal_dict[5]['darkcurrent']['data'], alpha=0.05) plt.plot(cal_dict[5]['darkcurrent']['wave'], cal_dict[5]['reference']['data'], alpha=0.05) plt.xlabel(r'Wavelength $\lambda$ [nm]', **hfont) plt.ylabel(r'Signal [DN]', **hfont) plt.show() sorted_keys = sorted(cal_dict.keys()) #[4:-4] tmp_channels = range(len(cal_dict[sorted_keys[0]]['darkcurrent']['wave'])) noise_dict = dict() #tmp_channels = np.delete(tmp_channels, [187, 258, 265, 811])794 gs = gridspec.GridSpec(2, 2) ax1 = plt.subplot(gs[0, :]) ax2 = plt.subplot(gs[1, :]) IntTimes = np.array(sorted_keys) noise = np.array([]) dark_tmp = cal_dict[110]['darkcurrent']['mean'] #ax1.plot( dark_tmp, '+') print(np.where(dark_tmp > 2361 )) for channel in tmp_channels: noise_dict[channel] = dict() dark = np.array([cal_dict[key]['darkcurrent']['mean'][channel] for key in sorted_keys]) noise_dict[channel]['dark'] = dark coeffs_dark = np.polyfit(sorted_keys, dark, deg=1) noise = np.append(noise, coeffs_dark[1]) ax1.plot(IntTimes, dark, '+') #ax1.plot(IntTimes, noise_dict[258]['dark']) ax2.plot(tmp_channels, noise) ax1.set_xlabel('Integration Time [ms]', **hfont) ax2.set_xlabel(r'Wavelength $\lambda$ [nm]', **hfont) ax1.set_ylabel('Signal [DN]', **hfont) ax2.set_ylabel('Signal [DN]', **hfont) plt.tight_layout() plt.show()
def labelPanels(axl, axlist=None, font='Arial', fontsize=18, weight='normal', xy=(-0.05, 1.05)): """ Provide labeling of panels in a figure with multiple subplots (axes) Parameters ---------- axl : list of axes objects If a single axis object is present, it will be converted to a list here. axlist : list of string labels (default : None) Contains a list of the string labels. If the default value is provided, the axes will be lettered in alphabetical sequence. font : string (default : 'Arial') Name of a valid font to use for the panel labels fontsize : float (default : 18, in points) Font size to use for axis labeling weight : string (default : 'normal') Font weight to use for labels. 'Bold', 'Italic', and 'Normal' are options xy : tuple (default : (-0.05, 1.05)) A tuple (x,y) indicating where the label should go relative to the axis frame. Values are normalized as a fraction of the frame size. Returns ------- Nothing """ if type(axl) is dict: axt = [axl[x] for x in axl] axlist = axl.keys() axl = axt if type(axl) is not list: axl = [axl] if axlist is None: axlist = string.uppercase[0:len(axl)] # assume we wish to go in sequence assert len(axlist) == len(axl) font = FontProperties() font.set_family('sans-serif') font.set_weight=weight font.set_size=fontsize font.set_style('normal') for i, ax in enumerate(axl): if ax is None: continue ax.annotate(axlist[i], xy=xy, xycoords='axes fraction', annotation_clip=False, color="k", verticalalignment='bottom',weight=weight, horizontalalignment='right', fontsize=fontsize, family='sans-serif', )
def label(lats, lons, text): #y = xy[1] - 0.15 # shift y-value for label so that it's below the artist lons_label = (np.max(lons)+np.min(lons)) / 2 lats_label = (np.max(lats) + np.min(lats)) / 2 x, y = (lons_label, lats_label ) #plt.text(x, y, text, color='#262626', ha="center", va="center", size=32, backgroundcolor='white', alpha=0.4 ) font0 = FontProperties() font0.set_family('sans-serif') plt.text(x, y, text, color='black', ha="center", va="center", size=64 , fontweight='bold', fontproperties=font0) if (plot_coords[2]=='Southern Indian Ocean'): plt.text(x, y, text, color='red', ha="center", va="center", size=64, fontproperties=font0)
def setLabels(self): """ Set plot attributes """ self.ppm.axpp.set_title("Seismograms") if self.opts.filemode == "pkl": axstk = self.axstk trans = transforms.blended_transform_factory(axstk.transAxes, axstk.transAxes) axstk.text(1, 1.01, self.opts.pklfile, transform=trans, va="bottom", ha="right", color="k") axpp = self.ppm.axpp trans = transforms.blended_transform_factory(axpp.transAxes, axpp.transData) font = FontProperties() font.set_family("monospace") axpp.text(1.025, 0, " " * 8 + "qual= CCC/SNR/COH", transform=trans, va="center", color="k", fontproperties=font)
def setLabels(self): """ Set plot attributes """ self.ppm.axpp.set_title('Seismograms') if self.opts.filemode == 'pkl': axstk = self.axstk trans = transforms.blended_transform_factory(axstk.transAxes, axstk.transAxes) axstk.text(1,1.01,self.opts.pklfile,transform=trans, va='bottom', ha='right',color='k') axpp = self.ppm.axpp trans = transforms.blended_transform_factory(axpp.transAxes, axpp.transData) font = FontProperties() font.set_family('monospace') axpp.text(1.025, 0, ' '*8+'qual= CCC/SNR/COH', transform=trans, va='center', color='k', fontproperties=font)
def make_legend(output_file, taxa_labels, colors): """ Hack to generate a separate legend image Creates a garbage pie chart (pretty, though) and saves its legend as a separate figure """ fig = plt.figure() font_prop = FontProperties() font_prop.set_size('xx-large') font_prop.set_family('sans-serif') seperate_legend = plt.figure(figsize=(5,3.5)) ax = fig.add_subplot(111) N = len(taxa_labels) wedges, texts = ax.pie([100/N]*N, colors=colors) seperate_legend.legend(wedges, taxa_labels, 'center', prop=font_prop, frameon=False) seperate_legend.savefig(output_file)
def watermark_seismic(ax, text, size, colour, xn, yn=None): """ Add semi-transparent text to the seismic. """ font = FontProperties() font.set_weight('bold') font.set_family('sans-serif') style = {'size': size, 'color': colour, 'alpha': 0.5, 'fontproperties': font } alignment = {'rotation': 33, 'horizontalalignment': 'left', 'verticalalignment': 'baseline' } params = dict(style, **alignment) # Axis ranges. xr = ax.get_xticks() yr = ax.get_yticks() aspect = xr.size / yr.size yn = yn or (xn / aspect) yn += 1 # Positions for even lines. xpos = np.linspace(xr[0], xr[-1], xn)[:-1] ypos = np.linspace(yr[0], yr[-1], yn)[:-1] # Intervals. xiv = xpos[1] - xpos[0] yiv = ypos[1] - ypos[0] # Adjust the y positions. ypos += yiv / 2 # Place everything. c = False for y in ypos: for x in xpos: if c: xi = x + xiv / 2 else: xi = x ax.text(xi, y, text, clip_box=ax.clipbox, clip_on=True, **params) c = not c return ax
def labelStation(self): """ Label stations at y axis on the right. The xcoords of the transform are axes, and the yscoords are data. """ axss = self.axss stations = [ sacdh.netsta for sacdh in self.saclist ] trans = transforms.blended_transform_factory(axss.transAxes, axss.transData) font = FontProperties() font.set_family('monospace') for i in range(self.nseis): axss.text(1.02, self.ybases[i], stations[i], transform=trans, va='center', color=self.colors[i], fontproperties=font) if self.opts.stack_on: axss.text(1.02, self.stackbase, 'Stack', transform=trans, va='center', color=self.stackcolor, fontproperties=font)
def plot_triangle(self): font_ = FontProperties() font_.set_family('sans-serif') font_.set_weight('normal') font_.set_style('italic') alpha = 0.8 self.fig = figure() alphal = 0.5 plot((self.x1[0], self.x2[0]), (self.x1[1], self.x2[1]), color='k', alpha=alphal) plot((self.x2[0], self.x3[0]), (self.x2[1], self.x3[1]), color='k', alpha=alphal) plot((self.x3[0], self.x1[0]), (self.x3[1], self.x1[1]), color='k', alpha=alphal) plot((self.x3[0], self.x4[0]), (self.x3[1], self.x4[1]), color='k', alpha=alphal) self.ax = gca() xlim(-0.2, 1.2) ylim(-0.2, 1.1) gca().annotate(r'One component', xy=self.x1, xytext=(0.85, -0.05), fontproperties=font_, alpha=alpha) gca().annotate(r'Two component', xy=self.x2, xytext=(-0.15, -0.05), fontproperties=font_, alpha=alpha) gca().annotate(r'Three component', xy=self.x3, xytext=(0.35, 0.90), fontproperties=font_, alpha=alpha) m = (self.x3[1] - self.x4[1])/(self.x3[0] - self.x4[0]) y = 0.1 x = self.x4[0] + (1.0/m)*y gca().annotate(r'Plane strain', xy=np.array([x, y]), xytext=(0.5, -0.1), fontproperties=font_,arrowprops=dict(facecolor='black',lw=0.5, arrowstyle="->",), alpha=alpha) m = (self.x3[1] - self.x1[1])/(self.x3[0] - self.x1[0]) y = 0.6 x = self.x1[0] + (1.0/m)*y dx = 0.02 gca().annotate(r'Axisymmetric Expansion', xy=np.array([x, y]), xytext=np.array([x+dx, y-1.0/m*dx]), fontproperties=font_, rotation=-55, alpha=alpha) m = self.x3[1]/self.x3[0] y = 0.6 x = (1.0/m)*y - 0.27 dx = 0.02 gca().annotate(r'Axisymmetric Contraction', xy=np.array([x, y]), xytext=np.array([x-dx, y+1.0/m*dx]), fontproperties=font_, rotation=55, alpha=alpha) grid(False) gca().axis('off') gcf().patch.set_visible(False) tight_layout()
def temperature(): FONTSTYLE = "serif" FONTSIZE = 12 hfont = {"family": FONTSTYLE, "fontsize": FONTSIZE} fontP = FontProperties() fontP.set_family(FONTSTYLE) fontP.set_size("small") gs = gridspec.GridSpec(1, 1) ax1 = plt.subplot(gs[0, :]) winter = "/home/jana_jo/DLR/Codes/measurements/LMU/291116_LMU/" summer = "/home/jana_jo/DLR/Codes/measurements/Roof_DLR/2016_09_14RoofDLR/" import glob file_prefixes = ["darkcurrent"] files_winter = sorted([file_ for file_ in glob.iglob(winter + "%s*" % file_prefixes[0])])[5:7] files_summer = sorted([file_ for file_ in glob.iglob(summer + "%s*" % file_prefixes[0])])[3:5] files_winter = [files_winter[0]] for win in files_winter: print(win) win_dict = ip.parse_ibsen_file(win) ax1.plot( win_dict["wave"][50:], win_dict["mean"][50:], color="darkblue", label="-3 $^{\circ}$C" % win_dict["IntTime"] ) files_summer = [files_summer[0]] for sum in files_summer: print(sum) sum_dict = ip.parse_ibsen_file(sum) ax1.plot( sum_dict["wave"][50:], sum_dict["mean"][50:], color="sandybrown", label="30 $^{\circ}$C" % sum_dict["IntTime"], ) assert win_dict["IntTime"] == sum_dict["IntTime"] ax1.set_ylabel("DN [a.u.]", **hfont) ax1.set_ylabel("DN [a.u.]", **hfont) ax1.set_xlabel("Wavelength [nm]", **hfont) ax1.legend(loc="best", prop=fontP, title=r" %s ms Integrationtime at temperature:" % win_dict["IntTime"]) plt.setp(ax1.get_legend().get_title(), fontsize="small", family=FONTSTYLE) plt.tight_layout() plt.show()
def __init__(self): print("==", self.__class__.__name__) evs_list = list(self.model()) evs = events.Events() evs.add_events((t,e,1) for t,e in evs_list) _, results = dynsnap.main([None, #'--dtmode=linear', #'--dtmax=10', #'--dtextra=5', #'--dont-merge-first', #'--peakfinder=greedy', ]+self.dsargs, evs=evs) results = results['results'] if results.thighs[-1] > evs.t_max(): results.thighs[-1] = evs.t_max()+1 import pcd.support.matplotlibutil as mplutil ax, extra = mplutil.get_axes(self.fname, figsize=self.figsize) ts, es = zip(*evs_list) ax.scatter(ts, es, s=self.ps, facecolor='b', edgecolor='b') #for thigh in results.thighs[:-1]: # ax.axvline(x=thigh-0.5) results.plot_intervals_patches(ax, shift=-.5) from matplotlib.font_manager import FontProperties font = FontProperties() font.set_family('serif') ax.set_xlabel('time', fontproperties=font) ax.set_ylabel('event ID', fontproperties=font) ax.set_xticks([]) ax.set_yticks([]) ax.autoscale(tight=True) if hasattr(self, 'xlim'): ax.set_xlim(*self.xlim) if hasattr(self, 'ylim'): ax.set_ylim(*self.ylim) #axB = ax.twinx() #results.plot_Jfinding(axB) mplutil.save_axes(ax, extra)
def _add_lines(self, axes, length): """ Adds the vertical sequencing bars between each peptide to the axes. """ # bae params from matplotlib.font_manager import FontProperties font = FontProperties() font.set_family("monospace") # add the vertical lines for index in range(1, length): offset = sum(self.offsets[:index]) + self._nterm_offset # pylint: disable=bad-continuation if ( ARGS.keep_lines or # b-ions have a line self.ions[1][index - 1] or # y-ions have a line self.ions[0][length - index - 1] ): axes.plot( (WIDTH * index + offset, WIDTH * index + offset), (VERT_LINE_MIN, VERT_LINE_MAX), self.color, linewidth=ARGS.line_width, ) # now have 20+30 room for the horizontal lines # bottom ion series b_ions = self.ions[1] for index, value in enumerate(b_ions): # add the horizontal line if value: self._process_b_line(axes, font, index) # top ion series y_ions = self.ions[0] for index, value in enumerate(y_ions): # add the horizontal line if value: self._process_y_line(axes, font, length, index)
def labelStation(self): """ label the seismogram with file name or net.sta """ axpp = self.axpp sacdh = self.sacdh if self.opts.nlab_on: slab = '{0:<8s}'.format(sacdh.netsta) else: slab = sacdh.filename.split('/')[-1] if self.opts.labelqual: hdrcc, hdrsn, hdrco = self.opts.qheaders[:3] cc = sacdh.gethdr(hdrcc) sn = sacdh.gethdr(hdrsn) co = sacdh.gethdr(hdrco) slab += 'qual={0:4.2f}/{1:.1f}/{2:4.2f}'.format(cc, sn, co) trans = transforms.blended_transform_factory(axpp.transAxes, axpp.transData) font = FontProperties() font.set_family('monospace') self.stalabel = axpp.text(1.025, self.ybase, slab, transform=trans, va='center', color=self.color, fontproperties=font)
def _plot_peptide(self, axes): """ Plots the peptide onto the axes, at fixed width with 25-50-25, where 25 is the leading space, 50 is the length of peptide (and height), and 25 is the trailing space (per peptide). """ from matplotlib.font_manager import FontProperties font = FontProperties() font.set_family("monospace") # now need to plot ytext widgets index = 0 peptide = self.peptide # first try nterm if self._nterm != "": text = r"$%s$" % self._nterm axes.text(FONT_POSITION, FONT_HEIGHT, text, fontsize=FONTSIZE, fontproperties=font) # iteratively find the next sub while peptide: # match residue match = re.match(RES, peptide) res = match.group(0) # create formatting string + set offsets residue = r"$%s$" % res sub_offset = FONTSIZE * SUBSCRIPT_RATIO * self._get_subscript_offset(res) seq_offset = FONTSIZE * self._get_sequence_offset(res) self.offsets.append(sub_offset + seq_offset) # x, y, text x_pos = WIDTH * index + FONT_POSITION + sum(self.offsets[:-1]) + self._nterm_offset axes.text(x_pos, FONT_HEIGHT, residue, fontsize=FONTSIZE, fontproperties=font) # reset out peptide and adjust idx peptide = peptide[match.end() :] index += 1 # first try nterm if self._cterm != "": text = r"$%s$" % self._cterm x_pos = WIDTH * index - FONT_POSITION + sum(self.offsets) + self._nterm_offset axes.text(x_pos, FONT_HEIGHT, text, fontsize=FONTSIZE, fontproperties=font)
def temperature(): FONTSTYLE = 'serif' FONTSIZE = 12 hfont = {'family':FONTSTYLE, 'fontsize': FONTSIZE} fontP = FontProperties() fontP.set_family(FONTSTYLE) fontP.set_size('small') gs = gridspec.GridSpec(1, 1) ax1 = plt.subplot(gs[0, :]) winter = '/home/joanna/DLR/Codes/measurements/measurements/LMU/291116_LMU/' summer = '/home/joanna/DLR/Codes/measurements/measurements/Roof_DLR/2016_09_14RoofDLR/' import glob file_prefixes = ['darkcurrent'] files_winter = sorted([file_ for file_ in glob.iglob(winter + '%s*' % file_prefixes[0])])[5:7] files_summer = sorted([file_ for file_ in glob.iglob(summer + '%s*' % file_prefixes[0])])[3:5] files_winter = [files_winter[0]] for win in files_winter: print(win) win_dict = ip.parse_ibsen_file(win) ax1.plot(win_dict['wave'][50:], win_dict['mean'][50:],color='darkblue', label='-3 $^{\circ}$C' % win_dict['IntTime']) files_summer = [files_summer[0]] for sum in files_summer: print(sum) sum_dict = ip.parse_ibsen_file(sum) ax1.plot(sum_dict['wave'][50:], sum_dict['mean'][50:],color='sandybrown', label='30 $^{\circ}$C' % sum_dict['IntTime']) assert win_dict['IntTime'] == sum_dict['IntTime'] ax1.set_ylabel('Signal [DN]', **hfont) ax1.set_ylabel('Signal [DN]', **hfont) ax1.set_xlabel(r'Wavelength $\lambda$ [nm]', **hfont) ax1.legend(loc='best', prop=fontP, title=r' %s ms Integrationtime at temperature:' % win_dict['IntTime']) plt.setp(ax1.get_legend().get_title(), fontsize='small', family=FONTSTYLE) plt.tight_layout() plt.show()
' '.join(union_codes - set(udpipe_table.keys())), file=sys.stderr) print('efselab is missing: %s' % ' '.join(union_codes - set(efselab_table.keys())), file=sys.stderr) import matplotlib.pyplot as plt #table = sorted( # (UD_NAMES[code], # bilty_table[code], udpipe_table[code], efselab_table[code]) # for code in common_codes) from matplotlib.font_manager import FontProperties import matplotlib.patches as mpatches prop = FontProperties() prop.set_family('serif') plt.xscale('log') plt.yscale('log') plt.xlabel('n: size of training data (tokens)', fontproperties=prop) plt.ylabel('e: PoS tagging error rate (%)', fontproperties=prop) table = [row for row in table if not None in row] train_sizes = [TRAIN_SIZE[code] for code, _, _, _ in table] bilty_acc = [100 * bilty for _, bilty, _, _ in table] efselab_acc = [100 * efselab for _, _, _, efselab in table] bilty_ax = plt.scatter(train_sizes, bilty_acc, marker='o', edgecolors='b', facecolors=[ 'b' if x <= y else 'none' for x, y in zip(bilty_acc, efselab_acc)
def transitVisibilityPlot(allData, markTransit=False, plotLegend=True, showMoonDist=True, print2file=False): """ Plot the visibility of transits. This function can conveniently be used with the output of the transitTimes function. Parameters ---------- allData : dictionary Essentially the output of `transitTimes`. A dictionary mapping consecutive numbers (one per transit) to another dictionary providing the following keys: ============ ==================================================== Key Description ------------ ---------------------------------------------------- Planet name Name of the planet Transit jd (Only if `markTransit is True) Array giving JD of start, mid-time, and end of transit. Obs jd Array specifying the HJD of the start, center and end of the observation. Obs cal Equivalent to 'Obs jd', but in the form of the calendar date. In particular, for each date, a list containing [Year, month, day, fractional hours] is given. Obs coord East longitude [deg], latitude [deg], and altitude [m] of the observatory. Star ra Right ascension of the star [deg]. Star dec Declination of the star [deg]. ============ ==================================================== .. note:: To use the list created by transitTimes, the LONGITUDE and LATITUDE of the observatory location must have been specified. markTransit : boolean, optional If True (default is False), the in-transit times will be clearly indicated in the plot. Note that this would not be the case otherwise, which is particularly important if extra off-transit time before and after the transit has been requested. showMoonDist : boolean, optional If True (default), the Moon distance will be shown. print2file : boolean or string, optional If True, the plot will be dumped to a png-file named: "transitVis-"[planetName].png. The default is False. If a string is given, it specifies the name of the output file. """ from PyAstronomy.pyasl import _ic if not _ic.check["matplotlib"]: raise(PE.PyARequiredImport("matplotlib is not installed.", where="transitVisibilityPlot", solution="Install matplotlib (http://matplotlib.org/)")) import matplotlib import matplotlib.pylab as plt from mpl_toolkits.axes_grid1 import host_subplot from matplotlib.ticker import MultipleLocator from matplotlib.font_manager import FontProperties from matplotlib import rcParams rcParams['xtick.major.pad'] = 12 if len(allData) == 0: raise(PE.PyAValError("Input dictionary is empty", where="transitVisibilityPlot", solution=["Use `transitTimes` to generate input dictionary", "Did you forget to supply observer's location?", "If you used `transitTime`, you might need to change the call argument (e.g., times)"])) # Check whether all relevant data have been specified reqK = ["Obs jd", "Obs coord", "Star ra", "Star dec", "Obs cal", "Planet name"] if markTransit: reqK.append("Transit jd") missingK = [] for k in reqK: if not k in allData[1]: missingK.append(k) if len(missingK) > 0: raise(PE.PyAValError("The following keys are missing in the input dictionary: " + ', '.join(missingK), where="transitVisibilityPlot", solution="Did you specify observer's location in `transitTimes`?")) fig = plt.figure(figsize=(15, 10)) fig.subplots_adjust(left=0.07, right=0.8, bottom=0.15, top=0.88) ax = host_subplot(111) font0 = FontProperties() font1 = font0.copy() font0.set_family('sans-serif') font0.set_weight('light') font1.set_family('sans-serif') font1.set_weight('medium') for n in six.iterkeys(allData): # JD array jdbinsize = 1.0 / 24. / 10. jds = np.arange(allData[n]["Obs jd"][0], allData[n]["Obs jd"][2], jdbinsize) # Get JD floating point jdsub = jds - np.floor(jds[0]) # Get alt/az of object altaz = eq2hor.eq2hor(jds, np.ones(jds.size) * allData[n]["Star ra"], np.ones(jds.size) * allData[n]["Star dec"], lon=allData[n]["Obs coord"][0], lat=allData[n]["Obs coord"][1], alt=allData[n]["Obs coord"][2]) # Get alt/az of Sun sunpos_altaz = eq2hor.eq2hor(jds, np.ones(jds.size) * allData[n]["Sun ra"], np.ones(jds.size) * allData[n]["Sun dec"], lon=allData[n]["Obs coord"][0], lat=allData[n]["Obs coord"][1], alt=allData[n]["Obs coord"][2]) # Define plot label plabel = "[%02d] %02d.%02d.%4d" % (n, allData[n]["Obs cal"][0][2], allData[n]["Obs cal"][0][1], allData[n]["Obs cal"][0][0]) # Find periods of: day, twilight, and night day = np.where(sunpos_altaz[0] >= 0.)[0] twi = np.where(np.logical_and( sunpos_altaz[0] > -18., sunpos_altaz[0] < 0.))[0] night = np.where(sunpos_altaz[0] <= -18.)[0] if (len(day) == 0) and (len(twi) == 0) and (len(night) == 0): print() print("transitVisibilityPlot - no points to draw for date %2d.%2d.%4d" % (allData[n]["Obs cal"][0][2], allData[n]["Obs cal"][0][1], allData[n]["Obs cal"][0][0])) print("Skip transit and continue with next") print() continue mpos = moonpos(jds) mpha = moonphase(jds) mpos_altaz = eq2hor.eq2hor(jds, mpos[0], mpos[1], lon=allData[n]["Obs coord"][0], lat=allData[n]["Obs coord"][1], alt=allData[n]["Obs coord"][2]) moonind = np.where(mpos_altaz[0] > 0.)[0] if showMoonDist: mdist = getAngDist(mpos[0], mpos[1], np.ones(jds.size) * allData[n]["Star ra"], np.ones(jds.size) * allData[n]["Star dec"]) bindist = int((2.0 / 24.) / jdbinsize) firstbin = np.random.randint(0, bindist) for mp in range(0, int(len(jds) / bindist)): bind = int(firstbin + float(mp) * bindist) ax.text(jdsub[bind], altaz[0][bind] - 1., str(int(mdist[bind])) + r"$^\circ$", ha="center", va="top", fontsize=8, stretch='ultra-condensed', fontproperties=font0, alpha=1.) if markTransit: # Mark points within transit. These may differ from that pertaining to the # observation if an extra offset was given to provide off-transit time. transit_only_ind = np.where(np.logical_and(jds >= allData[n]["Transit jd"][0], jds <= allData[n]["Transit jd"][2]))[0] ax.plot(jdsub[transit_only_ind], altaz[0] [transit_only_ind], 'g', linewidth=6, alpha=.3) if len(twi) > 1: # There are points in twilight linebreak = np.where( (jdsub[twi][1:] - jdsub[twi][:-1]) > 2.0 * jdbinsize)[0] if len(linebreak) > 0: plotrjd = np.insert(jdsub[twi], linebreak + 1, np.nan) plotdat = np.insert(altaz[0][twi], linebreak + 1, np.nan) ax.plot(plotrjd, plotdat, "-", color='#BEBEBE', linewidth=1.5) else: ax.plot(jdsub[twi], altaz[0][twi], "-", color='#BEBEBE', linewidth=1.5) ax.plot(jdsub[night], altaz[0][night], 'k', linewidth=1.5, label=plabel) ax.plot(jdsub[day], altaz[0][day], color='#FDB813', linewidth=1.5) altmax = np.argmax(altaz[0]) ax.text(jdsub[altmax], altaz[0][altmax], str(n), color="b", fontsize=14, fontproperties=font1, va="bottom", ha="center") if n == 29: ax.text(1.1, 1.0 - float(n) * 0.04, "too many transits", ha="left", va="top", transform=ax.transAxes, fontsize=10, fontproperties=font0, color="r") else: ax.text(1.1, 1.0 - float(n) * 0.04, plabel, ha="left", va="top", transform=ax.transAxes, fontsize=12, fontproperties=font0, color="b") ax.text(1.1, 1.03, "Start of observation", ha="left", va="top", transform=ax.transAxes, fontsize=12, fontproperties=font0, color="b") ax.text(1.1, 1.0, "[No.] Date", ha="left", va="top", transform=ax.transAxes, fontsize=12, fontproperties=font0, color="b") axrange = ax.get_xlim() ax.set_xlabel("UT [hours]") if axrange[1] - axrange[0] <= 1.0: jdhours = np.arange(0, 3, 1.0 / 24.) utchours = (np.arange(0, 72, dtype=int) + 12) % 24 else: jdhours = np.arange(0, 3, 1.0 / 12.) utchours = (np.arange(0, 72, 2, dtype=int) + 12) % 24 ax.set_xticks(jdhours) ax.set_xlim(axrange) ax.set_xticklabels(utchours, fontsize=18) # Make ax2 responsible for "top" axis and "right" axis ax2 = ax.twin() # Set upper x ticks ax2.set_xticks(jdhours) ax2.set_xticklabels(utchours, fontsize=18) ax2.set_xlabel("UT [hours]") # Horizon angle for airmass airmass_ang = np.arange(5., 90., 5.) geo_airmass = airmass.airmassPP(90. - airmass_ang) ax2.set_yticks(airmass_ang) airmassformat = [] for t in range(geo_airmass.size): airmassformat.append("%2.2f" % geo_airmass[t]) ax2.set_yticklabels(airmassformat, rotation=90) ax2.set_ylabel("Relative airmass", labelpad=32) ax2.tick_params(axis="y", pad=10, labelsize=10) plt.text(1.015, -0.04, "Plane-parallel", transform=ax.transAxes, ha='left', va='top', fontsize=10, rotation=90) ax22 = ax.twin() ax22.set_xticklabels([]) ax22.set_frame_on(True) ax22.patch.set_visible(False) ax22.yaxis.set_ticks_position('right') ax22.yaxis.set_label_position('right') ax22.spines['right'].set_position(('outward', 25)) ax22.spines['right'].set_color('k') ax22.spines['right'].set_visible(True) airmass2 = np.array([airmass.airmassSpherical( 90. - ang, allData[n]["Obs coord"][2]) for ang in airmass_ang]) ax22.set_yticks(airmass_ang) airmassformat = [] for t in range(airmass2.size): airmassformat.append("%2.2f" % airmass2[t]) ax22.set_yticklabels(airmassformat, rotation=90) ax22.tick_params(axis="y", pad=10, labelsize=10) plt.text(1.045, -0.04, "Spherical+Alt", transform=ax.transAxes, ha='left', va='top', fontsize=10, rotation=90) ax3 = ax.twiny() ax3.set_frame_on(True) ax3.patch.set_visible(False) ax3.xaxis.set_ticks_position('bottom') ax3.xaxis.set_label_position('bottom') ax3.spines['bottom'].set_position(('outward', 50)) ax3.spines['bottom'].set_color('k') ax3.spines['bottom'].set_visible(True) ltime, ldiff = localtime.localTime(utchours, np.repeat( allData[n]["Obs coord"][0], len(utchours))) jdltime = jdhours - ldiff / 24. ax3.set_xticks(jdltime) ax3.set_xticklabels(utchours) ax3.set_xlim([axrange[0], axrange[1]]) ax3.set_xlabel("Local time [hours]") ax.yaxis.set_major_locator(MultipleLocator(15)) ax.yaxis.set_minor_locator(MultipleLocator(5)) yticks = ax.get_yticks() ytickformat = [] for t in range(yticks.size): ytickformat.append(str(int(yticks[t])) + r"$^\circ$") ax.set_yticklabels(ytickformat, fontsize=20) ax.set_ylabel("Altitude", fontsize=18) yticksminor = np.array(ax.get_yticks(minor=True)) ymind = np.where(yticksminor % 15. != 0.)[0] yticksminor = yticksminor[ymind] # ax.set_yticks(yticksminor, minor=True) m_ytickformat = [] for t in range(yticksminor.size): m_ytickformat.append(str(int(yticksminor[t])) + r"$^\circ$") ax.set_yticklabels(m_ytickformat, minor=True) ax.yaxis.grid(color='gray', linestyle='dashed') ax.yaxis.grid(color='gray', which="minor", linestyle='dotted') ax2.xaxis.grid(color='gray', linestyle='dotted') def decifnec(s): try: r = s.decode("utf8") except AttributeError: r = s return r plt.text(0.5, 0.95, "Transit visibility of " + decifnec(allData[n]["Planet name"]), transform=fig.transFigure, ha='center', va='bottom', fontsize=20) if plotLegend: line1 = matplotlib.lines.Line2D( (0, 0), (1, 1), color='#FDB813', linestyle="-", linewidth=2) line2 = matplotlib.lines.Line2D( (0, 0), (1, 1), color='#BEBEBE', linestyle="-", linewidth=2) line3 = matplotlib.lines.Line2D( (0, 0), (1, 1), color='k', linestyle="-", linewidth=2) line4 = matplotlib.lines.Line2D( (0, 0), (1, 1), color='g', linestyle="-", linewidth=6, alpha=.3) if markTransit: lgd2 = plt.legend((line1, line2, line3, line4), ("day", "twilight", "night", "transit",), bbox_to_anchor=(0.88, 0.15), loc=2, borderaxespad=0., prop={'size': 12}, fancybox=True) else: lgd2 = plt.legend((line1, line2, line3), ("day", "twilight", "night",), bbox_to_anchor=(0.88, 0.13), loc=2, borderaxespad=0., prop={'size': 12}, fancybox=True) lgd2.get_frame().set_alpha(.5) targetco = r"Target coordinates: (%8.4f$^\circ$, %8.4f$^\circ$)" % \ (allData[n]["Star ra"], allData[n]["Star dec"]) obsco = "Obs coord.: (%8.4f$^\circ$, %8.4f$^\circ$, %4d m)" % \ (allData[n]["Obs coord"][0], allData[n] ["Obs coord"][1], allData[n]["Obs coord"][2]) plt.text(0.01, 0.97, targetco, transform=fig.transFigure, ha='left', va='center', fontsize=10) plt.text(0.01, 0.95, obsco, transform=fig.transFigure, ha='left', va='center', fontsize=10) if (print2file == True): outfile = "transVis-" + \ str(allData[n]["Planet name"]).replace(" ", "") + ".png" plt.savefig(outfile, format="png", dpi=300) elif isinstance(print2file, six.string_types): plt.savefig(print2file, format="png", dpi=300) else: plt.show()
def plot_basics(data, data_inst, fig, units): ### Setup ### from powerlaw import plot_pdf, Fit, pdf import pylab pylab.rcParams['xtick.major.pad'] = '8' pylab.rcParams['ytick.major.pad'] = '8' #pylab.rcParams['font.sans-serif']='Arial' from matplotlib.font_manager import FontProperties panel_label_font = FontProperties().copy() panel_label_font.set_weight("bold") panel_label_font.set_size(30.0) panel_label_font.set_family("sans-serif") n_data = 2 n_graphs = 4 annotate_coord = (-.4, .95) ############# ax1 = fig.add_subplot(n_graphs, n_data, data_inst) x, y = pdf(data, linear_bins=True) ind = y > 0 y = y[ind] x = x[:-1] x = x[ind] ax1.scatter(x, y, color='r', s=.5) plot_pdf(data[data > 0], ax=ax1, color='b', linewidth=2) from pylab import setp setp(ax1.get_xticklabels(), visible=False) if data_inst == 1: ax1.annotate("A", annotate_coord, xycoords="axes fraction", fontproperties=panel_label_font) ax2 = fig.add_subplot(n_graphs, n_data, n_data + data_inst, sharex=ax1) plot_pdf(data, ax=ax2, color='b', linewidth=2) fit = Fit(data, xmin=1, discrete=True) fit.power_law.plot_pdf(ax=ax2, linestyle='--', color='g') _ = fit.power_law.pdf() ax2.set_xlim((1, max(x))) setp(ax2.get_xticklabels(), visible=False) if data_inst == 1: ax2.annotate("B", annotate_coord, xycoords="axes fraction", fontproperties=panel_label_font) ax2.set_ylabel(u"p(X)") # (10^n)") ax3 = fig.add_subplot(n_graphs, n_data, n_data * 2 + data_inst) #, sharex=ax1)#, sharey=ax2) fit.power_law.plot_pdf(ax=ax3, linestyle='--', color='g') fit.exponential.plot_pdf(ax=ax3, linestyle='--', color='r') fit.lognormal.plot_pdf(ax=ax3, linestyle=':', color='r') fit.plot_pdf(ax=ax3, color='b', linewidth=2) ax3.set_ylim(ax2.get_ylim()) ax3.set_xlim(ax1.get_xlim()) if data_inst == 1: ax3.annotate("C", annotate_coord, xycoords="axes fraction", fontproperties=panel_label_font) ax3.set_xlabel(units)
from matplotlib.backends.backend_pdf import PdfPages import pandas as pd import matplotlib.dates as mdates from matplotlib.transforms import blended_transform_factory provider = True if provider: df = pd.read_csv("provider_locations_sm.csv.gz") idvar = "UMid" else: df = pd.read_csv("patient_locations_sm.csv.gz") idvar = "CSN" font0 = FontProperties() font0.set_family("monospace") font0.set_size(9) df["Time"] = pd.to_datetime(df.Time) df["Day"] = df.Time.dt.dayofyear # Discover the time step u = pd.Series(pd.to_datetime(df.Time.unique())) u = u.sort_values() u = u.diff() u = u.loc[u != pd.to_timedelta(0)] timestep = u.min() clr = plt.get_cmap("Set3").colors colors = {
def sequence_frequency_plot(df, seqID, ax, aminosY=True, clean_unused=-1, refseq=True, key_residues=None, border_color="green", border_width=2, labelsize=None, xrotation=0, yrotation=0, **kwargs): """Makes a heatmap subplot into the provided axis showing the sequence distribution of each residue type for each position. A part from the function arguments, any argument that can be provided to the :func:`seaborn.heatmap` function can also be provided here. By default, the heatmap generated will have the residue types as y-axis and the sequence positions as x-axis. Some tips: #. **Do you want to set the orientation of the color bar vertical?** \ Add the parameter: ``cbar_kws={"orientation": "vertical"}`` #. **Do you want to put the color bar in a different axis?** \ This is quite recommendable, as the color bar in the same axis does not \ tend to look that good. Add the parameter: ``cbar_ax=[second_axis]`` #. **You don't want a color bar?** \ Add the parameter: ``cbar=False`` :param df: Data container. :type df: Union[:class:`.DesignFrame`, :class:`.SequenceFrame`] :param str seqID: |seqID_param|. :param ax: Where to plot the heatmap. :type ax: :class:`~matplotlib.axes.Axes` :param bool aminosY: Set to :data:`False` to get invert the orientation of the heatmap. :param float clean_unused: Remove amino acids from the plot when they never get represented over the given frequency. Residues present in the reference sequence are not taken into account. :param rbool efseq: if :data:`True` (default), mark the original residues according to the reference sequence. :param key_residues: |keyres_param|. :type key_residue: |keyres_types| :param border_color: Color to use to mark the original residue types. :type border_color: Union[:class:`int`, :class:`str`] :param int border_width: Line width used to mark the original residue types. :param int labelsize: Change the size of the text in the axis. :param float xrotation: Rotation to apply in the x-axis text (degrees). :param float yrotation: Rotation to apply in the y-axis text (degrees). :raises: :ValueError: if input is not a :class:`~pandas.DataFrame` derived object. :KeyError: |reference_error|. .. rubric:: Example .. ipython:: In [1]: from rstoolbox.io import parse_rosetta_file ...: from rstoolbox.plot import sequence_frequency_plot ...: import matplotlib.pyplot as plt ...: df = parse_rosetta_file("../rstoolbox/tests/data/input_2seq.minisilent.gz", ...: {"sequence": "B"}) ...: fig = plt.figure(figsize=(25, 10)) ...: ax = plt.subplot2grid((1, 1), (0, 0)) ...: sequence_frequency_plot(df, "B", ax, refseq=False, cbar=False, xrotation=90) @savefig sequence_frequency_plot_docs.png width=5in In [2]: plt.show() """ order = [ "A", "V", "I", "L", "M", "F", "Y", "W", "S", "T", "N", "Q", "R", "H", "K", "D", "E", "C", "G", "P" ] data = copy.deepcopy(df) fp = FontProperties() fp.set_family("monospace") # Data type management. if not isinstance(data, pd.DataFrame): raise ValueError( "Input data must be in a DataFrame, DesignFrame or SequenceFrame") else: if not isinstance(data, (DesignFrame, SequenceFrame)): if len(set(data.columns.values).intersction( set(order))) == len(order): data = SequenceFrame(data) else: data = DesignFrame(data) if isinstance(data, DesignFrame): data = data.sequence_frequencies(seqID) if isinstance(data, SequenceFrame): order = sorted(data.columns.values.tolist(), key=lambda x: order.index(x)) if not data.is_transposed(): data = data.transpose().reindex(order) else: data = data.reindex(order) # Refseq and key_residues management. ref_seq = data.get_reference_sequence(seqID, key_residues) if refseq else "" # data and key_residues management. data = data.get_key_residues(key_residues) if clean_unused >= 0: data.delete_empty(clean_unused) data = data.clean() order = sorted(data.index.values.tolist(), key=lambda x: order.index(x)) data = data.reindex(order) # heatmap parameters and others kwargs.setdefault("cmap", "Blues") # define the color-range of the plot kwargs.setdefault("linewidths", 1) # linewidths are fixed to 1 kwargs.setdefault("square", True) # square is True if user don't say otherwise # by default the color bar is horizontal kwargs.setdefault("cbar_kws", {"orientation": "horizontal"}) # plot if not aminosY: data = data.transpose() sns.heatmap(data, ax=ax, **kwargs) # styling plot # seaborn made a change in the ticks from 0.7 to 0.8, # this should take care that both versions work ok. if LooseVersion(sns.__version__) < LooseVersion("0.8"): order.reverse() if aminosY: ax.yaxis.set_ticks(np.arange(0.5, len(order) + 0.5)) ax.yaxis.set_ticklabels(order, rotation=yrotation) for label in ax.get_yticklabels(): label.set_fontproperties(fp) ax.xaxis.set_ticks( np.arange(0.5, len(data.columns.values.tolist()) + 0.5)) ax.xaxis.set_ticklabels(data.columns.values.tolist(), rotation=xrotation) ax.set_ylabel("residue type") if labelsize is not None: ax.tick_params(labelsize=labelsize) else: ax.xaxis.set_ticks(np.arange(0.5, len(order) + 0.5)) ax.xaxis.set_ticklabels(order, rotation=xrotation) for label in ax.get_xticklabels(): label.set_fontproperties(fp) ax.yaxis.set_ticks( np.arange(0.5, len(data.index.values.tolist()) + 0.5)) ax.yaxis.set_ticklabels(data.index.values.tolist(), rotation=yrotation) ax.set_xlabel("residue type") if labelsize is not None: ax.tick_params(labelsize=labelsize) # marking reference sequence if ref_seq is not "" and refseq: if isinstance(border_color, int): border_color = sns.color_palette()[border_color] for i, aa in enumerate(ref_seq): if aminosY: aa_position = (i, order.index(aa)) else: aa_position = (order.index(aa), i) ax.add_patch( Rectangle(aa_position, 1, 1, fill=False, clip_on=False, edgecolor=border_color, lw=border_width, zorder=100))
def prep_boxplots(data, xlabel_str, figname='', fignum=1, do_legend=1, means=1, xlegoff=0, ylegstep=1, ylegoff=0, spacing=None): spacing_actual = { 'width': 0.1, 'wgapfac': 0.75, 'markersize': 12, 'off_fac': 0.7, 'x_step': 0.9, # 9 * width 'x_off': 0, 'box_to_marker': 1.1, 'notch_size': 0.2} if spacing is not None: spacing_actual.update(spacing) width = spacing_actual['width'] wgapfac = spacing_actual['wgapfac'] markersize = spacing_actual['markersize'] off_fac = spacing_actual['off_fac'] x_step = spacing_actual['x_step'] x_off = spacing_actual['x_off'] box_to_marker = spacing_actual['box_to_marker'] notch_size = spacing_actual['notch_size'] n = len(data) x_min = -width*3.8 #3.75 x_max = (n-1)*x_step+width*4.5 #3.75 if n > 1: halfticks = arange(1,n)*x_step-x_step/2 figure(fignum) # work out ordering of data from 'pos' key order = {} # `pos` position runs from 1 to n, `ns` runs from 0 to n-1 ns = [] for k, v in data.iteritems(): order[v['pos']] = k ns.append(v['pos']-1) ns.sort() assert ns == range(n) maxD = 0 max_dimval_markers = 0 labels = [] for pos in range(n): name = order[pos+1] pde_name = 'PD_E-'+name if 'known_dim' in data[name]: if n == 1: kdx1 = x_min kdx2 = x_max else: if pos == 0: kdx1 = x_min kdx2 = halfticks[0] elif pos == n-1: kdx1 = halfticks[n-2] kdx2 = x_max else: kdx1 = halfticks[pos-1] kdx2 = halfticks[pos] plot([[kdx1], [kdx2]], [data[name]['known_dim'],data[name]['known_dim']], 'k', linewidth=1, zorder=0) slope_data = loadObjects(pde_name)[2] ds_mins = array(slope_data[:,0])#,shape=(len(slope_data),1)) ds_mins.shape=(len(slope_data),1) ds_maxs = array(slope_data[:,1])#,shape=(len(slope_data),1)) ds_maxs.shape=(len(slope_data),1) max_ds = max([max(ds_mins[:,0]),max(ds_maxs[:,0])]) if max_ds > maxD: maxD = max_ds # limits args are ineffective here boxplot(ds_mins,positions=[pos*x_step-width*wgapfac+x_off],whis=100, means=means,monochrome=True,notch=2,notchsize=notch_size, limits=(),widths=width,fill=1) boxplot(ds_maxs,positions=[pos*x_step+width*wgapfac+x_off],whis=100, means=means,monochrome=True,notch=2,notchsize=notch_size, limits=(),widths=width,fill=1) if pos == 0: fa = figure(fignum).axes[0] fa.hold(True) if means: ds_all_mean = (mean(ds_mins[:,0])+mean(ds_maxs[:,0]))/2 plot([pos*x_step+x_off], [ds_all_mean], 'k^', markersize=markersize-2) pca_x = pos*x_step-width*(wgapfac+box_to_marker)+x_off isomap_x = pos*x_step+width*(wgapfac+box_to_marker)+x_off pca_ds = {} isomap_ds = {} try: pca_data = data[name]['pca'] except KeyError: pca_data = [] pca_ds, max_dimval_pca, pca_used = plot_markers(pca_data, pca_x, 'PCA', symbol_map['pca'], -1, width, off_fac, markersize) if max_dimval_pca > maxD: maxD = max_dimval_pca if max_dimval_pca > max_dimval_markers: max_dimval_markers = max_dimval_pca try: isomap_data = data[name]['isomap'] except KeyError: isomap_data = [] isomap_ds, max_dimval_iso, isomap_used = plot_markers(isomap_data, isomap_x, 'Isomap', symbol_map['isomap'], 1, width, off_fac, markersize) if max_dimval_iso > maxD: maxD = max_dimval_iso if max_dimval_iso > max_dimval_markers: max_dimval_markers = max_dimval_iso labels.append(data[name]['label']) ## legend if do_legend: font = FontProperties() font.set_family('sans-serif') font.set_size(11) x_legend = x_min + 3*width/4 + xlegoff y_legend = maxD+ylegoff # pca legend for k, s in pca_used: plot_markers([(k,s,y_legend)], x_legend, 'Legend', symbol_map['pca'], 1, width, off_fac, markersize) if k == 'var': legstr = "%s=%d%%"%(k,s) else: legstr = "%s=%d"%(k,s) text(x_legend+3*width/4, y_legend-width*2., legstr, fontproperties=font) y_legend -= ylegstep # isomap legend isomap_leg_data = [] for k, s in isomap_used: if y_legend-width*2. <= max_dimval_markers + 2: y_legend = maxD+ylegoff x_legend += x_step #-width*.75 plot_markers([(k,s,y_legend)], x_legend, 'Legend', symbol_map['isomap'], 1, width, off_fac, markersize) ## if k == 'eps': ## kstr = '\\epsilon' ## else: ## kstr = k text(x_legend+3*width/4, y_legend-width*2., "%s=%d"%(k,s), fontproperties=font) y_legend -= ylegstep ## tidy up axes, etc. fa.set_xticks(arange(n)*x_step) if n>1: for h in range(n-1): plot([halfticks[h], halfticks[h]], [0,maxD+1+ylegoff], 'k:') fa.set_xticklabels(labels) fa.set_position([0.07, 0.11, 0.9, 0.85]) fa.set_xlim(x_min,x_max) fa.set_ylim(0,maxD+1+ylegoff) if xlabel_str != '': xlabel(r'$\rm{'+xlabel_str+r'}$',args(fontsize=20,fontname='Times')) ylabel(r'$\rm{Dimension}$',args(fontsize=20,fontname='Times')) draw() if figname != '': save_fig(fignum, figname)
class GraphicsContext(basecore2d.GraphicsContextBase): def __init__(self, size, dpi=72, format='svg', renderer=None, *args, **kwargs): super(GraphicsContext, self).__init__(self, size, *args, **kwargs) width, height = size self.size = size self._height = height self.contents = StringIO() self._clipmap = {} self.format = format self._font_prop = FontProperties(weight='roman') self.state._transformed_clip_path = None import matplotlib.pyplot as plt self.figure = plt.figure() self._backend = renderer def render(self, format): return self.contents.getvalue() def clear(self): # TODO: clear the contents pass def width(self): return self.size[0] def height(self): return self.size[1] def save(self, filename): f = open(filename, 'w') self._backend.finalize() f.write(self.contents.getvalue()) f.close() # Text handling code def set_font(self, font): import kiva.constants as kc # Mapping of strings to valid Kiva font families: font_families = { kc.DEFAULT: 'default', kc.DECORATIVE: 'decorative', kc.ROMAN: 'rm', kc.SCRIPT: 'script', kc.SWISS: 'swiss', kc.MODERN: 'cm', } self.state.font = font face_name = font.face_name if face_name == '': face_name = 'Bitstream Vera Sans' else: face_name = font_families[font.family] self._font_prop.set_size(font.size) self._font_prop.set_weight('bold' if font.weight else 'regular') self._font_prop.set_family(face_name) def device_show_text(self, text): ttm = self.get_text_matrix() ctm = self.get_ctm() # not device_ctm!! m = affine.concat(ctm,ttm) tx,ty,sx,sy,angle = affine.trs_factor(m) angle = angle * 180. / pi gc = self._backend.new_gc() text = unicode_to_mathtext(text) if self._backend.flipy(): ty = self.height() - ty self._backend.draw_text(gc, tx, ty - 1, text, self._font_prop, angle, ismath=True) gc.restore() def device_get_full_text_extent(self, text): text = unicode_to_mathtext(text) width, height, descent = self._backend.get_text_width_height_descent(text, self._font_prop, ismath=True) return width, height, descent, 0. #height*1.2 # assume leading of 1.2*height # actual implementation =) def device_draw_image(self, img, rect): """ draw_image(img_gc, rect=(x,y,w,h)) Draws another gc into this one. If 'rect' is not provided, then the image gc is drawn into this one, rooted at (0,0) and at full pixel size. If 'rect' is provided, then the image is resized into the (w,h) given and drawn into this GC at point (x,y). img_gc is either a Numeric array (WxHx3 or WxHx4) or a GC from Kiva's Agg backend (kiva.agg.GraphicsContextArray). Requires the Python Imaging Library (PIL). """ from PIL import Image as PilImage from matplotlib import _image # We turn img into a PIL object, since that is what ReportLab # requires. To do this, we first determine if the input image # GC needs to be converted to RGBA/RGB. If so, we see if we can # do it nicely (using convert_pixel_format), and if not, we do # it brute-force using Agg. if type(img) == type(array([])): # Numeric array converted_img = agg.GraphicsContextArray(img, pix_format='rgba32') format = 'RGBA' elif isinstance(img, agg.GraphicsContextArray): if img.format().startswith('RGBA'): format = 'RGBA' elif img.format().startswith('RGB'): format = 'RGB' else: converted_img = img.convert_pixel_format('rgba32', inplace=0) format = 'RGBA' # Should probably take this into account # interp = img.get_image_interpolation() else: warnings.warn("Cannot render image of type %r into SVG context." % type(img)) return if rect == None: rect = (0, 0, img.width(), img.height()) width, height = img.width(), img.height() # converted_img now holds an Agg graphics context with the image pil_img = PilImage.fromstring(format, (converted_img.width(), converted_img.height()), converted_img.bmp_array.tostring()) left, top, width, height = rect if width != img.width() or height != img.height(): # This is not strictly required. pil_img = pil_img.resize((int(width), int(height)), PilImage.NEAREST) pil_img = pil_img.transpose(PilImage.FLIP_TOP_BOTTOM) # Fix for the SVG backend, which seems to flip x when a transform is provided. if self._backend.flipy(): pil_img = pil_img.transpose(PilImage.FLIP_LEFT_RIGHT) mpl_img = _image.frombuffer(pil_img.tostring(), width, height, True) mpl_img.is_grayscale = False gc = self._backend.new_gc() if self.state.clipping_path: gc.set_clip_path(self._get_transformed_clip_path()) transform = Affine2D.from_values(*affine.affine_params(self.get_ctm())) self._backend.draw_image(gc, left, top, mpl_img, dx=width, dy=height, transform=transform) gc.restore() def device_fill_points(self, points, mode): if mode in (FILL, FILL_STROKE, EOF_FILL_STROKE, EOF_FILL): fill = tuple(self.state.fill_color) else: fill = None if mode in (STROKE, FILL_STROKE, EOF_FILL_STROKE): color = tuple(self.state.line_color) else: color = tuple(self.state.fill_color) path = Path(points) gc = self._backend.new_gc() gc.set_linewidth(self.state.line_width) if not (self.state.line_dash[1] == 0).all(): gc.set_dashes(self.state.line_dash[0], list(self.state.line_dash[1])) if self.state.clipping_path: gc.set_clip_path(self._get_transformed_clip_path()) gc.set_joinstyle(line_join_map[self.state.line_join]) gc.set_capstyle(line_cap_map[self.state.line_cap]) gc.set_foreground(color, isRGB=True) gc.set_alpha(self.state.alpha) transform = Affine2D.from_values(*affine.affine_params(self.get_ctm())) self._backend.draw_path(gc, path, transform, fill) gc.restore() def _get_transformed_clip_path(self): x, y, width, height = self.state.clipping_path rect = ((x, y), (x+width, y), (x+width, y+height), (x, y+height)) transform = Affine2D.from_values(*affine.affine_params(self.get_ctm())) return TransformedPath(Path(rect), transform) # noops which seem to be needed def device_stroke_points(self, points, mode): # handled by device_fill_points pass def device_set_clipping_path(self, x, y, width, height): pass def device_destroy_clipping_path(self): pass def device_update_line_state(self): pass def device_update_fill_state(self): pass
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties font0 = FontProperties() font0.set_family('monospace') font0.set_size(16) pgconn = psycopg2.connect(database='asos', host='iemdb', user='******') ctx = get_autoplot_context(fdict, get_description()) varname = ctx['var'] month = ctx['month'] network = ctx['network'] station = ctx['zstation'] hour = ctx['hour'] nt = NetworkTable(network) if month == 'all': months = range(1, 13) elif month == 'fall': months = [9, 10, 11] elif month == 'winter': months = [12, 1, 2] elif month == 'spring': months = [3, 4, 5] elif month == 'summer': months = [6, 7, 8] elif month == 'gs': months = [5, 6, 7, 8, 9] else: ts = datetime.datetime.strptime("2000-"+month+"-01", '%Y-%b-%d') # make sure it is length two for the trick below in SQL months = [ts.month] df = read_sql(""" WITH obs as ( SELECT (valid + '10 minutes'::interval) at time zone %s as ts, tmpf::int as itmpf, dwpf::int as idwpf from alldata where station = %s and tmpf is not null and dwpf is not null and extract(month from valid at time zone %s) in %s), agg1 as ( SELECT date_trunc('hour', ts) as hts, avg(itmpf) as avg_itmpf, avg(idwpf) as avg_idwpf from obs WHERE extract(hour from ts) = %s GROUP by hts) SELECT extract(year from hts) as year, avg(avg_itmpf) as avg_tmpf, count(*) as cnt from agg1 GROUP by year ORDER by year ASC """, pgconn, params=(nt.sts[station]['tzname'], station, nt.sts[station]['tzname'], tuple(months), hour), index_col='year') minfreq = len(months) * 30 * 0.8 df2 = df[df['cnt'] > minfreq] (fig, ax) = plt.subplots(1, 1) ax.bar(df2.index.values, df2[varname], align='center', ec='b', fc='b') m = df2[varname].mean() ax.axhline(m, lw=2, zorder=5, color='k') slp, intercept, r, _, _ = stats.linregress(df2.index.values, df2[varname].values) ax.plot(df2.index.values, intercept + (df2.index.values * slp), color='r', lw=2, zorder=6) ax.text(0.02, 0.92, r"$\frac{^\circ}{decade} = %.2f,R^2=%.2f, avg = %.1f$" % ( slp * 10.0, r ** 2, m), va='bottom', transform=ax.transAxes, bbox=dict(color='white')) ax.set_ylim([df2[varname].min() - 5, df2[varname].max() + 5]) ax.set_xlim([df2.index.min() - 1, df2.index.max() + 1]) ax.grid(True) lts = datetime.datetime(2000, 1, 1, int(hour), 0) fig.text(0.5, 0.91, ("%s [%s] %s Local %s-%s\n" "%s [%s]" ) % (nt.sts[station]['name'], station, lts.strftime("%-I %p"), nt.sts[station]['archive_begin'].year, datetime.date.today().year, PDICT[varname], MDICT[month]), ha='center') return fig, df
def plotter(fdict): """ Go """ font0 = FontProperties() font0.set_family("monospace") font0.set_size(16) pgconn = get_dbconn("asos") ctx = get_autoplot_context(fdict, get_description()) varname = ctx["var"] month = ctx["month"] station = ctx["zstation"] hour = ctx["hour"] if month == "all": months = range(1, 13) elif month == "fall": months = [9, 10, 11] elif month == "winter": months = [12, 1, 2] elif month == "spring": months = [3, 4, 5] elif month == "summer": months = [6, 7, 8] elif month == "gs": months = [5, 6, 7, 8, 9] else: ts = datetime.datetime.strptime("2000-" + month + "-01", "%Y-%b-%d") # make sure it is length two for the trick below in SQL months = [ts.month] df = read_sql( """ WITH obs as ( SELECT (valid + '10 minutes'::interval) at time zone %s as ts, tmpf::int as itmpf, dwpf::int as idwpf from alldata where station = %s and tmpf is not null and dwpf is not null and extract(month from valid at time zone %s) in %s), agg1 as ( SELECT date_trunc('hour', ts) as hts, avg(itmpf) as avg_itmpf, avg(idwpf) as avg_idwpf from obs WHERE extract(hour from ts) = %s GROUP by hts) SELECT extract(year from hts)::int as year, avg(avg_itmpf) as avg_tmpf, count(*) as cnt from agg1 GROUP by year ORDER by year ASC """, pgconn, params=( ctx["_nt"].sts[station]["tzname"], station, ctx["_nt"].sts[station]["tzname"], tuple(months), hour, ), index_col="year", ) if df.empty: raise NoDataFound("No data was found.") minfreq = len(months) * 30 * 0.8 df2 = df[df["cnt"] > minfreq] (fig, ax) = plt.subplots(1, 1) ax.bar(df2.index.values, df2[varname], align="center", ec="b", fc="b") m = df2[varname].mean() ax.axhline(m, lw=2, zorder=5, color="k") slp, intercept, r, _, _ = stats.linregress(df2.index.values, df2[varname].values) ax.plot( df2.index.values, intercept + (df2.index.values * slp), color="r", lw=2, zorder=6, ) ax.text( 0.02, 0.92, r"$\frac{^\circ}{decade} = %.2f,R^2=%.2f, avg = %.1f$" % (slp * 10.0, r**2, m), va="bottom", transform=ax.transAxes, bbox=dict(color="white"), ) ax.set_ylim([df2[varname].min() - 5, df2[varname].max() + 5]) ax.grid(True) lts = datetime.datetime(2000, 1, 1, int(hour), 0) fig.text( 0.5, 0.91, ("%s [%s] %s Local %s-%s\n" "%s [%s]") % ( ctx["_nt"].sts[station]["name"], station, lts.strftime("%-I %p"), df2.index.min(), df2.index.max(), PDICT[varname], MDICT[month], ), ha="center", ) return fig, df
class AxisLabels(object): def __init__(self, parent): # Store references to axes self._ax1 = parent._ax1 self._ax2 = parent._ax2 self._wcs = parent._wcs self._figure = parent._figure # Set font self._label_fontproperties = FontProperties() self._ax2.yaxis.set_label_position('right') self._ax2.xaxis.set_label_position('top') system, equinox, units = wcs_util.system(self._wcs) if system == 'equatorial': if equinox == 'b1950': self.set_xtext('RA (B1950)') self.set_ytext('Dec (B1950)') else: self.set_xtext('RA (J2000)') self.set_ytext('Dec (J2000)') elif system == 'galactic': self.set_xtext('Galactic Longitude') self.set_ytext('Galactic Latitude') else: self.set_xtext('Ecliptic Longitude') self.set_ytext('Ecliptic Latitude') self.set_xposition('bottom') self.set_yposition('left') @auto_refresh def set_xtext(self, label): """ Set the x-axis label text """ self._xlabel1 = self._ax1.set_xlabel(label) self._xlabel2 = self._ax2.set_xlabel(label) @auto_refresh def set_ytext(self, label): """ Set the y-axis label text """ self._ylabel1 = self._ax1.set_ylabel(label) self._ylabel2 = self._ax2.set_ylabel(label) @auto_refresh def set_xpad(self, pad): """ Set the x-axis label displacement, in points """ self._xlabel1 = self._ax1.set_xlabel(self._xlabel1.get_text(), labelpad=pad) self._xlabel2 = self._ax2.set_xlabel(self._xlabel2.get_text(), labelpad=pad) @auto_refresh def set_ypad(self, pad): """ Set the y-axis label displacement, in points """ self._ylabel1 = self._ax1.set_ylabel(self._ylabel1.get_text(), labelpad=pad) self._ylabel2 = self._ax2.set_ylabel(self._ylabel2.get_text(), labelpad=pad) @auto_refresh @fixdocstring def set_font(self, family=None, style=None, variant=None, stretch=None, weight=None, size=None, fontproperties=None): """ Set the font of the axis labels Optional Keyword Arguments: common: family, style, variant, stretch, weight, size, fontproperties Default values are set by matplotlib or previously set values if set_font has already been called. Global default values can be set by editing the matplotlibrc file. """ if family: self._label_fontproperties.set_family(family) if style: self._label_fontproperties.set_style(style) if variant: self._label_fontproperties.set_variant(variant) if stretch: self._label_fontproperties.set_stretch(stretch) if weight: self._label_fontproperties.set_weight(weight) if size: self._label_fontproperties.set_size(size) if fontproperties: self._label_fontproperties = fontproperties self._xlabel1.set_fontproperties(self._label_fontproperties) self._xlabel2.set_fontproperties(self._label_fontproperties) self._ylabel1.set_fontproperties(self._label_fontproperties) self._ylabel2.set_fontproperties(self._label_fontproperties) @auto_refresh def show(self): """ Show the x- and y-axis labels """ self.show_x() self.show_y() @auto_refresh def hide(self): """ Hide the x- and y-axis labels """ self.hide_x() self.hide_y() @auto_refresh def show_x(self): """ Show the x-axis label """ if self._xposition == 'bottom': self._xlabel1.set_visible(True) else: self._xlabel2.set_visible(True) @auto_refresh def hide_x(self): """ Hide the x-axis label """ if self._xposition == 'bottom': self._xlabel1.set_visible(False) else: self._xlabel2.set_visible(False) @auto_refresh def show_y(self): """ Show the y-axis label """ if self._yposition == 'left': self._ylabel1.set_visible(True) else: self._ylabel2.set_visible(True) @auto_refresh def hide_y(self): """ Hide the y-axis label """ if self._yposition == 'left': self._ylabel1.set_visible(False) else: self._ylabel2.set_visible(False) @auto_refresh def set_xposition(self, position): "Set the position of the x-axis label ('top' or 'bottom')" if position == 'bottom': self._xlabel1.set_visible(True) self._xlabel2.set_visible(False) elif position == 'top': self._xlabel1.set_visible(False) self._xlabel2.set_visible(True) else: raise Exception("position should be one of 'top' or 'bottom'") self._xposition = position @auto_refresh def set_yposition(self, position): "Set the position of the y-axis label ('left' or 'right')" if position == 'left': self._ylabel1.set_visible(True) self._ylabel2.set_visible(False) elif position == 'right': self._ylabel1.set_visible(False) self._ylabel2.set_visible(True) else: raise Exception("position should be one of 'left' or 'right'") self._yposition = position
def main(): # Parse arguments parser = argparse.ArgumentParser() parser.add_argument('-j', '--job', action='append', nargs='+', help='job group') parser.add_argument('-l', '--label', action='append', nargs='?', help='job group label') parser.add_argument('-s', '--smooth', type=int, default=101, help='window for smoothing') parser.add_argument('-u', '--user', type=str, default=None, help='User defined field from log') args = parser.parse_args() # Prepare plots fig = plt.figure() ax1 = fig.add_subplot(4, 2, 1) ax2 = fig.add_subplot(4, 2, 2) ax3 = fig.add_subplot(4, 2, 3) ax4 = fig.add_subplot(4, 2, 4) ax5 = fig.add_subplot(4, 2, 5) ax6 = fig.add_subplot(4, 2, 6) ax7 = fig.add_subplot(4, 2, 8) ax8 = fig.add_subplot(4, 2, 7) results_info = '' # scan labels if args.label is not None: assert (len(args.job) == len(args.label) ), "The number of labels has to be same as the number of jobs" else: args.label = [''] * len(args.job) # Scan jobs and plot for iexp, exp_dir in enumerate(args.job): sum_reward = None sum_score = None sum_succ_p = None exp_info = '%30s seed std #traj gamma reward score success%%' % ( 'job group ' + str(iexp)) results_info = results_info + exp_info + '\n' cprint(exp_info, 'white', 'on_blue', attrs=['bold', 'underline']) for i, exp_path in enumerate(exp_dir): job = get_job_data(exp_path + '/job_data.txt') log = get_log(exp_path + '/logs/log.csv') epochs = np.arange(len(log['stoc_pol_mean'])) samples = np.cumsum( log['num_samples']) #epochs * job['num_traj'] * job['horizon'] # rewards reward = smooth_data( log['stoc_pol_mean'], window_length=args.smooth ) / job[ 'horizon'] # termination penalties provides rewards for path beyong termination ax1.plot(epochs, reward, label=job['job_name'], linewidth=2) ax7.plot(samples, reward, label=job['job_name'], linewidth=2) # score # score = smooth_data(log['score'], window_length=args.smooth)/log['num_samples'] # no termination penalties, hence normalized by samples score = smooth_data( log['score'], window_length=args.smooth) # score/step is returned ax3.plot(epochs, score, label=job['job_name'], linewidth=2) # Success percentage try: succ_p = smooth_data(log['success_rate'], window_length=args.smooth) except ValueError as e: succ_p = np.zeros(len(reward)) ax5.plot(epochs, succ_p, label=job['job_name'], linewidth=2) # user if args.user is not None: try: user = smooth_data(log[args.user], window_length=args.smooth) except: print("%s not found" % args.user) user = np.nan * epochs # user = log['time_sampling']+log['time_vpg']+log['time_npg'] ax8.plot(epochs, user, label=job['job_name'], linewidth=2) # gather stats if sum_reward is not None: sum_reward_min_length = min(len(sum_reward), len(score)) sum_reward = sum_reward[: sum_reward_min_length] + reward[: sum_reward_min_length] sum_score = sum_score[: sum_reward_min_length] + score[: sum_reward_min_length] sum_succ_p = sum_succ_p[: sum_reward_min_length] + succ_p[: sum_reward_min_length] else: sum_reward = reward sum_score = score sum_succ_p = succ_p sum_reward_min_length = len(sum_reward) # gather records exp_info = '%30s %3d %+1.2f %3d %0.3f %+.2f %+.2f %.1f%%' % (job['job_name'], job['seed'], \ job['init_std'], job['num_traj'], job['gamma'], reward[-1], score[-1], succ_p[-1]) print(exp_info) results_info = results_info + exp_info + '\n' # stats mean_reward = sum_reward[:sum_reward_min_length] / len(exp_dir) mean_score = sum_score[:sum_reward_min_length] / len(exp_dir) mean_succ_p = sum_succ_p[:sum_reward_min_length] / len(exp_dir) ax2.plot(mean_reward, label='g' + str(iexp) + ':' + args.label[iexp]) ax4.plot(mean_score, label='g' + str(iexp) + ':score') ax6.plot(mean_succ_p, label='g' + str(iexp) + ':' + 'success%') group_stats = "Group stats: <mean reward %+.2f>, <mean score %+.2f>, <mean success %+.1f>" % \ (mean_reward[-1], mean_score[-1], mean_succ_p[-1],) print(group_stats, end='\n\n') results_info = results_info + group_stats + '\n\n' # Format visuals ax1.set_ylabel('rewards/H') ax1.legend(fontsize='x-small') ax1.yaxis.tick_right() ax1.axes.xaxis.set_ticklabels([]) ax2.set_ylabel('mean rewards/H') ax2.yaxis.tick_right() ax2.legend() ax2.set_xlim(ax1.get_xlim()) ax2.set_ylim(ax1.get_ylim()) ax2.legend(fontsize='x-small') ax2.axes.xaxis.set_ticklabels([]) ax3.set_ylabel('score/H') ax3.yaxis.tick_right() ax3.set_xlim(ax1.get_xlim()) ax4.set_ylabel('mean score/H') ax4.yaxis.tick_right() ax4.set_xlim(ax1.get_xlim()) ax4.set_ylim(ax3.get_ylim()) ax4.legend(fontsize='x-small') ax5.set_ylabel('success') ax5.yaxis.tick_right() ax5.set_xlim(ax1.get_xlim()) ax5.set_ylim([-5, 105]) ax5.set_xlabel('#epochs') ax6.set_ylabel('mean success') ax6.legend(fontsize='x-small') ax6.set_xlim(ax1.get_xlim()) ax6.set_ylim(ax5.get_ylim()) ax6.yaxis.tick_right() ax6.set_xlabel('#epochs') ax7.set_xlabel('#samples') ax7.set_ylabel('rewards') ax7.yaxis.tick_right() ax8.set_xlabel('#epochs') ax8.set_ylabel(args.user) ax8.yaxis.tick_right() plt.tight_layout() plt.show() # add result info and save fig_text = plt.figure() font = FontProperties() font.set_family('monospace') fig_text.text(.5, .9, results_info, verticalalignment='top', horizontalalignment='center', fontproperties=font) with PdfPages(job['env_name'] + '.pdf') as pdf: pdf.savefig(fig) pdf.savefig(fig_text) plt.close()
def draw_graph(graph, vertex_label='label', vertex_color=None, vertex_color_dict=None, vertex_alpha=0.6, vertex_border=1, vertex_size=600, compact=False, colormap='YlOrRd', vmin=0, vmax=1, invert_colormap=False, secondary_vertex_label=None, secondary_vertex_color=None, secondary_vertex_alpha=0.6, secondary_vertex_border=1, secondary_vertex_size=600, secondary_vertex_colormap='YlOrRd', secondary_vertex_vmin=0, secondary_vertex_vmax=1, edge_label='label', secondary_edge_label=None, edge_colormap='YlOrRd', edge_vmin=0, edge_vmax=1, edge_color=None, edge_width=None, edge_alpha=0.5, dark_edge_colormap='YlOrRd', dark_edge_vmin=0, dark_edge_vmax=1, dark_edge_color=None, dark_edge_dotted=True, dark_edge_alpha=0.3, size=10, size_x_to_y_ratio=1, font_size=9, layout='graphviz', prog='neato', pos=None, verbose=True, file_name=None, title_key='id', ignore_for_layout="edge_attribute", logscale=False): """Plot graph layout.""" if size is not None: size_x = size size_y = int(float(size) / size_x_to_y_ratio) plt.figure(figsize=(size_x, size_y)) plt.grid(False) plt.axis('off') if vertex_label is not None: if secondary_vertex_label: vertex_labels = dict() for u, d in graph.nodes(data=True): label1 = _serialize_list(d.get(vertex_label, 'N/A')) label2 = _serialize_list(d.get(secondary_vertex_label, 'N/A')) vertex_labels[u] = '%s\n%s' % (label1, label2) else: vertex_labels = dict() for u, d in graph.nodes(data=True): label = d.get(vertex_label, 'N/A') vertex_labels[u] = _serialize_list(label) edges_normal = [(u, v) for (u, v, d) in graph.edges(data=True) if d.get('nesting', False) is False] edges_nesting = [(u, v) for (u, v, d) in graph.edges(data=True) if d.get('nesting', False) is True] if edge_label is not None: if secondary_edge_label: edge_labels = dict([ (( u, v, ), '%s\n%s' % (d.get(edge_label, ''), d.get(secondary_edge_label, ''))) for u, v, d in graph.edges(data=True) ]) else: edge_labels = dict([(( u, v, ), d.get(edge_label, '')) for u, v, d in graph.edges(data=True)]) if vertex_color is None: node_color = 'white' elif vertex_color in ['_labels_', '_label_', '__labels__', '__label__']: node_color = [] for u, d in graph.nodes(data=True): label = d.get('label', '.') if vertex_color_dict is not None: node_color.append(vertex_color_dict.get(label, 0)) else: node_color.append(hash(_serialize_list(label))) else: if invert_colormap: node_color = [ -d.get(vertex_color, 0) for u, d in graph.nodes(data=True) ] else: node_color = [ d.get(vertex_color, 0) for u, d in graph.nodes(data=True) ] if logscale is True: log_threshold = 0.01 node_color = [ math.log(c) if c > log_threshold else math.log(log_threshold) for c in node_color ] if edge_width is None: widths = 1 elif isinstance(edge_width, int): widths = edge_width else: widths = [ d.get(edge_width, 1) for u, v, d in graph.edges(data=True) if 'nesting' not in d ] if edge_color is None: edge_colors = 'black' elif edge_color in ['_labels_', '_label_', '__labels__', '__label__']: edge_colors = [ hash(str(d.get('label', '.'))) for u, v, d in graph.edges(data=True) if 'nesting' not in d ] else: if invert_colormap: edge_colors = [ -d.get(edge_color, 0) for u, v, d in graph.edges(data=True) if 'nesting' not in d ] else: edge_colors = [ d.get(edge_color, 0) for u, v, d in graph.edges(data=True) if 'nesting' not in d ] if dark_edge_color is None: dark_edge_colors = 'black' else: dark_edge_colors = [ d.get(dark_edge_color, 0) for u, v, d in graph.edges(data=True) if 'nesting' in d ] tmp_edge_set = [(a, b, d) for (a, b, d) in graph.edges(data=True) if ignore_for_layout in d] graph.remove_edges_from(tmp_edge_set) if pos is None: if layout == 'graphviz': graph_copy = graph.copy() pos = nx.nx_pydot.graphviz_layout(graph_copy, prog=prog) elif layout == "RNA": import RNA # this is part of the vienna RNA package rna_object = RNA.get_xy_coordinates(graph.graph['structure']) pos = { i: (rna_object.get(i).X, rna_object.get(i).Y) for i in range(len(graph.graph['structure'])) } elif layout == 'circular': pos = nx.circular_layout(graph) elif layout == 'random': pos = nx.random_layout(graph) elif layout == 'spring': pos = nx.spring_layout(graph) elif layout == 'shell': pos = nx.shell_layout(graph) elif layout == 'spectral': pos = nx.spectral_layout(graph) elif layout == 'KK': pos = KKEmbedder().transform(graph) else: raise Exception('Unknown layout format: %s' % layout) if vertex_border is False: linewidths = 0.001 else: linewidths = vertex_border graph.add_edges_from(tmp_edge_set) if secondary_vertex_color is not None: if secondary_vertex_border is False: secondary_linewidths = 0.001 else: secondary_linewidths = secondary_vertex_border secondary_node_color = [ d.get(secondary_vertex_color, 0) for u, d in graph.nodes(data=True) ] secondary_nodes = nx.draw_networkx_nodes( graph, pos, node_color=secondary_node_color, alpha=secondary_vertex_alpha, node_size=secondary_vertex_size, linewidths=secondary_linewidths, cmap=plt.get_cmap(secondary_vertex_colormap), vmin=secondary_vertex_vmin, vmax=secondary_vertex_vmax) secondary_nodes.set_edgecolor('k') if compact: nodes = nx.draw_networkx_nodes(graph, pos, node_color='w', alpha=1, node_size=vertex_size, linewidths=linewidths) nodes.set_edgecolor('k') nx.draw_networkx_nodes(graph, pos, node_color=node_color, alpha=vertex_alpha, node_size=vertex_size, linewidths=None, cmap=plt.get_cmap(colormap), vmin=vmin, vmax=vmax) else: nodes = nx.draw_networkx_nodes(graph, pos, node_color=node_color, alpha=vertex_alpha, node_size=vertex_size, linewidths=linewidths, cmap=plt.get_cmap(colormap), vmin=vmin, vmax=vmax) nodes.set_edgecolor('k') nx.draw_networkx_edges(graph, pos, edgelist=edges_normal, width=widths, edge_color=edge_colors, edge_cmap=plt.get_cmap(edge_colormap), edge_vmin=edge_vmin, edge_vmax=edge_vmax, alpha=edge_alpha) if dark_edge_dotted: style = 'dotted' else: style = 'solid' nx.draw_networkx_edges(graph, pos, edgelist=edges_nesting, width=1, edge_cmap=plt.get_cmap(dark_edge_colormap), edge_vmin=dark_edge_vmin, edge_vmax=dark_edge_vmax, edge_color=dark_edge_colors, style=style, alpha=dark_edge_alpha) if edge_label is not None: nx.draw_networkx_edge_labels(graph, pos, edge_labels=edge_labels, font_size=font_size) if vertex_label is not None: nx.draw_networkx_labels(graph, pos, vertex_labels, font_size=font_size, font_weight='normal', font_color='black') if title_key: title = str(graph.graph.get(title_key, '')) font = FontProperties() font.set_family('monospace') plt.title(title, fontproperties=font) if size is not None: # here we decide if we output the image. # note: if size is not set, the canvas has been created outside # of this function. # we wont write on a canvas that we didn't create ourselves. if file_name is None: plt.show() else: plt.savefig(file_name, bbox_inches='tight', transparent=True, pad_inches=0) plt.close()
class LabelViewer2(object): def __init__(self, args): """Visualize bounding boxes""" self.ax = None self.fig = None self.frame_index = 0 self.file_index = 0 self.label = None self.start_index = 0 self.scale = args.scale if isdir(args.label): input_names = sorted([ splitext(n)[0] for n in os.listdir(args.label) if splitext(n)[1] == '.json' ]) label_paths = [join(args.label, n + '.json') for n in input_names] else: label_paths = [args.label] self.label_paths = label_paths self.image_dir = args.image_dir self.font = FontProperties() self.font.set_family(['Luxi Mono', 'monospace']) self.font.set_weight('bold') self.font.set_size(18 * self.scale) self.with_image = True self.with_attr = not args.no_attr self.with_lane = not args.no_lane self.with_drivable = not args.no_drivable self.with_box2d = not args.no_box2d self.poly2d = True self.target_objects = args.target_objects if len(self.target_objects) > 0: print('Only showing objects:', self.target_objects) self.out_dir = args.output_dir self.label_map = dict([(l.name, l) for l in labels]) self.color_mode = 'random' self.label_colors = {} self.label = read_labels(self.label_paths[self.file_index]) self.image_width = self.label[0]['resolution']['width'] self.image_height = self.label[0]['resolution']['height'] self.instance_mode = False self.drivable_mode = False self.with_post = False # with post processing if args.drivable: self.set_drivable_mode() if args.instance: self.set_instance_mode() def view(self): self.frame_index = 0 if self.out_dir is None: self.show() else: self.write() def show(self): # Read and draw image dpi = 80 w = self.image_width / dpi # 16 h = self.image_height / dpi # 9 self.fig = plt.figure(figsize=(w, h), dpi=dpi) self.ax = self.fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False) # if len(self.image_paths) > 1: plt.connect('key_release_event', self.next_image) self.show_image() plt.show() def write(self): dpi = 80 w = self.image_width / dpi # 16 h = self.image_height / dpi # 9 self.fig = plt.figure(figsize=(w, h), dpi=dpi) self.ax = self.fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False) out_paths = [] self.start_index = 0 self.frame_index = 0 self.file_index = 0 while self.file_index < len(self.label_paths): if self.label is None: self.label = read_labels(self.label_paths[self.file_index]) out_name = splitext( split(self.label[self.frame_index - self.start_index]['name'])[1])[0] + '.png' out_path = join(self.out_dir, out_name) if self.show_image(): self.fig.savefig(out_path, dpi=dpi) out_paths.append(out_path) self.frame_index += 1 if self.frame_index >= len(self.label): self.start_index = self.frame_index self.file_index += 1 self.label = None if self.with_post: print('Post-processing') p = Pool(10) if self.instance_mode: p.map(convert_instance_rgb, out_paths) if self.drivable_mode: p = Pool(10) p.map(convert_drivable_rgb, out_paths) def set_instance_mode(self): self.with_image = False self.with_attr = False self.with_drivable = False self.with_lane = False self.with_box2d = False self.poly2d = True self.color_mode = 'instance' self.instance_mode = True self.with_post = True def set_drivable_mode(self): self.with_image = False self.with_attr = False self.with_drivable = True self.with_lane = False self.with_box2d = False self.poly2d = False self.color_mode = 'instance' self.drivable_mode = True self.with_post = True def show_image(self): plt.cla() if self.frame_index >= self.start_index + len(self.label): self.label = None self.file_index += 1 self.start_index = self.frame_index if self.file_index >= len(self.label_paths): self.file_index = 0 self.frame_index = 0 self.start_index = 0 if self.label is None: self.label = read_labels(self.label_paths[self.file_index]) frame = self.label[self.frame_index - self.start_index] print('Image:', frame['name']) self.fig.canvas.set_window_title(frame['name']) if self.with_image: if 'url' in frame and len(frame['url']) > 0: req = urllib.request.Request(frame['url']) image_data = urllib.request.urlopen(req, timeout=300).read() im = np.asarray(Image.open(io.BytesIO(image_data))) else: image_path = join(self.image_dir, frame['name']) # Fix the problem that mpimg reads jpg as uint8, png as float #img = mpimg.imread(image_path) img = np.asarray(Image.open(image_path)) im = np.array(img, dtype=np.uint8) self.ax.imshow(im, interpolation='nearest', aspect='auto') else: self.ax.set_xlim(0, self.image_width - 1) self.ax.set_ylim(0, self.image_height - 1) self.ax.invert_yaxis() self.ax.add_patch( self.poly2patch([[0, 0], [0, self.image_height - 1], [self.image_width - 1, self.image_height - 1], [self.image_width - 1, 0]], types='LLLL', closed=True, alpha=1., color='black')) if 'labels' not in frame or frame['labels'] is None: print('No labels') return True objects = frame['labels'] if len(self.target_objects) > 0: objects = get_target_objects(objects, self.target_objects) if len(objects) == 0: return False if self.with_attr: self.show_attributes(frame) if self.with_drivable: self.draw_drivable(objects) if self.with_lane: self.draw_lanes(objects) if self.with_box2d: [ self.ax.add_patch(self.box2rect(b['id'], b['box2d'])) for b in get_boxes(objects) ] if self.poly2d: self.draw_other_poly2d(objects) self.ax.axis('off') return True def next_image(self, event): if event.key == 'n': self.frame_index += 1 elif event.key == 'p': self.frame_index -= 1 else: return self.frame_index = max(self.frame_index, 0) if self.show_image(): plt.draw() else: self.next_image(event) def poly2patch(self, vertices, types, closed=False, alpha=1., color=None): moves = {'L': Path.LINETO, 'C': Path.CURVE4} points = [v for v in vertices] codes = [moves[t] for t in types] codes[0] = Path.MOVETO if closed: points.append(points[0]) codes.append(Path.CLOSEPOLY) if color is None: color = random_color() # print(codes, points) return mpatches.PathPatch( Path(points, codes), facecolor=color if closed else 'none', edgecolor=color, # if not closed else 'none', lw=1 if closed else 2 * self.scale, alpha=alpha, antialiased=False, snap=True) def draw_drivable(self, objects): objects = get_areas(objects) colors = np.array([[0, 0, 0, 255], [217, 83, 79, 255], [91, 192, 222, 255]]) / 255 for obj in objects: if self.color_mode == 'random': if obj['attributes']['areaType'] == 'direct': color = colors[1] else: color = colors[2] alpha = 0.5 else: color = ( (1 if obj['attributes']['areaType'] == 'direct' else 2) / 255., (obj['id'] // 255) / 255, (obj['id'] % 255) / 255.) alpha = 1 for poly in obj['poly2d']: self.ax.add_patch( self.poly2patch(poly['vertices'], poly['types'], closed=poly['closed'], alpha=alpha, color=color)) def draw_lanes(self, objects): objects = get_lanes(objects) # colors = np.array([[0, 0, 0, 255], # [217, 83, 79, 255], # [91, 192, 222, 255]]) / 255 colors = np.array([[0, 0, 0, 255], [255, 0, 0, 255], [0, 0, 255, 255] ]) / 255 for obj in objects: if self.color_mode == 'random': if obj['attributes']['laneDirection'] == 'parallel': color = colors[1] else: color = colors[2] alpha = 0.9 else: color = (0, (obj['id'] // 255) / 255, (obj['id'] % 255) / 255.) alpha = 1 for poly in obj['poly2d']: self.ax.add_patch( self.poly2patch(poly['vertices'], poly['types'], closed=poly['closed'], alpha=alpha, color=color)) def draw_other_poly2d(self, objects): color_mode = self.color_mode objects = get_other_poly2d(objects) for obj in objects: if 'poly2d' not in obj: continue if color_mode == 'random': color = self.get_label_color(obj['id']) alpha = 0.5 elif color_mode == 'instance': try: label = self.label_map[obj['category']] color = (label.trainId / 255., (obj['id'] // 255) / 255, (obj['id'] % 255) / 255.) except KeyError: color = (1, 0, 0) alpha = 1 else: raise ValueError('Unknown color mode {}'.format( self.color_mode)) for poly in obj['poly2d']: self.ax.add_patch( self.poly2patch(poly['vertices'], poly['types'], closed=poly['closed'], alpha=alpha, color=color)) def box2rect(self, label_id, box2d): """generate individual bounding box from label""" x1 = box2d['x1'] y1 = box2d['y1'] x2 = box2d['x2'] y2 = box2d['y2'] box_color = self.get_label_color(label_id) # Draw and add one box to the figure return mpatches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=3 * self.scale, edgecolor=box_color, facecolor='none', fill=False, alpha=0.75) def get_label_color(self, label_id): if label_id not in self.label_colors: self.label_colors[label_id] = random_color() return self.label_colors[label_id] def show_attributes(self, frame): if 'attributes' not in frame: return attributes = frame['attributes'] if attributes is None or len(attributes) == 0: return key_width = 0 for k, _ in attributes.items(): if len(k) > key_width: key_width = len(k) attr_tag = io.StringIO() for k, v in attributes.items(): attr_tag.write('{}: {}\n'.format(k.rjust(key_width, ' '), v)) attr_tag.seek(0) self.ax.text(25 * self.scale, 90 * self.scale, attr_tag.read()[:-1], fontproperties=self.font, color='red', bbox={ 'facecolor': 'white', 'alpha': 0.4, 'pad': 10, 'lw': 0 })
class LabelViewer(object): def __init__(self, args): """Visualize bounding boxes.""" self.ax = None self.fig = None self.current_index = 0 self.scale = args.scale image_paths = [args.image] label_paths = [args.label] if isdir(args.label): input_names = sorted([ splitext(n)[0] for n in os.listdir(args.label) if splitext(n)[1] == ".json" ]) image_paths = [join(args.image, n + ".jpg") for n in input_names] label_paths = [join(args.label, n + ".json") for n in input_names] self.image_paths = image_paths self.label_paths = label_paths self.font = FontProperties() self.font.set_family(["Luxi Mono", "monospace"]) self.font.set_weight("bold") self.font.set_size(18 * self.scale) self.with_image = True self.with_attr = not args.no_attr self.with_lane = not args.no_lane self.with_drivable = not args.no_drivable self.with_box2d = not args.no_box2d self.with_segment = True self.target_objects = args.target_objects if len(self.target_objects) > 0: print("Only showing objects:", self.target_objects) self.out_dir = args.output_dir self.label_map = dict([(label.name, label) for label in labels]) self.color_mode = "random" self.image_width = 1280 self.image_height = 720 self.instance_mode = False self.drivable_mode = False self.with_post = False # with post processing if args.drivable: self.set_drivable_mode() if args.instance: self.set_instance_mode() def view(self): self.current_index = 0 if self.out_dir is None: self.show() else: self.write() def show(self): # Read and draw image dpi = 80 w = 16 h = 9 self.fig = plt.figure(figsize=(w, h), dpi=dpi) self.ax = self.fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False) if len(self.image_paths) > 1: plt.connect("key_release_event", self.next_image) self.show_image() plt.show() def write(self): dpi = 80 w = 16 h = 9 self.fig = plt.figure(figsize=(w, h), dpi=dpi) self.ax = self.fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False) out_paths = [] for i in range(len(self.image_paths)): self.current_index = i out_name = splitext(split(self.image_paths[i])[1])[0] + ".png" out_path = join(self.out_dir, out_name) if self.show_image(): self.fig.savefig(out_path, dpi=dpi) out_paths.append(out_path) if self.with_post: print("Post-processing") p = Pool(10) if self.instance_mode: p.map(convert_instance_rgb, out_paths) if self.drivable_mode: p = Pool(10) p.map(convert_drivable_rgb, out_paths) def set_instance_mode(self): self.with_image = False self.with_attr = False self.with_drivable = False self.with_lane = False self.with_box2d = False self.with_segment = True self.color_mode = "instance" self.instance_mode = True self.with_post = True def set_drivable_mode(self): self.with_image = False self.with_attr = False self.with_drivable = True self.with_lane = False self.with_box2d = False self.with_segment = False self.color_mode = "instance" self.drivable_mode = True self.with_post = True def show_image(self): plt.cla() label_path = self.label_paths[self.current_index] name = splitext(split(label_path)[1])[0] print("Image:", name) self.fig.canvas.set_window_title(name) if self.with_image: image_path = self.image_paths[self.current_index] img = mpimg.imread(image_path) im = np.array(img, dtype=np.uint8) self.ax.imshow(im, interpolation="nearest", aspect="auto") else: self.ax.set_xlim(0, self.image_width - 1) self.ax.set_ylim(0, self.image_height - 1) self.ax.invert_yaxis() self.ax.add_patch( self.poly2patch( [ [0, 0, "L"], [0, self.image_height - 1, "L"], [self.image_width - 1, self.image_height - 1, "L"], [self.image_width - 1, 0, "L"], ], closed=True, alpha=1.0, color="black", )) # Read annotation labels with open(label_path) as data_file: label = json.load(data_file) objects = label["frames"][0]["objects"] if len(self.target_objects) > 0: objects = get_target_objects(objects, self.target_objects) if len(objects) == 0: return False if "attributes" in label and self.with_attr: attributes = label["attributes"] self.ax.text( 25 * self.scale, 90 * self.scale, " scene: {}\nweather: {}\n time: {}".format( attributes["scene"], attributes["weather"], attributes["timeofday"], ), fontproperties=self.font, color="red", bbox={ "facecolor": "white", "alpha": 0.4, "pad": 10, "lw": 0 }, ) if self.with_drivable: self.draw_drivable(objects) if self.with_lane: self.draw_lanes(objects) if self.with_box2d: [ self.ax.add_patch(self.box2rect(b["box2d"])) for b in get_boxes(objects) ] if self.with_segment: self.draw_segments(objects) self.ax.axis("off") return True def next_image(self, event): if event.key == "n": self.current_index += 1 elif event.key == "p": self.current_index -= 1 else: return self.current_index = max( min(self.current_index, len(self.image_paths) - 1), 0) if self.show_image(): plt.draw() else: self.next_image(event) def poly2patch(self, poly2d, closed=False, alpha=1.0, color=None): moves = {"L": Path.LINETO, "C": Path.CURVE4} points = [p[:2] for p in poly2d] codes = [moves[p[2]] for p in poly2d] codes[0] = Path.MOVETO if closed: points.append(points[0]) if codes[-1] == 4: codes.append(Path.LINETO) else: codes.append(Path.CLOSEPOLY) if color is None: color = random_color() # print(codes, points) return mpatches.PathPatch( Path(points, codes), facecolor=color if closed else "none", edgecolor=color, # if not closed else 'none', lw=1 if closed else 2 * self.scale, alpha=alpha, antialiased=False, snap=True, ) def draw_drivable(self, objects): objects = get_areas_v0(objects) colors = (np.array([[0, 0, 0, 255], [217, 83, 79, 255], [91, 192, 222, 255]]) / 255) for obj in objects: if self.color_mode == "random": if obj["category"] == "area/drivable": color = colors[1] else: color = colors[2] alpha = 0.5 else: color = ( (1 if obj["category"] == "area/drivable" else 2) / 255.0, obj["id"] / 255.0, 0, ) alpha = 1 self.ax.add_patch( self.poly2patch(obj["poly2d"], closed=True, alpha=alpha, color=color)) def draw_lanes(self, objects): objects = get_lanes(objects) # colors = np.array([[0, 0, 0, 255], # [217, 83, 79, 255], # [91, 192, 222, 255]]) / 255 colors = ( np.array([[0, 0, 0, 255], [255, 0, 0, 255], [0, 0, 255, 255]]) / 255) for obj in objects: if self.color_mode == "random": if obj["attributes"]["direction"] == "parallel": color = colors[1] else: color = colors[2] alpha = 0.9 else: color = ( (1 if obj["category"] == "area/drivable" else 2) / 255.0, obj["id"] / 255.0, 0, ) alpha = 1 self.ax.add_patch( self.poly2patch(obj["poly2d"], closed=False, alpha=alpha, color=color)) def draw_segments(self, objects): color_mode = self.color_mode for obj in objects: if "segments2d" not in obj: continue if color_mode == "random": color = random_color() alpha = 0.5 elif color_mode == "instance": try: label = self.label_map[obj["category"]] color = (label.trainId / 255.0, obj["id"] / 255.0, 0) except KeyError: color = (1, 0, 0) alpha = 1 else: raise ValueError("Unknown color mode {}".format( self.color_mode)) for segment in obj["segments2d"]: self.ax.add_patch( self.poly2patch(segment, closed=True, alpha=alpha, color=color)) def box2rect(self, box2d): """generate individual bounding box from label.""" x1 = box2d["x1"] y1 = box2d["y1"] x2 = box2d["x2"] y2 = box2d["y2"] # Pick random color for each box box_color = random_color() # Draw and add one box to the figure return mpatches.Rectangle( (x1, y1), x2 - x1, y2 - y1, linewidth=3 * self.scale, edgecolor=box_color, facecolor="none", fill=False, alpha=0.75, )
cbar.set_label(labs[i][j]) fignm = ofile + '.png' #fignm = ofile + '-scale2.png' saveFigure(fignm, opts) if __name__ == '__main__': ckey = 'RdBu_r' ckey = 'jet' cdict = cm.datad[ckey] cmap = matplotlib.colors.LinearSegmentedColormap(ckey, cdict) fontp = FontProperties() fontp.set_family('monospace') cfile = '/opt/local/seismo/data/GeolProv/physio/cordillera.xy' coastdir = '/opt/local/seismo/data/CoastBoundaries/' files = 'coastline-na.xy', 'international-na.xy', 'political-na.xy' cfiles = [cfile,] + [coastdir+f for f in files] colors = 'mkkkkk' opts, ifiles = getParams() stafile = opts.stafile imodnm = opts.refmodel stadict = readStation(stafile) imodel = refModel(imodnm) # crustal model and crustal correction partition:
import pickle import numpy as np with open('precision_plots.p', 'rb') as f: accuracy_plot, precision_30_plot, precision_50_plot, lion_extended_percentile_options, power = pickle.load( f) ptsne_precision = 0.10817 # Calculated elsewhere baseline_extended_precision = 0.124845 # Calculated elsewhere legend_list = list() plt.figure(dpi=300) plt.gcf().set_size_inches(3.3, 2) font_properties = FontProperties() font_properties.set_family('serif') font_properties.set_name('Times New Roman') font_properties.set_size(8) color_dict = {90: 'blue', 95: 'green', 99: 'red', 100: 'cyan'} legend_lines = list() print(lion_extended_percentile_options) selected_power = {90: 13.8, 95: 16.5, 99: 21.1, 100: 26.6} p_50_index = np.where([np.abs(i - 50) < 0.00001 for i in power])[0][0] for perc in sorted(lion_extended_percentile_options): print("=========================== ", perc,
class LabelViewer2(object): def __init__(self, args): """Visualize bounding boxes.""" self.ax = None self.fig = None self.frame_index = 0 self.file_index = 0 self.label = None self.start_index = 0 self.scale = args.scale if isdir(args.label): input_names = sorted([ splitext(n)[0] for n in os.listdir(args.label) if splitext(n)[1] == ".json" ]) label_paths = [join(args.label, n + ".json") for n in input_names] else: label_paths = [args.label] self.label_paths = label_paths self.image_dir = args.image_dir self.font = FontProperties() self.font.set_family(["Luxi Mono", "monospace"]) self.font.set_weight("bold") self.font.set_size(18 * self.scale) self.with_image = True self.with_attr = not args.no_attr self.with_lane = not args.no_lane self.with_drivable = not args.no_drivable self.with_box2d = not args.no_box2d self.poly2d = True self.target_objects = args.target_objects if len(self.target_objects) > 0: print("Only showing objects:", self.target_objects) self.out_dir = args.output_dir self.label_map = dict([(label.name, label) for label in labels]) self.color_mode = "random" self.label_colors = {} self.image_width = 1280 self.image_height = 720 self.instance_mode = False self.drivable_mode = False self.with_post = False # with post processing if args.drivable: self.set_drivable_mode() if args.instance: self.set_instance_mode() self.label = read_labels(self.label_paths[self.file_index]) def view(self): self.frame_index = 0 if self.out_dir is None: self.show() else: self.write() def show(self): # Read and draw image dpi = 80 w = 16 h = 9 self.fig = plt.figure(figsize=(w, h), dpi=dpi) self.ax = self.fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False) # if len(self.image_paths) > 1: plt.connect("key_release_event", self.next_image) self.show_image() plt.show() def write(self): dpi = 80 w = 16 h = 9 self.fig = plt.figure(figsize=(w, h), dpi=dpi) self.ax = self.fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False) out_paths = [] self.start_index = 0 self.frame_index = 0 self.file_index = 0 while self.file_index < len(self.label_paths): if self.label is None: self.label = read_labels(self.label_paths[self.file_index]) out_name = (splitext( split(self.label[self.frame_index - self.start_index]["name"])[1])[0] + ".png") out_path = join(self.out_dir, out_name) if self.show_image(): self.fig.savefig(out_path, dpi=dpi) out_paths.append(out_path) self.frame_index += 1 if self.frame_index >= len(self.label): self.start_index = self.frame_index self.file_index += 1 self.label = None if self.with_post: print("Post-processing") p = Pool(10) if self.instance_mode: p.map(convert_instance_rgb, out_paths) if self.drivable_mode: p = Pool(10) p.map(convert_drivable_rgb, out_paths) def set_instance_mode(self): self.with_image = False self.with_attr = False self.with_drivable = False self.with_lane = False self.with_box2d = False self.poly2d = True self.color_mode = "instance" self.instance_mode = True self.with_post = True def set_drivable_mode(self): self.with_image = False self.with_attr = False self.with_drivable = True self.with_lane = False self.with_box2d = False self.poly2d = False self.color_mode = "instance" self.drivable_mode = True self.with_post = True def show_image(self): plt.cla() if self.frame_index >= self.start_index + len(self.label): self.label = None self.file_index += 1 self.start_index = self.frame_index if self.file_index >= len(self.label_paths): self.file_index = 0 self.frame_index = 0 self.start_index = 0 if self.label is None: self.label = read_labels(self.label_paths[self.file_index]) frame = self.label[self.frame_index - self.start_index] print("Image:", frame["name"]) self.fig.canvas.set_window_title(frame["name"]) if self.with_image: if "url" in frame and len(frame["url"]) > 0: req = urllib.request.Request(frame["url"]) image_data = urllib.request.urlopen(req, timeout=300).read() im = np.asarray(Image.open(io.BytesIO(image_data))) else: image_path = join(self.image_dir, frame["name"]) img = mpimg.imread(image_path) im = np.array(img, dtype=np.uint8) self.ax.imshow(im, interpolation="nearest", aspect="auto") else: self.ax.set_xlim(0, self.image_width - 1) self.ax.set_ylim(0, self.image_height - 1) self.ax.invert_yaxis() self.ax.add_patch( self.poly2patch( [ [0, 0], [0, self.image_height - 1], [self.image_width - 1, self.image_height - 1], [self.image_width - 1, 0], ], types="LLLL", closed=True, alpha=1.0, color="black", )) if "labels" not in frame or frame["labels"] is None: print("No labels") return True objects = frame["labels"] calibration = None if "intrinsics" in frame and "cali" in frame["intrinsics"]: calibration = np.array(frame["intrinsics"]["cali"]) if len(self.target_objects) > 0: objects = get_target_objects(objects, self.target_objects) if len(objects) == 0: return False if self.with_attr: self.show_attributes(frame) if self.with_drivable: self.draw_drivable(objects) if self.with_lane: self.draw_lanes(objects) if self.with_box2d: for b in get_boxes(objects): attributes = {} if "attributes" in b: attributes = b["attributes"] if "box3d" in b: occluded = False if "occluded" in attributes: occluded = attributes["occluded"] for line in self.box3d_to_lines(b["id"], b["box3d"], calibration, occluded): self.ax.add_patch(line) else: self.ax.add_patch(self.box2rect(b["id"], b["box2d"])) text = b["category"][:3] if "occluded" in attributes and attributes["occluded"]: text += ",o" if "truncated" in attributes and attributes["truncated"]: text += ",t" if "crowd" in attributes and attributes["crowd"]: text += ",c" [ self.ax.text( (b["box2d"]["x1"]) * self.scale, (b["box2d"]["y1"] - 4) * self.scale, text, fontsize=10 * self.scale, bbox={ "facecolor": "white", "edgecolor": "none", "alpha": 0.5, "boxstyle": "square,pad=0.1", }, ) ] if self.poly2d: self.draw_other_poly2d(objects) self.ax.axis("off") return True def next_image(self, event): if event.key == "n": self.frame_index += 1 elif event.key == "p": self.frame_index -= 1 else: return self.frame_index = max(self.frame_index, 0) if self.show_image(): plt.draw() else: self.next_image(event) def poly2patch(self, vertices, types, closed=False, alpha=1.0, color=None): moves = {"L": Path.LINETO, "C": Path.CURVE4} points = [v for v in vertices] codes = [moves[t] for t in types] codes[0] = Path.MOVETO if closed: points.append(points[0]) codes.append(Path.CLOSEPOLY) if color is None: color = random_color() # print(codes, points) return mpatches.PathPatch( Path(points, codes), facecolor=color if closed else "none", edgecolor=color, # if not closed else 'none', lw=1 if closed else 2 * self.scale, alpha=alpha, antialiased=False, snap=True, ) def draw_drivable(self, objects): objects = get_areas(objects) colors = (np.array([[0, 0, 0, 255], [217, 83, 79, 255], [91, 192, 222, 255]]) / 255) for obj in objects: if self.color_mode == "random": if obj["attributes"]["areaType"] == "direct": color = colors[1] else: color = colors[2] alpha = 0.5 else: color = ( (1 if obj["attributes"]["areaType"] == "direct" else 2) / 255.0, (obj["id"] // 255) / 255, (obj["id"] % 255) / 255.0, ) alpha = 1 for poly in obj["poly2d"]: self.ax.add_patch( self.poly2patch( poly["vertices"], poly["types"], closed=poly["closed"], alpha=alpha, color=color, )) def draw_lanes(self, objects): objects = get_lanes(objects) # colors = np.array([[0, 0, 0, 255], # [217, 83, 79, 255], # [91, 192, 222, 255]]) / 255 colors = ( np.array([[0, 0, 0, 255], [255, 0, 0, 255], [0, 0, 255, 255]]) / 255) for obj in objects: if self.color_mode == "random": if obj["attributes"]["laneDirection"] == "parallel": color = colors[1] else: color = colors[2] alpha = 0.9 else: color = ( 0, (obj["id"] // 255) / 255, (obj["id"] % 255) / 255.0, ) alpha = 1 for poly in obj["poly2d"]: self.ax.add_patch( self.poly2patch( poly["vertices"], poly["types"], closed=poly["closed"], alpha=alpha, color=color, )) def draw_other_poly2d(self, objects): color_mode = self.color_mode objects = get_other_poly2d(objects) for obj in objects: if "poly2d" not in obj: continue if color_mode == "random": color = self.get_label_color(obj["id"]) alpha = 0.5 elif color_mode == "instance": try: label = self.label_map[obj["category"]] color = ( label.trainId / 255.0, (obj["id"] // 255) / 255, (obj["id"] % 255) / 255.0, ) except KeyError: color = (1, 0, 0) alpha = 1 else: raise ValueError("Unknown color mode {}".format( self.color_mode)) for poly in obj["poly2d"]: self.ax.add_patch( self.poly2patch( poly["vertices"], poly["types"], closed=poly["closed"], alpha=alpha, color=color, )) def box2rect(self, label_id, box2d): """generate individual bounding box from label.""" x1 = box2d["x1"] y1 = box2d["y1"] x2 = box2d["x2"] y2 = box2d["y2"] box_color = self.get_label_color(label_id).tolist() # Draw and add one box to the figure return mpatches.Rectangle( (x1, y1), x2 - x1, y2 - y1, linewidth=2 * self.scale, edgecolor=box_color + [0.75], facecolor=box_color + [0.25], fill=True, ) def box3d_to_lines(self, label_id, box3d, calibration, occluded): """generate individual bounding box from 3d label.""" label = Label3d.from_box3d(box3d) edges = label.get_edges_with_visibility(calibration) box_color = self.get_label_color(label_id) alpha = 0.5 if occluded else 0.8 lines = [] for edge in edges["dashed"]: lines.append( mpatches.Polygon( edge, linewidth=2 * self.scale, linestyle=(0, (2, 2)), edgecolor=box_color, facecolor="none", fill=False, alpha=alpha, )) for edge in edges["solid"]: lines.append( mpatches.Polygon( edge, linewidth=2 * self.scale, edgecolor=box_color, facecolor="none", fill=False, alpha=alpha, )) return lines def get_label_color(self, label_id): if label_id not in self.label_colors: self.label_colors[label_id] = random_color() return self.label_colors[label_id] def show_attributes(self, frame): if "attributes" not in frame: return attributes = frame["attributes"] if attributes is None or len(attributes) == 0: return key_width = 0 for k, _ in attributes.items(): if len(k) > key_width: key_width = len(k) attr_tag = io.StringIO() for k, v in attributes.items(): attr_tag.write("{}: {}\n".format(k.rjust(key_width, " "), v)) attr_tag.seek(0) self.ax.text( 25 * self.scale, 90 * self.scale, attr_tag.read()[:-1], fontproperties=self.font, color="red", bbox={ "facecolor": "white", "alpha": 0.4, "pad": 10, "lw": 0 }, )
sp2 = fig.add_subplot(122) sp1 = fig.add_subplot(121) sp1.minorticks_on() sp2.minorticks_on() sp1.set_xlabel('log( 1 + $\delta_{gal}$ )') sp2.set_xlabel('log( 1 + $\delta_{gal}$ )') sp1.set_ylabel('log( SFR$_{UV+IR}$ / [M$_{\odot}$ / yr] )') sp1.axis([-0.05, 1.7, -0.15, 1.9]) sp2.axis([-0.05, 1.7, -0.15, 1.9]) fig.subplots_adjust(wspace=0, left=0.08, bottom=0.09, top=0.98, right=0.98) font = FontProperties() font.set_family('sans-serif') t = sp1.text( 0.03, 0.97, 'Star-Forming', fontweight='normal', fontsize=24, color='b', transform=sp1.transAxes, horizontalalignment='left', verticalalignment='top', path_effects=[PathEffects.withStroke(linewidth=2, foreground='k')]) t = sp2.text( 0.03,
import pylab from numpy import genfromtxt from matplotlib.font_manager import FontProperties pylab.rcParams['xtick.major.pad'] = '8' pylab.rcParams['ytick.major.pad'] = '8' # pylab.rcParams['font.sans-serif']='Arial' rc('font', family='sans-serif') rc('font', size=10.0) rc('text', usetex=False) panel_label_font = FontProperties().copy() panel_label_font.set_weight("bold") panel_label_font.set_size(20.0) panel_label_font.set_family("sans-serif") blackouts = genfromtxt('input/tweets.txt') data = blackouts # / 10 ** 3 # data can be list or numpy array results = powerlaw.Fit(data) print("alpha", results.power_law.alpha) print("sigma", results.power_law.sigma) R, p = results.distribution_compare('power_law', 'lognormal', normalized_ratio=True) print(R) print(p) def plot_basics(data, data_inst, fig, units):
def load_font_properties(size=None, family=None): fontP = FontProperties() fontP.set_size(load_font()['size'] if size is None else size) fontP.set_family(load_font()["family"] if family is None else family) return fontP
#rc('font', family='sans-serif') rc('font', size=20.0) #rc('text', usetex=False) #rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']}) rc('legend', **{'fontsize': 20}) ## for Palatino and other serif fonts use: #rc('font',**{'family':'serif','serif':['Palatino']}) rc('text', usetex=True) from matplotlib.font_manager import FontProperties panel_label_font = FontProperties().copy() panel_label_font.set_weight("bold") panel_label_font.set_size(26.0) panel_label_font.set_family("Sans-Serif") import matplotlib.pyplot as plt params = { 'font.family': 'Sans-Serif', 'font.serif': ['Times'], 'xtick.labelsize': 24, 'ytick.labelsize': 24 } plt.rcParams.update(params) maj_color = '#347aa3' min_color = '#e25115'
def plot(): plt.figure(dpi=320, figsize=(15, 7)) font = FontProperties() font.set_family('serif') font.set_name('Times New Roman') x = [0, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5] bleu = [ 43.33, 9.1, 27.6, 32.05, 31, 33, 14, 15, 16.42, 3.19, 5.33, 33.63, 0.87, 2.16 ] x_coord = [0, 1, 2, 3, 4, 5] x_labels = [ '[2]\n2017', '[5]\n2017', '[6]\n2019', '[1]\n2019', '[3]\n2019', '[4]\n2019' ] plt.text(x[0] + 0.1, bleu[0] - 1, 'NMT', { 'color': 'black', 'fontsize': 22 }) plt.text(x[1] + 0.1, bleu[1] - 1, 'NMT', { 'color': 'black', 'fontsize': 22 }) plt.text(x[2] + 0.1, bleu[2] - 1, 'NMT', { 'color': 'black', 'fontsize': 22 }) plt.text(x[3] + 0.1, bleu[3] - 1, 'NMT', { 'color': 'black', 'fontsize': 22 }) plt.text(x[4] + 0.1, bleu[4] - 1, 'NMT', { 'color': 'black', 'fontsize': 22 }) plt.text(x[5] + 0.1, bleu[5] - 1, 'HMM', { 'color': 'black', 'fontsize': 22 }) plt.text(x[6] + 0.1, bleu[6] - 2, 'NMT', { 'color': 'black', 'fontsize': 22 }) plt.text(x[7] + 0.1, bleu[7] - 1, 'HMM', { 'color': 'black', 'fontsize': 22 }) plt.text(x[8] + 0.1, bleu[8], 'NNGen', {'color': 'black', 'fontsize': 22}) plt.text(x[9] + 0.1, bleu[9] - 1, 'NMT', { 'color': 'black', 'fontsize': 22 }) plt.text(x[10] + 0.1, bleu[10] - 1, 'NMT', { 'color': 'black', 'fontsize': 22 }) plt.text(x[11] + 0.1, bleu[11] - 1, 'NMT', { 'color': 'black', 'fontsize': 22 }) plt.text(x[12] + 0.1, bleu[12] - 1, 'NMT', { 'color': 'black', 'fontsize': 22 }) plt.text(x[13] + 0.1, bleu[13], 'NNGen', { 'color': 'black', 'fontsize': 22 }) colors = [ 'darkred', 'green', 'green', 'green', 'red', 'red', 'lightsalmon', 'lightsalmon', 'lightsalmon', 'peachpuff', 'blue', 'darkred', 'silver', 'silver' ] scale = np.array([450 for _ in range(len(x))]) plt.scatter(x=x, y=bleu, s=scale, c=colors, marker='^') plt.yticks(fontsize=20) plt.xlim(-0.3, 5.7) plt.xticks(x_coord, x_labels, fontsize=20) plt.grid(axis='y', alpha=0.75) plt.xlabel("Статьи", fontsize=25) plt.ylabel("BLEU", fontsize=28) plt.show()
def plot_trial_steps(self): '''Plot target (sig-eps-curve of the tensile test) and trial curves and corresponding phi function together with trail steps from the iteration process. NOTE: the global variable 'rec_trial_steps' must be set to 'True' in order to store the iteration values within the global variables 'phi_trial_list_n' and 'sig_trial_list_n' n - index of the time steps to be considered i - index of the iteration steps performed in order to fit the target curve ''' #------------------------------------------------------------------- # configure the style of the font to be used for labels and ticks #------------------------------------------------------------------- # from matplotlib.font_manager import FontProperties font = FontProperties() # font.serif : Times, Palatino, New Century Schoolbook, Bookman, Computer Modern Roman # font.sans-serif : Helvetica, Avant Garde, Computer Modern Sans serif # font.cursive : Zapf Chancery # font.monospace : Courier, Computer Modern Typewriter font.set_name('Script MT') # name = ['Times New Roman', 'Helvetica', 'Script MT'] #? font.set_family('serif') # family = ['serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'] font.set_style('normal') # style = ['normal', 'italic', 'oblique'] font.set_size('small') # size = ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', '11'] font.set_variant('normal') # variant= ['normal', 'small-caps'] font.set_weight('medium') # weight = ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black'] #------------------------------------------------------------------- p.figure(facecolor='white', dpi=600, figsize=(8, 6)) # white background # time list corresponding to the specified numbers of steps and step size # step_list = [n * self.step_size for n in range(self.n_steps + 1)] # get list of lists containing the trial values of 'sig_app' and 'phi_trail' # the lists are defined as global variables of 'MATSCalibDamageFn' and are filled # within the iteration process when the method 'get_lack_of_fit" is called # phi_trial_list_n = [[1.]] + self.phi_trial_list_n sig_trial_list_n = [[0.]] + self.sig_trial_list_n xrange = 10. # plotting range for strain [mm/m] yrange = 15. # plotting range for stress [MPa] for n in range(self.n_steps): for i in range(len(phi_trial_list_n[n + 1])): x = np.array([step_list[n], step_list[n + 1]]) eps = 1000. * x # plot strains in permil on the x-axis #-------------------------------------- # sig-eps trial #-------------------------------------- # plot the numerically calculated sig-eps-curve (tensile test) # (with trial steps) # sig_trail = np.array( [sig_trial_list_n[n][-1], sig_trial_list_n[n + 1][i]]) p.subplot(222) p.plot(eps, sig_trail, color='k', linewidth=1) p.xlabel(r'strain $\varepsilon$ [1E-3]', fontproperties=font) p.ylabel('stress $\sigma$ [MPa]', fontproperties=font) if self.format_ticks: # format ticks for plot p.axis([0, xrange, 0., yrange], fontproperties=font) locs, labels = p.xticks() p.xticks(locs, ["%.0f" % x for x in locs], fontproperties=font) locs, labels = p.yticks() p.yticks(locs, ["%.0f" % x for x in locs], fontproperties=font) #-------------------------------------- # phi_trail #-------------------------------------- # plot the fitted phi-function # (with trial steps) # p.subplot(224) phi_trail = np.array( [phi_trial_list_n[n][-1], phi_trial_list_n[n + 1][i]]) p.plot(eps, phi_trail, color='k', linewidth=1) p.xlabel(r'strain $\varepsilon$ [1E-3]', fontproperties=font) p.ylabel('integrity $\phi$ [-]', fontproperties=font) if self.format_ticks: # format ticks for plot p.yticks([0, 0.2, 0.4, 0.6, 0.8, 1.0]) p.axis([0, xrange, 0., 1.]) locs, labels = p.xticks() p.xticks(locs, ["%.0f" % x for x in locs], fontproperties=font) locs, labels = p.yticks() p.yticks(locs, ["%.1f" % x for x in locs], fontproperties=font) #-------------------------------------- # sig-eps target #-------------------------------------- # plot the sig-eps-target curve (tensile test) # p.subplot(221) eps = 1000. * self.mfn_line_array_target.xdata[:-1] sig_target = self.mfn_line_array_target.ydata[:-1] p.plot(eps, sig_target, color='black', linewidth=1) p.xlabel(r'strain $\varepsilon$ [1E-3]', fontproperties=font) p.ylabel('stress $\sigma$ [MPa]', fontproperties=font) if self.format_ticks: # format ticks for plot p.axis([0, xrange, 0., yrange]) locs, labels = p.xticks() p.xticks(locs, ["%.0f" % x for x in locs], fontproperties=font) locs, labels = p.yticks() p.yticks(locs, ["%.0f" % x for x in locs], fontproperties=font) #-------------------------------------- # phi_trail (final) #-------------------------------------- # plot the corresponding fitted phi-function # (without trial steps) # p.subplot(223) eps = 1000. * self.fitted_phi_fn.xdata[:-1] phi_fn = self.fitted_phi_fn.ydata[:-1] p.plot(eps, phi_fn, color='black', linewidth=1) p.xlabel(r'strain $\varepsilon$ [1E-3]', fontproperties=font) p.ylabel('integrity $\phi$ [-]', fontproperties=font) if self.format_ticks: # format ticks for plot p.yticks([0, 0.2, 0.4, 0.6, 0.8, 1.0]) p.axis([0, xrange, 0., 1.]) locs, labels = p.xticks() p.xticks(locs, ["%.0f" % x for x in locs], fontproperties=font) locs, labels = p.yticks() p.yticks(locs, ["%.1f" % x for x in locs], fontproperties=font) # save figure with calibration process in directory # "/simdb/simdata/lcc_table/output_images/save_fig_to_file.png" simdata_dir = os.path.join(simdb.simdata_dir, 'mats_calib_damage_fn') if os.path.isdir(simdata_dir) == False: os.makedirs(simdata_dir) ctt_key = self.test_key filename = os.path.join(simdata_dir, ctt_key + self.param_key + '.pdf') p.savefig(filename) print('plot_trail_steps.png saved to file %s' % (filename)) filename = os.path.join(simdata_dir, ctt_key + self.param_key + '.png') p.savefig(filename, dpi=600) print('plot_trail_steps.png saved to file %s' % (filename)) p.show()
def _draw_tracks(self, **kwargs): '''create an axis for each track and moves accordingly all the child features''' self.ax.set_axis_off() self.Drawn_objects = [] self.track_axes = [] draw_xmin = kwargs.get('xmin', None) draw_xmax = kwargs.get('xmax', None) if draw_xmin: self.xmin = draw_xmin if draw_xmax: self.xmax = draw_xmax '''estimate track height using track.drawn_lines find max and min x coords, and check for colorbar presence in at least one track''' cbars = False self.drawn_lines = 0 Xs = [] track_height_user_specified = False for track in self.tracks: if track.features: #skip tracks with no features if track.track_height and self.fig_height: #if not fig_height is specified user track heights are ignored track_height_user_specified = True if track_height_user_specified and not track.track_height: track_height_user_specified = False #disable if some track has not a specified heigth warnings.warn( 'All tracks need to have a specified track_height, reverting to automatic track height' ) track._sort_features( dpi=self.dpi, xoffset=self.xmin) #THIS WILL DRAW ALL THE FEATURES if track.draw_cb: if cbars != 'label': cbars = 'simple' if track.cb_label: cbars = 'label' self.drawn_lines += track.drawn_lines Xs.append(track.xmin) Xs.append(track.xmax) if self.xmin == None: self.xmin = min(Xs) if self.xmax == None: self.xmax = max(Xs) '''auto estimate fig_heigth and panning if needed ''' if not self.fig_height: #automatcally set fig height basing on the total number of features self.fig_height = self._estimate_fig_height() self.vpadding = (float(self.padding) / self.dpi) / self.fig_height self.vtrack_padding = (float(self.track_padding) / self.dpi) / self.fig_height '''set colorbar dimension ''' if cbars == 'label': cbar_extent = 0.015 cbar_axis_space = 0.05 cbar_right_pad = 0.03 else: cbar_extent = 0.015 cbar_axis_space = 0.05 cbar_right_pad = 0.01 '''arrange tracks''' axis_left_pad = self.hpadding default_figure_bottom_space = self.vpadding + float( self.figure_bottom_space) / self.dpi axis_bottom_pad = 1.0 - default_figure_bottom_space axis_width = 1. - 2 * self.hpadding axis_scale = None # used to persist the same scale on all the tracks if cbars: axis_width -= (cbar_extent + cbar_axis_space + cbar_right_pad) '''cycle trought tracks and draw them as axix object ''' #canvas_height = 0 for track_num, track in enumerate(self.tracks): if track.features: #skip tracks with no features '''define axis dimensions and position and create axis''' if track_height_user_specified: axis_height = track.track_height / float(self.fig_height) else: if axis_scale: axis_height = axis_scale * track.drawn_lines else: axis_height = ( float(track.drawn_lines) / self.drawn_lines) - self.vpadding / ( 2. * len(self.tracks) ) - default_figure_bottom_space / len(self.tracks) axis_scale = axis_height / float(track.drawn_lines) axis_bottom_pad -= (axis_height + self.vtrack_padding / 2.) axis = matplotlib.pyplot.axes( [axis_left_pad, axis_bottom_pad, axis_width, axis_height ], ) self.track_axes.append(axis) '''handle track axis display, ticks and tickslabel ''' '''set Y lims ''' if isinstance(track, tracks.PlotTrack): if track.show_name: if track.show_name == 'top': axis.set_ylim(track.Ycord, track.ymax + 1) track_name = axis.text( self.xmin + (self.xmax * 0.01), track.ymax + .5, track.name, horizontalalignment='left', verticalalignment='bottom', fontproperties=track.name_font_feat, ) elif track.show_name == 'bottom': axis.set_ylim(track.Ycord - 1, track.ymax) track_name = axis.text( self.xmin + (self.xmax * 0.01), track.Ycord - .5, track.name, horizontalalignment='left', verticalalignment='bottom', fontproperties=track.name_font_feat, ) else: axis.set_ylim(track.Ycord, track.ymax) else: if track.show_name: if track.show_name == 'top': axis.set_ylim(track.Ycord, track.ymax + 2.5) track_name = axis.text( self.xmin + (self.xmax * 0.01), track.ymax + 1.5, track.name, horizontalalignment='left', verticalalignment='bottom', fontproperties=track.name_font_feat, ) elif track.show_name == 'bottom': axis.set_ylim(track.Ycord - 1, track.ymax + 1.5) track_name = axis.text( self.xmin + (self.xmax * 0.01), track.Ycord - 0., track.name, horizontalalignment='left', verticalalignment='bottom', fontproperties=track.name_font_feat, ) else: axis.set_ylim( track.Ycord, track.ymax + 1.5, ) '''set X lims''' axis.set_xlim(self.xmin, self.xmax) '''handle last bottom axis ''' if (track_num + 1 == len( self.tracks)) and ('force no axis' not in track.draw_axis): track.draw_axis.append('bottom') if not self.track_padding: if (track_num + 1 != len(self.tracks)): if 'bottom' in track.draw_axis: del track.draw_axis[track.draw_axis.index( 'bottom')] if 'top' in track.draw_axis: del track.draw_axis[track.draw_axis.index('top')] axis.spines["top"].set_color('none') '''handle axis and ticks''' for spine in ["right", "left", "top", "bottom"]: if spine not in track.draw_axis: axis.spines[spine].set_color('none') # don't draw axis if ("right" not in track.draw_axis) and ("left" not in track.draw_axis): axis.yaxis.set_ticks([]) # dont'show ticks and labels on y if 'top' not in track.draw_axis: axis.xaxis.set_ticks_position( 'bottom') # only show bottom ticks if 'right' not in track.draw_axis: axis.yaxis.set_ticks_position( 'left') # only show left ticks '''handle X ticks and labels ''' step = int( round(self.xmax / 10., 1 - len(str(int(self.xmax / 10.))))) auto_X_major_ticks = range(self.xmin, self.xmax + 1, step) step_min = step / 4. tick = auto_X_major_ticks[0] auto_X_minor_ticks = [] while tick <= self.xmax: auto_X_minor_ticks.append(int(round(tick))) tick += step_min '''use sequence as X ticks ''' if track.x_use_sequence: if len(track.x_use_sequence) < self.xmax - self.xmin: raise Exception( 'Sequence must be of the same length of X coords') track.xticks_minor = [] track.xticklabels_minor = [] for i, seq in enumerate(track.x_use_sequence): if self.xmin + i <= self.xmax: track.xticks_minor.append(self.xmin + i) track.xticklabels_minor.append(str(seq)) '''major X ticks ''' X_major_ticks_labels = None if track.xticks_major != None: X_major_ticks = track.xticks_major if (track.xticklabels_major != None) \ and len(track.xticklabels_major) == len(track.xticks_major): X_major_ticks_labels = track.xticklabels_major else: X_major_ticks = auto_X_major_ticks if 'bottom' in track.draw_axis: axis.set_xticks(X_major_ticks) else: axis.set_xticks([]) '''major ticks labels ''' if (track_num + 1 == len( self.tracks )) or track.show_xticklabels: # last track or forced display if X_major_ticks_labels == None: X_major_ticks_labels = [] for i in X_major_ticks: if isinstance(i, (float, int)): X_major_ticks_labels.append( i + self.start_position) else: X_major_ticks_labels.append(i) axis.set_xticklabels(X_major_ticks_labels, fontsize=track.tickfontsize) if track.x_use_sequence: try: axis.xaxis.set_tick_params(pad=15, ) except: pass #not supported in matplotlib <1 else: axis.set_xticklabels([]) '''minor X ticks ''' X_minor_ticks_labels = None if track.xticks_minor != None: X_minor_ticks = track.xticks_minor if (track.xticklabels_minor != None) \ and len(track.xticklabels_minor) == len(track.xticks_minor): X_minor_ticks_labels = track.xticklabels_minor else: X_minor_ticks = auto_X_minor_ticks if 'bottom' in track.draw_axis: axis.set_xticks(X_minor_ticks, minor=True) else: axis.set_xticks([], minor=True) '''minor ticks labels ''' if (track_num + 1 == len( self.tracks )) or track.show_xticklabels: # last track or forced display if X_minor_ticks_labels == None: X_minor_ticks_labels = [] '''for i in X_minor_ticks: if i in X_major_ticks: X_minor_ticks_labels.append('') else: label = '' if isinstance(i, (float, int)): label = str(i+ self.start_position) else: label = i if (len(str(i).split('.')[0])>=4) : #avoid too long minor ticks label = '' X_minor_ticks_labels = [] break#no minor ticks displayed X_minor_ticks_labels.append(label)''' axis.set_xticklabels(X_minor_ticks_labels, fontsize=track.tickfontsize_minor, minor=True) else: axis.set_xticklabels([], minor=True) '''handle Y ticks and labels ''' '''major Y ticks ''' Y_major_ticks_labels = None if track.yticks_major != None: Y_major_ticks = track.yticks_major if (track.yticklabels_major != None) \ and len(track.yticklabels_major) == len(track.yticks_major): Y_major_ticks_labels = track.yticklabels_major else: Y_major_ticks = None if ('left' in track.draw_axis) and track.yticks_major: axis.set_yticks(Y_major_ticks) '''major ticks labels ''' if Y_major_ticks and track.show_yticklabels: if Y_major_ticks_labels == None: Y_major_ticks_labels = [] for i in Y_major_ticks: Y_major_ticks_labels.append(i) axis.set_yticklabels(Y_major_ticks_labels, fontsize=track.tickfontsize) else: try: axis.yaxis.set_tick_params( labelsize=track.tickfontsize) except: pass #not supported in matplotlib <1 '''minor Y ticks ''' Y_minor_ticks_labels = None if track.yticks_minor != None: Y_minor_ticks = track.yticks_minor if (track.yticklabels_minor != None) \ and len(track.yticklabels_minor) == len(track.yticks_minor): Y_minor_ticks_labels = track.yticklabels_minor else: Y_minor_ticks = None if ('left' in track.draw_axis) and track.yticks_minor: axis.set_yticks(Y_minor_ticks, minor=True) '''minor ticks labels ''' if Y_minor_ticks and track.show_yticklabels: if Y_minor_ticks_labels == None: Y_minor_ticks_labels = [] for i in Y_minor_ticks: if i in Y_major_ticks: Y_minor_ticks_labels.append('') else: Y_minor_ticks_labels.append(i) axis.set_yticklabels(Y_minor_ticks_labels, fontsize=track.tickfontsize_minor, minor=True) else: try: axis.yaxis.set_tick_params( which='minor', labelsize=track.tickfontsize) except: pass #not supported in matplotlib <1 '''draw grid''' if self.grid: if (self.grid == 'major') or (self.grid == 'both'): for X in auto_X_major_ticks: axis.axvline(X, ls=':', c='grey', alpha=0.66, zorder=-1) if (self.grid == 'minor') or (self.grid == 'both'): for X in auto_X_minor_ticks: axis.axvline(X, ls=':', c='grey', alpha=0.33, zorder=-1) '''add feature patches to track axes ''' for feature in track.features: self.Drawn_objects.append(feature) for patch in feature.patches: if isinstance(patch, matplotlib.lines.Line2D): axis.add_line(patch) elif isinstance(patch, matplotlib.patches.Patch): axis.add_patch(patch) else: axis.add_artist(patch) patch.set_transform( axis.transData ) # IMPORTANT WORKAROUND!!! if not manually set, transform is not passed correctly in Line2D objects for feat_name in feature.feat_name: axis.add_artist(feat_name) if track.draw_cb: cb_axis = matplotlib.pyplot.axes([ axis_left_pad + axis_width + cbar_axis_space - cbar_right_pad, axis_bottom_pad, cbar_extent, axis_height ], ) if (track.min_score == None) and (track.max_score == None): for feat in track.features: if feat.norm != None: track.norm = feat.norm break cb1 = matplotlib.colorbar.ColorbarBase( cb_axis, cmap=track.cm, norm=track.norm, alpha=track.cb_alpha, orientation='vertical') if track.cb_label: cb1.set_label(track.cb_label) #cb_axis.axes.set_axis_off() for label in cb_axis.get_yticklabels(): label.set_fontsize('xx-small') '''handle legend ''' legend_font = FontProperties() legend_font.set_size('x-small') legend_font.set_family('serif') legend_font.set_weight('normal') axis.legend(prop=legend_font) '''set panel size and panning ''' self.fig.set_figheight(self.fig_height) self.fig.set_figwidth(self.fig_width)
def plot_ACTimes(bundle, size, fraction=1.0, name='Name_left_empty', save=False, plotting=False): chain, mcmc, niter = bundle.mcmc_trimmedchain, bundle.mcmc, bundle.mcmc_Niter somestate = mcmc.state.deepcopy() actimes = np.zeros((somestate.Nvars, 2)) if (plotting): fig = plt.figure(figsize=(size[0], size[1])) font = FontProperties() font.set_family('serif') font.set_style('italic') #fig.suptitle('Autocorelation', fontsize=12) for i in range(somestate.Nvars): if (plotting): ax = plt.subplot(somestate.Nvars + 1, 1, 1 + i) ax.set_ylabel(somestate.get_keys()[i]) ax.yaxis.label.set_size(28) ax.tick_params(axis='x', labelbottom='off') ax.tick_params(axis='both', labelsize=13) ax.locator_params(axis='y', nbins=3) plt.grid() if (bundle.mcmc_is_emcee): chain_from_emcee = bundle.mcmc_chain Nwalkers = bundle.mcmc_Nwalkers Niter = bundle.mcmc_Niter temp = np.zeros(Niter / Nwalkers) x = 0 for k in range(Nwalkers): for p in range(Niter / Nwalkers): temp[p] = chain_from_emcee[(Niter / Nwalkers) * k + p, i] r = auto_correlation(temp) s = np.sum(r) if (plotting): ax.plot(r, alpha=0.39, color="darkolivegreen") for j in range(len(r)): if (r[j] < 0.5): actimes[i, 0] += j break for j in range(len(r)): if (r[j] >= 0.0): actimes[i, 1] += r[j] else: break actimes[i, 0] /= Nwalkers actimes[i, 1] /= Nwalkers else: r = auto_correlation(chain[:, i]) if (plotting): ax.plot(r) for j in range(len(r)): if (r[j] < 0.5): actimes[i, 0] = j break for j in range(len(r)): if (r[j] >= 0.0): actimes[i, 1] += r[j] else: break print "AC time {t}".format(t=actimes[i]) if (plotting): ax.set_xlabel("$k-lag$") ax.xaxis.label.set_size(28) ax.tick_params(axis='x', labelbottom='on') plt.xlim([0, len(r) * fraction]) if (save): plt.savefig('mcmcplots/{n}.png'.format(n=name), bbox_inches='tight') plt.close('all') bundle.mcmc_actimes = actimes
class Colorbar(object): def __init__(self, parent): self._figure = parent._figure self._colorbar_axes = None self._parent = parent self._base_settings = {} self._ticklabel_fontproperties = FontProperties() self._axislabel_fontproperties = FontProperties() @auto_refresh def show(self, location='right', width=0.2, pad=0.05, ticks=None, labels=True, log_format=False, box=None, box_orientation='vertical', axis_label_text=None, axis_label_rotation=None, axis_label_pad=5): """ Show a colorbar on the side of the image. Parameters ---------- location : str, optional Where to place the colorbar. Should be one of 'left', 'right', 'top', 'bottom'. width : float, optional The width of the colorbar relative to the canvas size. pad : float, optional The spacing between the colorbar and the image relative to the canvas size. ticks : list, optional The position of the ticks on the colorbar. labels : bool, optional Whether to show numerical labels. log_format : bool, optional Whether to format ticks in exponential notation box : list, optional A custom box within which to place the colorbar. This should be in the form [xmin, ymin, dx, dy] and be in relative figure units. This overrides the location argument. box_orientation str, optional The orientation of the colorbar within the box. Can be 'horizontal' or 'vertical' axis_label_text str, optional Optional text label of the colorbar. """ self._base_settings['location'] = location self._base_settings['width'] = width self._base_settings['pad'] = pad self._base_settings['ticks'] = ticks self._base_settings['labels'] = labels self._base_settings['log_format'] = log_format self._base_settings['box'] = box self._base_settings['box_orientation'] = box_orientation self._base_settings['axis_label_text'] = axis_label_text self._base_settings['axis_label_rotation'] = axis_label_rotation self._base_settings['axis_label_pad'] = axis_label_pad if self._parent.image: if self._colorbar_axes: self._figure.delaxes(self._colorbar_axes) if box is None: divider = make_axes_locatable(self._parent.ax) if location == 'right': self._colorbar_axes = divider.new_horizontal( size=width, pad=pad, axes_class=maxes.Axes) orientation = 'vertical' elif location == 'top': self._colorbar_axes = divider.new_vertical( size=width, pad=pad, axes_class=maxes.Axes) orientation = 'horizontal' elif location == 'left': warnings.warn("Left colorbar not fully implemented") self._colorbar_axes = divider.new_horizontal( size=width, pad=pad, pack_start=True, axes_class=maxes.Axes) locator = divider.new_locator(nx=0, ny=0) self._colorbar_axes.set_axes_locator(locator) orientation = 'vertical' elif location == 'bottom': warnings.warn("Bottom colorbar not fully implemented") self._colorbar_axes = divider.new_vertical( size=width, pad=pad, pack_start=True, axes_class=maxes.Axes) locator = divider.new_locator(nx=0, ny=0) self._colorbar_axes.set_axes_locator(locator) orientation = 'horizontal' else: raise Exception("location should be one of: right/top") self._figure.add_axes(self._colorbar_axes) else: self._colorbar_axes = self._figure.add_axes(box) orientation = box_orientation if log_format: format = LogFormatterMathtext() else: format = None self._colorbar = self._figure.colorbar(self._parent.image, cax=self._colorbar_axes, orientation=orientation, format=format, ticks=ticks) if axis_label_text: if axis_label_rotation: self._colorbar.set_label(axis_label_text, rotation=axis_label_rotation) else: self._colorbar.set_label(axis_label_text) if location == 'right': for tick in self._colorbar_axes.yaxis.get_major_ticks(): tick.tick1line.set_visible(True) tick.tick2line.set_visible(True) tick.label1.set_visible(False) tick.label2.set_visible(labels) self._colorbar_axes.yaxis.set_label_position('right') self._colorbar_axes.yaxis.labelpad = axis_label_pad elif location == 'top': for tick in self._colorbar_axes.xaxis.get_major_ticks(): tick.tick1line.set_visible(True) tick.tick2line.set_visible(True) tick.label1.set_visible(False) tick.label2.set_visible(labels) self._colorbar_axes.xaxis.set_label_position('top') self._colorbar_axes.xaxis.labelpad = axis_label_pad elif location == 'left': for tick in self._colorbar_axes.yaxis.get_major_ticks(): tick.tick1line.set_visible(True) tick.tick2line.set_visible(True) tick.label1.set_visible(labels) tick.label2.set_visible(False) self._colorbar_axes.yaxis.set_label_position('left') self._colorbar_axes.yaxis.labelpad = axis_label_pad elif location == 'bottom': for tick in self._colorbar_axes.xaxis.get_major_ticks(): tick.tick1line.set_visible(True) tick.tick2line.set_visible(True) tick.label1.set_visible(labels) tick.label2.set_visible(False) self._colorbar_axes.xaxis.set_label_position('bottom') self._colorbar_axes.xaxis.labelpad = axis_label_pad else: warnings.warn( "No image is shown, therefore, no colorbar will be plotted") @auto_refresh def update(self): if self._colorbar_axes: self.show(**self._base_settings) @auto_refresh def hide(self): self._figure.delaxes(self._colorbar_axes) self._colorbar_axes = None @auto_refresh def _remove(self): self._figure.delaxes(self._colorbar_axes) # LOCATION AND SIZE @auto_refresh def set_location(self, location): """ Set the location of the colorbar. Should be one of 'left', 'right', 'top', 'bottom'. """ self._base_settings['location'] = location self.show(**self._base_settings) self.set_font(fontproperties=self._ticklabel_fontproperties) self.set_axis_label_font(fontproperties=self._axislabel_fontproperties) @auto_refresh def set_width(self, width): """ Set the width of the colorbar relative to the canvas size. """ self._base_settings['width'] = width self.show(**self._base_settings) self.set_font(fontproperties=self._ticklabel_fontproperties) self.set_axis_label_font(fontproperties=self._axislabel_fontproperties) @auto_refresh def set_pad(self, pad): """ Set the spacing between the colorbar and the image relative to the canvas size. """ self._base_settings['pad'] = pad self.show(**self._base_settings) self.set_font(fontproperties=self._ticklabel_fontproperties) self.set_axis_label_font(fontproperties=self._axislabel_fontproperties) @auto_refresh def set_ticks(self, ticks): """ Set the position of the ticks on the colorbar. """ self._base_settings['ticks'] = ticks self.show(**self._base_settings) self.set_font(fontproperties=self._ticklabel_fontproperties) self.set_axis_label_font(fontproperties=self._axislabel_fontproperties) @auto_refresh def set_labels(self, labels): """ Set whether to show numerical labels. """ self._base_settings['labels'] = labels self.show(**self._base_settings) self.set_font(fontproperties=self._ticklabel_fontproperties) self.set_axis_label_font(fontproperties=self._axislabel_fontproperties) @auto_refresh def set_box(self, box, box_orientation='vertical'): """ Set the box within which to place the colorbar. This should be in the form [xmin, ymin, dx, dy] and be in relative figure units. The orientation of the colorbar within the box can be controlled with the box_orientation argument. """ self._base_settings['box'] = box self._base_settings['box_orientation'] = box_orientation self.show(**self._base_settings) self.set_font(fontproperties=self._ticklabel_fontproperties) self.set_axis_label_font(fontproperties=self._axislabel_fontproperties) @auto_refresh def set_axis_label_text(self, axis_label_text): """ Set the colorbar label text. """ self._base_settings['axis_label_text'] = axis_label_text self.show(**self._base_settings) self.set_font(fontproperties=self._ticklabel_fontproperties) self.set_axis_label_font(fontproperties=self._axislabel_fontproperties) @auto_refresh def set_axis_label_rotation(self, axis_label_rotation): """ Set the colorbar label rotation. """ self._base_settings['axis_label_rotation'] = axis_label_rotation self.show(**self._base_settings) self.set_font(fontproperties=self._ticklabel_fontproperties) self.set_axis_label_font(fontproperties=self._axislabel_fontproperties) @auto_refresh def set_axis_label_pad(self, axis_label_pad): """ Set the colorbar label displacement, in points. """ self._base_settings['axis_label_pad'] = axis_label_pad self.show(**self._base_settings) self.set_font(fontproperties=self._ticklabel_fontproperties) self.set_axis_label_font(fontproperties=self._axislabel_fontproperties) # FONT PROPERTIES @auto_refresh def set_label_properties(self, *args, **kwargs): warnings.warn( "set_label_properties is deprecated - use set_font instead", DeprecationWarning) self.set_font(*args, **kwargs) @auto_refresh @fixdocstring def set_font(self, family=None, style=None, variant=None, stretch=None, weight=None, size=None, fontproperties=None): """ Set the font of the tick labels. Parameters ---------- common: family, style, variant, stretch, weight, size, fontproperties Notes ----- Default values are set by matplotlib or previously set values if set_font has already been called. Global default values can be set by editing the matplotlibrc file. """ if family: self._ticklabel_fontproperties.set_family(family) if style: self._ticklabel_fontproperties.set_style(style) if variant: self._ticklabel_fontproperties.set_variant(variant) if stretch: self._ticklabel_fontproperties.set_stretch(stretch) if weight: self._ticklabel_fontproperties.set_weight(weight) if size: self._ticklabel_fontproperties.set_size(size) if fontproperties: self._ticklabel_fontproperties = fontproperties # Update the tick label font properties for label in self._colorbar_axes.get_xticklabels(): label.set_fontproperties(self._ticklabel_fontproperties) for label in self._colorbar_axes.get_yticklabels(): label.set_fontproperties(self._ticklabel_fontproperties) # Also update the offset text font properties label = self._colorbar_axes.xaxis.get_offset_text() label.set_fontproperties(self._ticklabel_fontproperties) label = self._colorbar_axes.yaxis.get_offset_text() label.set_fontproperties(self._ticklabel_fontproperties) @auto_refresh @fixdocstring def set_axis_label_font(self, family=None, style=None, variant=None, stretch=None, weight=None, size=None, fontproperties=None): """ Set the font of the tick labels. Parameters ---------- common: family, style, variant, stretch, weight, size, fontproperties Notes ----- Default values are set by matplotlib or previously set values if set_font has already been called. Global default values can be set by editing the matplotlibrc file. """ if family: self._axislabel_fontproperties.set_family(family) if style: self._axislabel_fontproperties.set_style(style) if variant: self._axislabel_fontproperties.set_variant(variant) if stretch: self._axislabel_fontproperties.set_stretch(stretch) if weight: self._axislabel_fontproperties.set_weight(weight) if size: self._axislabel_fontproperties.set_size(size) if fontproperties: self._axislabel_fontproperties = fontproperties # Update the label font properties label = self._colorbar_axes.xaxis.get_label() label.set_fontproperties(self._axislabel_fontproperties) label = self._colorbar_axes.yaxis.get_label() label.set_fontproperties(self._axislabel_fontproperties) # FRAME PROPERTIES @auto_refresh def set_frame_linewidth(self, linewidth): """ Set the linewidth of the colorbar frame, in points. """ warnings.warn("This method is not functional at this time") for key in self._colorbar_axes.spines: self._colorbar_axes.spines[key].set_linewidth(linewidth) @auto_refresh def set_frame_color(self, color): """ Set the color of the colorbar frame, in points. """ warnings.warn("This method is not functional at this time") for key in self._colorbar_axes.spines: self._colorbar_axes.spines[key].set_edgecolor(color)
class Colorbar(object): def __init__(self, parent): self._figure = parent._figure self._colorbar_axes = None self._parent = parent self._base_settings = {} self._label_fontproperties = FontProperties() @auto_refresh def show(self, location='right', width=0.2, pad=0.05, ticks=None, labels=True): ''' Show a colorbar on the side of the image. Optional Keyword Arguments: *location*: [ string ] Where to place the colorbar. Should be one of 'left', 'right', 'top', 'bottom'. *width*: [ float ] The width of the colorbar relative to the canvas size. *pad*: [ float ] The spacing between the colorbar and the image relative to the canvas size. *ticks*: [ None or list ] The position of the ticks on the colorbar. *labels*: [ True or False ] Whether to show numerical labels. ''' self._base_settings['location'] = location self._base_settings['width'] = width self._base_settings['pad'] = pad if self._parent.image: if self._colorbar_axes: self._parent._figure.delaxes(self._colorbar_axes) divider = make_axes_locatable(self._parent._ax1) if location == 'right': self._colorbar_axes = divider.new_horizontal( size=width, pad=pad, axes_class=maxes.Axes) orientation = 'vertical' elif location == 'top': self._colorbar_axes = divider.new_vertical( size=width, pad=pad, axes_class=maxes.Axes) orientation = 'horizontal' elif location == 'left': warnings.warn("Left colorbar not fully implemented") self._colorbar_axes = divider.new_horizontal( size=width, pad=pad, pack_start=True, axes_class=maxes.Axes) locator = divider.new_locator(nx=0, ny=0) self._colorbar_axes.set_axes_locator(locator) orientation = 'vertical' elif location == 'bottom': warnings.warn("Bottom colorbar not fully implemented") self._colorbar_axes = divider.new_vertical( size=width, pad=pad, pack_start=True, axes_class=maxes.Axes) locator = divider.new_locator(nx=0, ny=0) self._colorbar_axes.set_axes_locator(locator) orientation = 'horizontal' else: raise Exception("location should be one of: right/top") self._parent._figure.add_axes(self._colorbar_axes) self._colorbar = self._parent._figure.colorbar( self._parent.image, cax=self._colorbar_axes, orientation=orientation, ticks=ticks) if location == 'right': for tick in self._colorbar_axes.yaxis.get_major_ticks(): tick.tick1On = True tick.tick2On = True tick.label1On = False tick.label2On = labels if location == 'top': for tick in self._colorbar_axes.xaxis.get_major_ticks(): tick.tick1On = True tick.tick2On = True tick.label1On = False tick.label2On = labels if location == 'left': for tick in self._colorbar_axes.yaxis.get_major_ticks(): tick.tick1On = True tick.tick2On = True tick.label1On = labels tick.label2On = False if location == 'bottom': for tick in self._colorbar_axes.xaxis.get_major_ticks(): tick.tick1On = True tick.tick2On = True tick.label1On = labels tick.label2On = False else: warnings.warn( "No image is shown, therefore, no colorbar will be plotted") @auto_refresh def update(self): if self._colorbar_axes: self.show(**self._base_settings) @auto_refresh def hide(self): self._parent._figure.delaxes(self._colorbar_axes) self._colorbar_axes = None @auto_refresh def _remove(self): self._parent._figure.delaxes(self._colorbar_axes) # LOCATION AND SIZE @auto_refresh def set_location(self, location): ''' Set the location of the colorbar. Should be one of 'left', 'right', 'top', 'bottom'. ''' self._base_settings['location'] = location self.show(**self._base_settings) self.set_font(fontproperties=self._label_fontproperties) @auto_refresh def set_width(self, width): ''' Set the width of the colorbar relative to the canvas size. ''' self._base_settings['width'] = width self.show(**self._base_settings) self.set_font(fontproperties=self._label_fontproperties) @auto_refresh def set_pad(self, pad): ''' Set the spacing between the colorbar and the image relative to the canvas size. ''' self._base_settings['pad'] = pad self.show(**self._base_settings) self.set_font(fontproperties=self._label_fontproperties) # FONT PROPERTIES @auto_refresh def set_label_properties(self, *args, **kwargs): warnings.warn( "set_label_properties is deprecated - use set_font instead", DeprecationWarning) self.set_font(*args, **kwargs) @auto_refresh @fixdocstring def set_font(self, family=None, style=None, variant=None, stretch=None, weight=None, size=None, fontproperties=None): ''' Set the font of the tick labels Optional Keyword Arguments: common: family, style, variant, stretch, weight, size, fontproperties Default values are set by matplotlib or previously set values if set_font has already been called. Global default values can be set by editing the matplotlibrc file. ''' if family: self._label_fontproperties.set_family(family) if style: self._label_fontproperties.set_style(style) if variant: self._label_fontproperties.set_variant(variant) if stretch: self._label_fontproperties.set_stretch(stretch) if weight: self._label_fontproperties.set_weight(weight) if size: self._label_fontproperties.set_size(size) if fontproperties: self._label_fontproperties = fontproperties for label in self._colorbar_axes.get_xticklabels(): label.set_fontproperties(self._label_fontproperties) for label in self._colorbar_axes.get_yticklabels(): label.set_fontproperties(self._label_fontproperties) # FRAME PROPERTIES @auto_refresh def set_frame_linewidth(self, linewidth): ''' Set the linewidth of the colorbar frame, in points ''' warnings.warn("This method is not functional at this time") for key in self._colorbar_axes.spines: self._colorbar_axes.spines[key].set_linewidth(linewidth) @auto_refresh def set_frame_color(self, color): ''' Set the color of the colorbar frame, in points ''' warnings.warn("This method is not functional at this time") for key in self._colorbar_axes.spines: self._colorbar_axes.spines[key].set_edgecolor(color)
def plotDistanceMetric(time, signal, signalNoise, Prediction, Spec, Sens, CorrectPeriod, sizes): #define colors---------------------------------------------------------------------------------------------- # colors = ['#556270', '#4ECDC4', '#FF6B6B', '#C44D58'] # colors =['#339900', '#99cc33', '#ff9966', '#cc3300'] # colors = ['#3c469c', '#b7b3ef', '#f68984', '#f37370'] # colors = ['#009688','#4db6ac','#f63737','#ff0000'] # colors = ['#3366CC', '#79BEDB', '#E84150', '#FFB36D'] colors = ['#3366CC', '#79BEDB', '#E84150', '#FFB36D'] #Specify Font Properties of text------------------------------------------------------------------- font0 = FontProperties() font0.set_weight('medium') font0.set_family('monospace') font0.set_size(25) # Specify Font properties of Legends font1 = FontProperties() font1.set_weight('normal') font1.set_family('sans-serif') font1.set_style('italic') font1.set_size(14) # Specify font properties of Titles font2 = FontProperties() font2.set_size(16) font2.set_family('sans-serif') font2.set_weight('medium') font2.set_style('italic') #create grid------------------------------------------------------------------------------------------------- fig = plt.figure() fig.set_dpi(96) fig.set_figheight(1080 / 96) fig.set_figwidth(1920 / 96) gs0 = grid.GridSpec(3, 2) ax1 = plt.subplot(gs0[:, 1]) ax2 = plt.subplot(gs0[0, 0]) ax3 = plt.subplot(gs0[1, 0]) ax4 = plt.subplot(gs0[2, 0]) #plot signal-------------------------------------------------------------------------------------------------- ax2.plot(time, signal / max(signal), color='#263763') ax2.plot(time, signalNoise, color=colors[0], linewidth=2, label="Noise Selection") ax2.fill_between(time, signalNoise, where=signalNoise == 1, facecolor=colors[1], alpha=0.6, interpolate=True) ax2.tick_params(labelsize=10) ax2.set_xlabel(" Time (s) ", fontproperties=font1) ax2.set_ylabel(" Amplitude (r.u.)", fontproperties=font1) ax2.set_title("True Noise Selection", fontproperties=font2) ax2.axis('tight') ax3.plot(time, signal / max(signal), color='#263763') ax3.plot(time, Prediction, color=colors[2], linewidth=2, label='Noise Predicted Classification') ax3.fill_between(time, Prediction, where=Prediction == 1, facecolor=colors[3], alpha=0.6, interpolate=True) ax3.tick_params(labelsize=10) ax3.set_xlabel(" Time (s) ", fontproperties=font1) ax3.set_ylabel(" Amplitude (r.u.)", fontproperties=font1) ax3.set_title("Noise Prediction", fontproperties=font2) ax3.axis('tight') ax4.plot(time, signal / max(signal), color='#263763') ax4.plot(time, Prediction, color=colors[2], linewidth=2, label='Correct') ax4.plot(time, 1.01 * signalNoise, color=colors[0], linewidth=2, label='Error') ax4.fill_between(time, Prediction, where=Prediction == 1, facecolor=colors[3], alpha=0.6, interpolate=True) ax4.fill_between(time, 1.01 * signalNoise, where=signalNoise == 1, facecolor=colors[1], alpha=0.6, interpolate=True) ax4.tick_params(labelsize=10) ax4.set_xlabel(" Time (s) ", fontproperties=font1) ax4.set_ylabel(" Amplitude (r.u.)", fontproperties=font1) ax4.set_title("Superposition", fontproperties=font2) ax4.axis('tight') #plot pie chart--------------------------------------------------------------------------------------------- labels = 'TP', 'TN', 'FP', 'FN' TP = sizes[0] / sum(sizes) TN = sizes[1] / sum(sizes) FP = sizes[2] / sum(sizes) FN = sizes[3] / sum(sizes) perc = [TP, TN, FP, FN] explode = (0, 0, 0.1, 0.1) # explode a slice if required patches, texts, autotexts = ax1.pie( sizes, colors=colors, shadow=False, explode=explode, autopct='%1.1f%%', startangle=180 ) # draw a circle at the center of pie to make it look like a donut for p, at, pc in zip(patches, autotexts, perc): p.set_linewidth(3) p.set_alpha(0.8) p.set_edgecolor('lightgray') at.set_fontproperties(font0) at.set_size(25 * np.log10(100 * pc + 2)) at.set_path_effects( [pte.Stroke(linewidth=2, foreground='slategray'), pte.Normal()]) at.set_color('black') ax1.legend(patches, labels, loc='best', fontsize=15) centre_circle = plt.Circle((0, 0), 0.35, color='lightgray', fc='white', linewidth=3) ax1.add_artist(centre_circle) text = 'Sensitivity = ' + str(round( Sens * 100.0, 2)) + ' %' + '\n' + 'Specificity = ' + str( round(Spec * 100.0, 2)) + ' %' + '\n' + 'Accuracy = ' + str( round(CorrectPeriod * 100.0, 2)) + ' %' kwargs = dict(size=15, fontweight='medium', va='center', color='slategray') txt = ax1.text(0, 0, text, ha='center', **kwargs) txt.set_path_effects( [pte.Stroke(linewidth=2.5, foreground='white'), pte.Normal()]) ax1.axis('equal') gs0.tight_layout(fig)
from matplotlib import pyplot as plt from matplotlib.font_manager import FontProperties from subprocess import call import random import sys import math from scipy.spatial import ConvexHull from shapely import geometry font = FontProperties() font.set_family('Times New Roman') font.set_size(12) def generate_points_on_circle(size): for i in range(size): angle = random.uniform(0, 2 * math.pi) yield (math.cos(angle), math.sin(angle)) def generate_points_in_circle(size): for i in range(size): angle = random.uniform(0, 2 * math.pi) radius = random.random() yield (radius * math.cos(angle), radius * math.sin(angle)) def generate_points_with_normal_dist(size): return [(random.gauss(0, 1), random.gauss(0, 1)) for i in range(size)]
import numpy as np import pandas as pd from matplotlib.font_manager import FontProperties FONTSTYLE = 'serif' FONTSIZE = 12 hfont = {'family':FONTSTYLE, 'fontsize': FONTSIZE} fontP = FontProperties() fontP.set_family(FONTSTYLE) fontP.set_size('small') def get_halogen_spectra(reference_file): d = np.genfromtxt(reference_file, delimiter=',') wavelength = d[:, 0] intensity_mW = d[:, 1] * 10 ** -6 #[W/(m3sr) into mW/(nmm2sr)] relative_error = d[:, 2] return wavelength, intensity_mW, relative_error def calc_scaling_factors(waveibs, ref, reference_file): wave, intensity, r_err = get_halogen_spectra(reference_file) start_ind = np.where(waveibs > wave[0])[0][0] end_ind = np.where(waveibs < wave[-1])[0][-1] mod_waves = waveibs[start_ind:end_ind] mod_intensity = ref[start_ind:end_ind] assert len(mod_waves) == len(mod_intensity) assert ref[start_ind] == mod_intensity[0]
def plotClusters(y_pred, OSignal, time, XPCA, n_clusters): """ :param y_pred: Matrix with Classification values :param OSignal: Original Signal :param time: Time array :param XPCA: Matrix after PCA. XPCA[0] - feature 1 and XPCA[1] - feature 2 :param n_clusters: Number of clusters :return: plot object of clusters """ #Specify plot parameters # color face_color_r = 248 / 255.0 face_color_g = 247 / 255.0 face_color_b = 249 / 255.0 # pars left = 0.05 # the left side of the subplots of the figure right = 0.95 # the right side of the subplots of the figure bottom = 0.05 # the bottom of the subplots of the figure top = 0.92 # the top of the subplots of the figure wspace = 0.5 # the amount of width reserved for blank space between subplots hspace = 0.4 # the amount of height reserved for white space between subplots pars = SubplotParams(left, bottom, right, top, wspace, hspace) #specify Font properties with fontmanager--------------------------------------------------- font0 = FontProperties() font0.set_weight('medium') font0.set_family('monospace') #Specify Font properties of Legends font1 = FontProperties() font1.set_weight('normal') font1.set_family('sans-serif') font1.set_style('italic') font1.set_size(17) #Specify font properties of Titles font2 = FontProperties() font2.set_size(20) font2.set_family('sans-serif') font2.set_weight('medium') font2.set_style('italic') #Cluster colors--------------------------------------------------------------------------------------------- # scatColors = np.array([x for x in ['darkseagreen', 'indianred', 'cornflowerblue', 'darkorange', 'indigo']]) #scatColors = np.array([x for x in ['#93d1ff', '#ffc425', '#fc3366', '#032569']]) #pallete 1 #scatColors = np.array([x for x in ['#ce3635', '#2caae2', '#2ce2aa', '#c38ce3']]) #pallete Gamut scatColors = np.array([ x for x in [ '#3366CC', '#79BEDB', '#E84150', '#FFB36D', '#6aba8f', '#78136f', '#236B5D', '#AB5436', '#3366CC', '#AB5436' ] ]) markers = np.array( [x for x in {'o', 'v', 's', '*', '8', 'D', 'd', '+', 'o', 'v'}]) Colors = itertools.cycle(scatColors) Markers = itertools.cycle(markers) #Create Grid Frame for Clustering Representation---------------------------------------- PCAFrame = pandas.DataFrame(data=XPCA, columns=['Var1', 'Var2']) g = sns.JointGrid('Var1', 'Var2', PCAFrame) f2 = g.fig f2.set_dpi(96) f2.set_figheight(1080 / 96) f2.set_figwidth(1920 / 96) #Create figure for signal representation with clusters----------------------------------- f, axes = plt.subplots(n_clusters + 1, 1) f.subplots_adjust(left=left, bottom=bottom, right=right, top=top, wspace=wspace, hspace=hspace) f.set_dpi(96) f.set_figheight(1080 / 96) f.set_figwidth(1920 / 96) f.set_facecolor((face_color_r, face_color_g, face_color_b)) axes[0].plot(time, OSignal) #Cycle for figure plotting------------------------------------------------------------------------------ for s in range(0, n_clusters): x = XPCA[np.where(y_pred == s)[0], 0] y = XPCA[np.where(y_pred == s)[0], 1] color = next(Colors) marker = next(Markers) cmap = sns.light_palette(color, as_cmap=True) ax = sns.kdeplot(x, y, cmap=cmap, shade=True, shade_lowest=False, alpha=1, ax=g.ax_joint) txt = ax.text(np.mean(x), np.mean(y), "Cluster " + str(s + 1), fontproperties=font0, size=30, color=color) txt.set_path_effects( [pte.Stroke(linewidth=2.5, foreground='white'), pte.Normal()]) sns.distplot(XPCA[np.where(y_pred == s)[0], 0], bins=50, color=color, kde=True, ax=g.ax_marg_x) sns.distplot(XPCA[np.where(y_pred == s)[0], 1], bins=50, color=color, kde=True, vertical=True, ax=g.ax_marg_y) #plot signal--------------------------------------------------------------------------------------------- sns.despine(fig=f) # with sns.axes_style("darkgrid"): axes[s + 1].plot(time, OSignal, color='gray', alpha=0.2) axes[s + 1].plot(time[np.where(y_pred == s)[0]], OSignal[np.where(y_pred == s)[0]], '.', color=color, marker=marker) # axes[s + 1].patch.set_facecolor('ivory') axes[s + 1].axis('tight') axes[s + 1].tick_params(labelsize=15) axes[s + 1].set_ylabel(" Amplitude (r.u.) ", fontproperties=font1) axes[s + 1].set_title("Signal representation from Cluster: " + str(s), fontproperties=font2) axes[s + 1].grid(True, linestyle='-', color='white') axes[s + 1].axes.get_xaxis().set_visible(False) axes[n_clusters].set_xlabel(" Time (s) ", fontproperties=font1) axes[n_clusters].axes.get_xaxis().set_visible(True) #print(markers[y_pred.tolist()]) for c in range(0, n_clusters): xTemp = XPCA[np.where(y_pred == c)[0], 0] yTemp = XPCA[np.where(y_pred == c)[0], 1] XPCATemp = XPCA[np.where(y_pred == c)[0], :] #Find Dots for that Cluster Selection, Tempor = RemoveUglyDots(XPCATemp) for _X, _Y, _color, _marker in zip(xTemp[Tempor], yTemp[Tempor], scatColors[y_pred.tolist()], markers[y_pred.tolist()]): g.ax_joint.plot(_X, _Y, color=scatColors[c], marker=markers[c], alpha=.5, linewidth=.5) # g.ax_joint.plot(xTemp[Tempor], yTemp[Tempor], color='black', marker='o') # for _Data, _color, _marker in zip(XPCA, scatColors[y_pred.tolist()], markers[y_pred.tolist()]): # g.ax_joint.plot(_Data[0], _Data[1] , color = _color, marker=_marker, alpha=.5, linewidth=.5) # g.plot_joint(plt.scatter, s=10, color=scatColors[y_pred.tolist()], alpha=.35, linewidth=.5) #g.ax_joint.scatter(XPCA[:,0], XPCA[:,1], color=scatColors[y_pred.tolist()], alpha=.35, linewidth=.5) g.set_axis_labels("Feature 1", "Feature 2", fontsize=20, fontproperties=font1) g.fig.suptitle("Clusters after selecting 2 features with PCA ", color="gray", alpha=.9, fontsize=15) g.fig.tight_layout() plt.show()
def main(tax_table, output_dir, samples_to_analyze=None): """Generates pie chart of the most abundant twelve taxa in the sample INPUTS: otu_table -- a biom formatted taxonomy table at the desired level of resolution output_dir -- the location of the directory where output files should be stored. samples_to_analyze -- a list of sample ids to plot. If no value is passed, then all samples in the biom table are analyzed. OUTPUTS: A pdf of the piechart summarizing the most abundant taxa will be generated and saved to the output directory. These will follow the naming convention PIECHART_<SAMPLEID>.pdf. """ # Creates the text around hte file name FILENAME_BEFORE = 'piechart_' FILENAME_AFTER = '.pdf' # Handles string cleaning RENDER = 'LATEX' UNCLASSIFIED = False # Sets up the rare threshhold for RARE_THRESH = 0.0 SUM_MIN = 1 # Sets up axis parameters AXIS_LENGTH = 7.25 AXIS_BORDER = 0.01 AXIS_TITLE = 0 AXIS_LEGEND = 7 # Modifies the axis limits AX_LIMS = [-1.05, 1.05] # Sets up constants for getting the colormap and plotting MAP_NAME = 'BrBG' NUM_SHOW = 12 OTHER_COLOR = array([[85/255, 85/255, 85/255]]) # Sets up plotting parameters FIG_LEGEND = True FIG_COLOR_EDGE = False FIG_LEG_FRAME = False FIG_LEG_OFFSET = [0.95, 0.025, 1.0, 0.95] # Sets up the the legend font LEG_FONT = FontProperties() LEG_FONT.set_size(28) LEG_FONT.set_family('sans-serif') # Sets the general font properties use_latex = True rc_font_family = 'sans-serif' rc_font = ['Helvetica', 'Arial'] # Sets up the colormap colormap = translate_colors((NUM_SHOW-1), MAP_NAME) colormap = vstack((colormap, OTHER_COLOR)) # Sets up plotting constants (axis_dims, fig_dims) = calculate_dimensions_rectangle( axis_width=AXIS_LENGTH, axis_height=AXIS_LENGTH, border=AXIS_BORDER, title=AXIS_TITLE, legend=AXIS_LEGEND) # Walks over a taxa tree and prioritizes based on taxonomy (tree, all_taxa) = build_tree_from_taxontable(tax_table) # Sets up samples for which tables are being generated if samples_to_analyze is not None: samples_to_test = samples_to_analyze else: samples_to_test = all_taxa.keys() # Checks the samples exist if samples_to_test: samples_to_test = set(samples_to_test) tmp = {k: v for k, v in all_taxa.items() if k in samples_to_test} all_taxa = tmp if not samples_to_test: raise ValueError("No samples!") # Walks over the table filt_fun = lambda v, i, md: v.sum() > 0 for samp, filtered_table, rare, unique in sample_rare_unique(tree, tax_table, all_taxa, RARE_THRESH): # abund_fun = lambda v, i, md: i in all_taxa[samp] filtered_table = tax_table.filterObservations(filt_fun) sample_data = filtered_table.sampleData(samp) taxa = filtered_table.ObservationIds # Calculates abundance and limits to the top n samples. abund_rank = calculate_abundance(sample=sample_data, taxa=taxa, sum_min=SUM_MIN) abund_rank = abund_rank[:(NUM_SHOW-1)] # Cleans the greengenes strings and adds an "Other" Category for # missing taxa [sample_tax, sample_freq] = [list(a) for a in zip(*abund_rank)] clean_tax = [clean_greengenes_string(tax, RENDER, unclassified=UNCLASSIFIED) for tax in sample_tax] clean_tax.append('Other') sample_freq.append(1-sum(sample_freq)) # Sets up the sample filename filename = pjoin(output_dir, '%s%s%s' % (FILENAME_BEFORE, samp, FILENAME_AFTER)) # Creates the pie chart render_single_pie(data_vec=sample_freq, group_names=clean_tax, axis_dims=axis_dims, fig_dims=fig_dims, file_out=filename, legend=FIG_LEGEND, colors=colormap, show_edge=FIG_COLOR_EDGE, legend_frame=FIG_LEG_FRAME, rc_font=rc_font, legend_offset=FIG_LEG_OFFSET, rc_fam=rc_font_family, legend_font=LEG_FONT, use_latex=use_latex, x_lims=AX_LIMS, y_lims=AX_LIMS)
class LabelViewer(object): def __init__(self, args): """Visualize bounding boxes""" self.ax = None self.fig = None self.current_index = 0 self.scale = args.scale image_paths = [args.image] label_paths = [args.label] if isdir(args.label): input_names = sorted([ splitext(n)[0] for n in os.listdir(args.label) if splitext(n)[1] == '.json' ]) image_paths = [join(args.image, n + '.jpg') for n in input_names] label_paths = [join(args.label, n + '.json') for n in input_names] self.image_paths = image_paths self.label_paths = label_paths self.font = FontProperties() self.font.set_family(['Luxi Mono', 'monospace']) self.font.set_weight('bold') self.font.set_size(18 * self.scale) self.with_image = True self.with_attr = not args.no_attr self.with_lane = not args.no_lane self.with_drivable = not args.no_drivable self.with_box2d = not args.no_box2d self.with_segment = True self.target_objects = args.target_objects print(len(self.target_objects)) self.out_dir = args.output_dir self.label_map = dict([(l.name, l) for l in labels]) self.color_mode = 'random' self.image_width = 1280 self.image_height = 720 self.instance_mode = False self.drivable_mode = False self.with_post = False # with post processing if args.drivable: self.set_drivable_mode() if args.instance: self.set_instance_mode() def view(self): self.current_index = 0 if self.out_dir is None: self.show() else: self.write() def show(self): # Read and draw image dpi = 80 w = 16 h = 9 self.fig = plt.figure(figsize=(w, h), dpi=dpi) self.ax = self.fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False) if len(self.image_paths) > 1: plt.connect('key_release_event', self.next_image) self.show_image() plt.show() def write(self): dpi = 80 w = 16 h = 9 self.fig = plt.figure(figsize=(w, h), dpi=dpi) self.ax = self.fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False) out_paths = [] for i in range(len(self.image_paths)): self.current_index = i out_name = splitext(split(self.image_paths[i])[1])[0] + '.png' out_path = join(self.out_dir, out_name) if self.show_image(): self.fig.savefig(out_path, dpi=dpi) out_paths.append(out_path) if self.with_post: print('Post-processing') p = Pool(10) if self.instance_mode: p.map(convert_instance_rgb, out_paths) if self.drivable_mode: p = Pool(10) p.map(convert_drivable_rgb, out_paths) def set_instance_mode(self): self.with_image = False self.with_attr = False self.with_drivable = False self.with_lane = False self.with_box2d = False self.with_segment = True self.color_mode = 'instance' self.instance_mode = True self.with_post = True def set_drivable_mode(self): self.with_image = False self.with_attr = False self.with_drivable = True self.with_lane = False self.with_box2d = False self.with_segment = False self.color_mode = 'instance' self.drivable_mode = True self.with_post = True def show_image(self): plt.cla() label_path = self.label_paths[self.current_index] name = splitext(split(label_path)[1])[0] print('Image:', name) self.fig.canvas.set_window_title(name) if self.with_image: image_path = self.image_paths[self.current_index] img = mpimg.imread(image_path) im = np.array(img, dtype=np.uint8) self.ax.imshow(im, interpolation='nearest', aspect='auto') else: self.ax.set_xlim(0, self.image_width - 1) self.ax.set_ylim(0, self.image_height - 1) self.ax.invert_yaxis() self.ax.add_patch( self.poly2patch( [[0, 0, 'L'], [0, self.image_height - 1, 'L'], [self.image_width - 1, self.image_height - 1, 'L'], [self.image_width - 1, 0, 'L']], closed=True, alpha=1., color='black')) # Read annotation labels with open(label_path) as data_file: label = json.load(data_file) objects = label['frames'][0]['objects'] if len(self.target_objects) > 0: objects = get_target_objects(objects, self.target_objects) if len(objects) == 0: return False if 'attributes' in label and self.with_attr: attributes = label['attributes'] self.ax.text(25 * self.scale, 90 * self.scale, ' scene: {}\nweather: {}\n time: {}'.format( attributes['scene'], attributes['weather'], attributes['timeofday']), fontproperties=self.font, color='red', bbox={ 'facecolor': 'white', 'alpha': 0.4, 'pad': 10, 'lw': 0 }) if self.with_drivable: self.draw_drivable(objects) if self.with_lane: self.draw_lanes(objects) if self.with_box2d: [ self.ax.add_patch(self.box2rect(b['box2d'])) for b in get_boxes(objects) ] if self.with_segment: self.draw_segments(objects) self.ax.axis('off') return True def next_image(self, event): if event.key == 'n': self.current_index += 1 elif event.key == 'p': self.current_index -= 1 else: return self.current_index = max( min(self.current_index, len(self.image_paths) - 1), 0) if self.show_image(): plt.draw() else: self.next_image(event) def poly2patch(self, poly2d, closed=False, alpha=1., color=None): moves = {'L': Path.LINETO, 'C': Path.CURVE4} points = [p[:2] for p in poly2d] codes = [moves[p[2]] for p in poly2d] codes[0] = Path.MOVETO if closed: points.append(points[0]) codes.append(Path.CLOSEPOLY) if color is None: color = random_color() # print(codes, points) return mpatches.PathPatch( Path(points, codes), facecolor=color if closed else 'none', edgecolor=color, # if not closed else 'none', lw=1 if closed else 2 * self.scale, alpha=alpha, antialiased=False, snap=True) def draw_drivable(self, objects): objects = get_areas(objects) colors = np.array([[0, 0, 0, 255], [217, 83, 79, 255], [91, 192, 222, 255]]) / 255 for obj in objects: if self.color_mode == 'random': if obj['category'] == 'area/drivable': color = colors[1] else: color = colors[2] alpha = 0.5 else: color = ((1 if obj['category'] == 'area/drivable' else 2) / 255., obj['id'] / 255., 0) alpha = 1 self.ax.add_patch( self.poly2patch(obj['poly2d'], closed=True, alpha=alpha, color=color)) def draw_lanes(self, objects): objects = get_lanes(objects) # colors = np.array([[0, 0, 0, 255], # [217, 83, 79, 255], # [91, 192, 222, 255]]) / 255 colors = np.array([[0, 0, 0, 255], [255, 0, 0, 255], [0, 0, 255, 255] ]) / 255 for obj in objects: if self.color_mode == 'random': if obj['attributes']['direction'] == 'parallel': color = colors[1] else: color = colors[2] alpha = 0.9 else: color = ((1 if obj['category'] == 'area/drivable' else 2) / 255., obj['id'] / 255., 0) alpha = 1 self.ax.add_patch( self.poly2patch(obj['poly2d'], closed=False, alpha=alpha, color=color)) def draw_segments(self, objects): color_mode = self.color_mode for obj in objects: if 'segments2d' not in obj: continue if color_mode == 'random': color = random_color() alpha = 0.5 elif color_mode == 'instance': try: label = self.label_map[obj['category']] color = (label.trainId / 255., obj['id'] / 255., 0) except KeyError: color = (1, 0, 0) alpha = 1 else: raise ValueError('Unknown color mode {}'.format( self.color_mode)) for segment in obj['segments2d']: self.ax.add_patch( self.poly2patch(segment, closed=True, alpha=alpha, color=color)) def box2rect(self, box2d): """generate individual bounding box from label""" x1 = box2d['x1'] y1 = box2d['y1'] x2 = box2d['x2'] y2 = box2d['y2'] # Pick random color for each box box_color = random_color() # Draw and add one box to the figure return mpatches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=3 * self.scale, edgecolor=box_color, facecolor='none', fill=False, alpha=0.75)
from matplotlib.font_manager import FontProperties import matplotlib.pyplot as plt # My phone's internal storage usage by file type file_types_and_usage = ( ('image | jpeg', 1483), ('video | mp4', 678), ('audio | mpeg', 642), ('audio | ogg', 405), ('application | octet-stream', 103), ('other', 247), ) # Prepare fonts font1 = FontProperties() font1.set_family('Source Han Sans TW') font2 = font1.copy() font2.set_size('small') # Prepare subplots fig1, ax1 = plt.subplots() # Draw pie chart _, texts, autotexts = ax1.pie( # sizes [ftu[1] for ftu in file_types_and_usage], labels=[ftu[0] for ftu in file_types_and_usage], autopct='%1.f%%', pctdistance=0.7, startangle=90 )
def plotter(fdict): """ Go """ font0 = FontProperties() font0.set_family("monospace") font0.set_size(16) font1 = FontProperties() font1.set_size(16) pgconn = get_dbconn("asos") ctx = get_autoplot_context(fdict, get_description()) varname = ctx["var"] varname2 = varname.split("_")[1] if varname2 in ["dwpf", "tmpf", "feel"]: varname2 = "i" + varname2 month = ctx["month"] station = ctx["zstation"] if month == "all": months = range(1, 13) elif month == "fall": months = [9, 10, 11] elif month == "winter": months = [12, 1, 2] elif month == "spring": months = [3, 4, 5] elif month == "summer": months = [6, 7, 8] elif month == "gs": months = [5, 6, 7, 8, 9] else: ts = datetime.datetime.strptime("2000-" + month + "-01", "%Y-%b-%d") # make sure it is length two for the trick below in SQL months = [ts.month] df = read_sql( """ WITH obs as ( SELECT (valid + '10 minutes'::interval) at time zone %s as ts, tmpf::int as itmpf, dwpf::int as idwpf, feel::int as ifeel, mslp, alti from alldata where station = %s and extract(month from valid at time zone %s) in %s), agg1 as ( SELECT extract(hour from ts) as hr, max(idwpf) as max_dwpf, max(itmpf) as max_tmpf, min(idwpf) as min_dwpf, min(itmpf) as min_tmpf, min(ifeel) as min_feel, max(ifeel) as max_feel, max(alti) as max_alti, min(alti) as min_alti, max(mslp) as max_mslp, min(mslp) as min_mslp from obs GROUP by hr) SELECT o.ts, a.hr::int as hr, a.""" + varname + """ from agg1 a JOIN obs o on (a.hr = extract(hour from o.ts) and a.""" + varname + """ = o.""" + varname2 + """) ORDER by a.hr ASC, o.ts DESC """, pgconn, params=( ctx["_nt"].sts[station]["tzname"], station, ctx["_nt"].sts[station]["tzname"], tuple(months), ), index_col=None, ) if df.empty: raise NoDataFound("No Data was found.") y0 = 0.1 yheight = 0.8 dy = yheight / 24.0 (fig, ax) = plt.subplots(1, 1, figsize=(8, 8)) ax.set_position([0.12, y0, 0.57, yheight]) ax.barh(df["hr"], df[varname], align="center") ax.set_ylim(-0.5, 23.5) ax.set_yticks([0, 4, 8, 12, 16, 20]) ax.set_yticklabels(["Mid", "4 AM", "8 AM", "Noon", "4 PM", "8 PM"]) ax.grid(True) ax.set_xlim([df[varname].min() - 5, df[varname].max() + 5]) ax.set_ylabel( "Local Time %s" % (ctx["_nt"].sts[station]["tzname"],), fontproperties=font1, ) ab = ctx["_nt"].sts[station]["archive_begin"] if ab is None: raise NoDataFound("Unknown station metadata") fig.text( 0.5, 0.93, ("%s [%s] %s-%s\n" "%s [%s]") % ( ctx["_nt"].sts[station]["name"], station, ab.year, datetime.date.today().year, PDICT[varname], MDICT[month], ), ha="center", fontproperties=font1, ) ypos = y0 + (dy / 2.0) for hr in range(24): sdf = df[df["hr"] == hr] if sdf.empty: continue row = sdf.iloc[0] fig.text( 0.7, ypos, "%3.0f: %s%s" % ( row[varname], row["ts"].strftime("%d %b %Y"), ("*" if len(sdf.index) > 1 else ""), ), fontproperties=font0, va="center", ) ypos += dy ax.set_xlabel( "%s %s, * denotes ties" % (PDICT[varname], UNITS[varname]), fontproperties=font1, ) return plt.gcf(), df
def main(): with open("./data/shape-%s/%s%s%sbox-plot--%s-%s-%s-%s-%s-%s.dat"%(load.f_shape,load.debug_str,load.hires_str,load.slice_str,load.f_shape,load.f_param1,load.f_param2,load.f_param3,load.f_param4,load.f_param5),"r") as boxData: fileLines = boxData.readlines() #get number of boxes and protein types. little hokey but it works. in boxData.readlines(), there is exactly one '\n' newline string #for each protein type block. therefor, the number of protein types is equal to the number of times "\n" appears by itself in the list. numProteinTypes = len([line for line in fileLines if line=="\n"]) numNewLines = numProteinTypes #it follows that the total number of lines in the data file, minus the number of blank lines in the data file, is equal to #the number of protein types * the number of box types. divide by number of protein types to get number of box types. numBoxes = (len(fileLines) - numNewLines)/numProteinTypes #grab the names of the proteins used, and the names of the boxes proteinTypeList = [] boxList = [] for line in fileLines: if (line != "\n"): proteinTypeList += [line.split("\t")[0]] boxList += [line.split("\t")[1]] #prune duplicates proteinTypeList = list(set(proteinTypeList)) boxList = list(set(boxList)) #generate list of proteinType and box combinations to feed into stackData plotNameList_D = [] plotNameList_E = [] numProteinTypes_D = 0 numProteinTypes_E = 0 for box in boxList: for proteinType in proteinTypeList: if "D_" in proteinType: plotNameList_D += ["%s-%s"%(box,proteinType)] if "E_" in proteinType: plotNameList_E += ["%s-%s"%(box,proteinType)] #print "" #print "plotNameList before ", plotNameList_D, "\n" new_plotNameList_D = [0]*len(plotNameList_D) P_Ord = [3,0,2,1,7,4,6,5,11,8,10,9] if load.f_param4 == '97.00': P_Ord = [3,0,2,1,11,8,10,9,15,12,14,13,7,4,6,5] if load.f_param4 == '96.00': P_Ord = [15,12,14,13,3,0,2,1,7,4,6,5,11,8,10,9] for i in range(len(P_Ord)): new_plotNameList_D[i] = plotNameList_D[P_Ord[i]] for i in range(len(plotNameList_D)): plotNameList_D[i] = new_plotNameList_D[i] #print "plotNameList after ",plotNameList_D,"\n" plotProteinLabels = ['MinD:ATP (cyto)','MinD:ATP (mem)','MinE:MinD:ATP','MinD:ADP (cyto)'] #pass plotNameList through stackData to generate the list of line data to be plotted plotCurveList_D = stackData(plotNameList_D) plotCurveList_E = stackData(plotNameList_E) #get a time axis for the plot from the length of one of the data sets we have difD = 2.5 # (um)^2 s^- 1 time_step = .1*load.dx*load.dx/difD #sec print_denominator = 1000 #This is from the c++ I wanted to format things the same here. box_time_step = time_step*print_denominator timeAxis = np.linspace(0,box_time_step*len(plotCurveList_D[0]),len(plotCurveList_D[0])) #begin messy code (to deal with matplotlib) - don't judge me start_time_as_frac_of_ten = float(load.f_param6) end_time_as_frac_of_ten = float(load.f_param7) tot_time = len(plotCurveList_D[0])*box_time_step start = int(tot_time*start_time_as_frac_of_ten/10.0/box_time_step) end = int(tot_time*end_time_as_frac_of_ten/10.0/box_time_step) (start, end) = find_period(plotCurveList_D[len(plotCurveList_D)-2]) (start, end) = find_period(np.array(returnData(boxList[len(boxList)-1], 'D_ND'))) # print useful coordination data period = timeAxis[end-1] - timeAxis[start] print 'period is', period firsttime = timeAxis[start] while firsttime > 9*period: firsttime -= period print 'early start time is', firsttime print 'and end time is ',firsttime+period print 'and file numbers are', firsttime*2, 'and', (firsttime+period)*2 # now offset time so it starts at zero timeAxis = timeAxis - timeAxis[start] if load.f_param4 == '97.00' or (load.f_shape == 'triangle' and load.f_param3 == '6.01'): if load.f_param4 == '97.00': start_time_as_frac_of_ten = 0 end_time_as_frac_of_ten = 2.3 if load.f_shape == 'triangle' and load.f_param3 == '6.01': start_time_as_frac_of_ten = 5.00 end_time_as_frac_of_ten = 9.00 start = int(tot_time*start_time_as_frac_of_ten/10.0/box_time_step) end = int(tot_time*end_time_as_frac_of_ten/10.0/box_time_step) #print set(plotCurveList_D[1]).union(set(plotCurveList_D[2])) #get num on each plot for proteinType in proteinTypeList: if "D_" in proteinType: numProteinTypes_D += 1 if "E_" in proteinType: numProteinTypes_E +=1 #plot scales. colors limited for now. colorScale = ["b","g","r","c","m","y"] alphaScale_D = [n/numProteinTypes for n in range(1,numProteinTypes_D+1)] alphaScale_E = [n/numProteinTypes for n in range(1,numProteinTypes_E+1)] #generate the plot #f, (bax,sectionax) = plt.subplots(1, 2) bax = plt.subplot2grid((2,5), (0,0), colspan=4, rowspan=2) sectionax = plt.subplot2grid((2,5), (0,4), colspan=1,rowspan=2) # first plot the section data... sectiondata = np.loadtxt("data/shape-%s/membrane_files/%s%s%ssections-%s-%s-%s-%s-%s-%s.dat" % (load.f_shape,load.debug_str,load.hires_str,load.slice_str,load.f_shape, load.f_param1,load.f_param2,load.f_param3,load.f_param4,load.f_param5)) def plot_sections(sectionax, sectiondata): dx = load.dx x = np.arange(sectiondata.shape[1]*1.0)*dx y = np.arange(sectiondata.shape[0]*1.0)*dx X,Y = np.meshgrid(x,y) inmembrane = np.zeros_like(sectiondata) inmembrane[sectiondata>0] = 1.0 xmax = X[sectiondata>0].max() xmin = X[sectiondata>0].min() ymax = Y[sectiondata>0].max() ymin = Y[sectiondata>0].min() ymean = (Y*inmembrane).sum()/inmembrane.sum() xmean = (X*inmembrane).sum()/inmembrane.sum() yweighted = (Y*sectiondata).sum()/sectiondata.sum() xweighted = (X*sectiondata).sum()/sectiondata.sum() levels = [0.5, 1.5, 2.5, 3.5, 4.5] mycolors = ["w","g","r","m","c","y"] for i in xrange(min(4, len(boxList))): if boxList[i] == 'Right': mycolors[1] = colorScale[i] if boxList[i] == 'Mid': mycolors[2] = colorScale[i] if boxList[i] == 'Left': mycolors[3] = colorScale[i] mycolors = colorScale[1:] if load.f_param4 == '97.00': mycolors = ['g','r','m','c'] if load.f_param4 == '96.00': #rightup = 2, rightdown = 1, leftup = 4, leftdown = 3 mycolors = ['g','r','c','m'] #print mycolors # here we rotate so that the order of sections will match the # box plot. xdir, ydir = xweighted - xmean, yweighted - ymean xdir, ydir = xdir/np.sqrt(xdir**2+ydir**2), ydir/np.sqrt(xdir**2+ydir**2) extraxspace = .5 extrayspace = 0 Yrotated = X*xdir + Y*ydir Xrotated = X*ydir - Y*xdir sectionax.contourf(Xrotated, Yrotated, sectiondata, levels=levels, colors=mycolors) xmin = Xrotated[sectiondata>0].min() xmax = Xrotated[sectiondata>0].max() ymin = Yrotated[sectiondata>0].min() ymax = Yrotated[sectiondata>0].max() sectionax.set_xlim(xmin-extraxspace, xmax) sectionax.set_ylim(ymin-extrayspace, ymax) sectionax.set_aspect('equal') sectionax.set_frame_on(False) sectionax.axes.get_xaxis().set_visible(False) sectionax.axes.get_yaxis().set_visible(False) sectionax.add_artist(AnchoredSizeBar( sectionax.transData, 1.00, # length of the bar in the data reference "1$\mu$", # label of the bar #bbox_to_anchor=(0.,0.,1.,1.), loc=8, # 'best', # location (lower right) pad=-(ymax-ymin)/2.0 + 0.4, borderpad=0.25, sep=3, frameon=False )) plot_sections(sectionax, sectiondata) section_names = ['Bottom Section','Center Section','Top Section'] if load.f_param4 == '97.00': section_names = ['Lower Section','Middle Left Section','Middle Right Section','Upper Section'] # section_names = ['rightup','mid','left','rightdown'] if load.f_param4 == '96.00': section_names = ['Lower Left Section','Lower Right Section','Upper Left Section','Upper Right Section'] # section_names = ['rightdown','rightup','leftdown','leftup'] font=FontProperties() font.set_family('serif') text_adjust = -.2*box_time_step*(end-start) j=0 k=0 for i in range(len(plotCurveList_D[:,0])): if i%(numProteinTypes_D)==0: j+=1 k=0 if i==0: bax.plot(timeAxis[start:end], plotCurveList_D[i, start:end], color=colorScale[j],alpha=alphaScale_D[k]) y_text_label = i*.8/len(plotCurveList_D[:,0]) + .1*np.floor(i/numProteinTypes_D) if load.f_param4 == '97.00' or load.f_param4 == '96.00': y_text_label = i*.8/len(plotCurveList_D[:,0]) + .07*np.floor(i/numProteinTypes_D) y_label = (plotCurveList_D[i, start+int(1/box_time_step)])/2.0 bax.annotate('%s'%plotProteinLabels[i],xy=(1,y_label),xytext=(text_adjust,y_text_label), fontsize=7, fontproperties=font, arrowprops=dict(facecolor='black',shrink=0.05, width=.3, headwidth=5.)) bax.fill_between(timeAxis[start:end], [0 for x in range(len(timeAxis))[start:end]], plotCurveList_D[i, start:end], alpha=alphaScale_D[k],facecolor=colorScale[j]) elif i!=0: bax.plot(timeAxis[start:end], plotCurveList_D[i,start:end], color=colorScale[j],alpha=alphaScale_D[k]) y_text_label = i*.8/len(plotCurveList_D[:,0]) + .1*np.floor(i/numProteinTypes_D) y_label = (plotCurveList_D[i, start+int(1/box_time_step)] + plotCurveList_D[i-1, start+int(1/box_time_step)])/2.0 if load.f_param4 == '97.00' or load.f_param4 == '96.00': y_text_label = i*.8/len(plotCurveList_D[:,0]) + .07*np.floor(i/numProteinTypes_D) bax.annotate('%s'%plotProteinLabels[i%numProteinTypes_D],xy=(1,y_label),xytext=(text_adjust,y_text_label), fontsize=7, fontproperties=font, arrowprops=dict(facecolor='black',shrink=0.05, width=.3, headwidth=5.)) if (i+1)%(numProteinTypes_D)==0: bax.text(-0.2,y_text_label+.04,section_names[int(np.floor(i/numProteinTypes_D))],transform=bax.transAxes,fontsize=9,fontproperties=font,) bax.fill_between(timeAxis[start:end], plotCurveList_D[i-1, start:end], plotCurveList_D[i, start:end], alpha=alphaScale_D[k],facecolor=colorScale[j]) k+=1 bax.set_xlim(timeAxis[start],timeAxis[end-1]) bax.get_yaxis().set_visible(False) bax.set_ylim(0, 1) bax.set_title("MinD protein counts over time") bax.set_xlabel("Time (s)") rax = bax.twinx() rax.set_ylabel('Fraction of proteins in each stage and section',labelpad=-15) rax.yaxis.set_ticklabels([0,"","","","",1.0]) #bax.set_ylabel("Fraction of proteins") # 'A', xy=(Az, Ax), xytext=(1.2,-3.5), # path_effects=texteff, # arrowprops=dict(shrink=0.01, width=1, # headwidth=hw, path_effects=arroweff)) #bax.legend(plotNameList_D,bbox_to_anchor=(0.3,-0.05,1.0,1.0),loc=4,prop={'size':8}).draw_frame(False) plt.savefig(load.print_string("box-plot_D","")) plt.figure() #f, (bax,sectionax) = plt.subplots(1, 2) bax = plt.subplot2grid((2,5), (0,0), colspan=4, rowspan=2) sectionax = plt.subplot2grid((2,5), (0,4), colspan=1,rowspan=2) # First plot the section data... plot_sections(sectionax, sectiondata) j=0 k=0 for i in range(len(plotCurveList_E)): if i%(numProteinTypes_E)==0: j+=1 k=0 if i==0: bax.plot(timeAxis[start:end],plotCurveList_E[i][start:end],color=colorScale[j],alpha=alphaScale_E[k]) bax.fill_between(timeAxis[start:end],[0 for x in range(len(timeAxis))[start:end]],plotCurveList_E[i][start:end],alpha=alphaScale_E[k],facecolor=colorScale[j]) elif i!=0: bax.plot(timeAxis[start:end],plotCurveList_E[i][start:end],color=colorScale[j],alpha=alphaScale_E[k]) bax.fill_between(timeAxis[start:end],plotCurveList_E[i-1][start:end],plotCurveList_E[i][start:end],alpha=alphaScale_E[k],facecolor=colorScale[j]) #print "i is ",i," || k is", k," || j is",j k+=1 bax.set_xlim(timeAxis[start],timeAxis[end-1]) bax.set_ylim(0, 1) bax.set_title("MinE protein counts over time") bax.set_xlabel("Time (s)") bax.set_ylabel("Fraction of proteins") bax.legend(plotNameList_E,bbox_to_anchor=(0.3,-0.05,1.0,1.0),loc="lower right",prop={'size':8}).draw_frame(False) plt.savefig(load.print_string("box-plot_E","")) plt.show() return 0
ax.set_ylabel('Damped oscillation [V]', labelpad=19) plt.show() fig, ax = plt.subplots(figsize=(5, 3)) fig.subplots_adjust(bottom=0.15, left=0.2) ax.plot(x, y) ax.set_xlabel('time [s]', position=(0., 1e6), horizontalalignment='left') ax.set_ylabel('Damped oscillation [V]', position=(1e6, 1), horizontalalignment='right') plt.show() font = FontProperties() font.set_family('serif') font.set_name('Times New Roman') font.set_style('italic') fig, ax = plt.subplots(figsize=(5, 3)) fig.subplots_adjust(bottom=0.15, left=0.2) ax.plot(x, y) ax.set_xlabel('time [s]', fontsize='large', fontweight='bold') ax.set_ylabel('Damped oscillation [V]', fontproperties=font) plt.show() fig, ax = plt.subplots(figsize=(5, 3)) fig.subplots_adjust(bottom=0.2, left=0.2) ax.plot(x, np.cumsum(y**2)) ax.set_xlabel('time [s] \n This was a long experiment')