我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用matplotlib.pylab.text()。
def text(x,y,s,**args): """ Version of pylab.text that can be applied to arrays. Usage: >>> text(x,y,s, fontsize=10) will plot the strings in array 's' at coordinates given by arrays 'x' and 'y'. """ for j in range(x.size): pylab.text(x[j],y[j],s[j], **args)
def axis(self, imin, imax, jmin, jmax, xlabels=[], ylabels=[]): imid=(imin+imax)/2 # midpoint of X-axis jmid=(jmin+jmax)/2 # midpoint of Y-axis # Create axis lines self.create_line((imin, jmax, imax, jmax)) self.create_line((imin, jmin, imin, jmax)) self.create_line((imin, jmin, imax, jmin)) self.create_line((imax, jmin, imax, jmax)) self.create_line((imid, jmin, imid, jmax)) self.create_line((imin, jmid, imax, jmid)) # Create tick marks and labels tic = imin for label in xlabels: self.create_line((tic, jmax+ 5, tic, jmax)) self.create_text( tic, jmax+10, text=label) if len(xlabels)!=1: tic+=(imax-imin)/(len(xlabels)-1) tic = jmax for label in ylabels: self.create_line((imin , tic, imin-5, tic)) self.create_text( imin-20, tic, text=label) if len(ylabels)!=1: tic-=(jmax-jmin)/(len(ylabels)-1)
def plot_representations(X, y, title): """Plot distributions and thier labels.""" x_min, x_max = np.min(X, 0), np.max(X, 0) X = (X - x_min) / (x_max - x_min) f = plt.figure(figsize=(15, 10.8), dpi=300) # ax = plt.subplot(111) for i in range(X.shape[0]): plt.text(X[i, 0], X[i, 1], str(y[i]), color=plt.cm.Set1(y[i] / 10.), fontdict={'weight': 'bold', 'size': 9}) # if hasattr(offsetbox, 'AnnotationBbox'): # # only print thumbnails with matplotlib > 1.0 # shown_images = np.array([[1., 1.]]) # just something big # for i in range(digits.data.shape[0]): # dist = np.sum((X[i] - shown_images) ** 2, 1) # if np.min(dist) < 4e-3: # # don't show points that are too close # continue # shown_images = np.r_[shown_images, [X[i]]] # imagebox = offsetbox.AnnotationBbox( # offsetbox.OffsetImage(digits.images[i], cmap=plt.cm.gray_r), # X[i]) # ax.add_artist(imagebox) plt.xticks([]), plt.yticks([]) if title is not None: plt.title(title) return f
def plotBoundVsK(KVals=np.arange(1,50), alpha=0.5, gamma=10, labels=None, betaFunc='prior'): if labels is None: txtlabel = str(alpha) labels = [None, None] else: txtlabel = 'alpha\n' + str(alpha) exactVals = np.zeros(len(KVals)) boundVals = np.zeros(len(KVals)) for ii, K in enumerate(KVals): betaVec = 1.0/(1.0 + gamma) * np.ones(K+1) for k in range(1, K): betaVec[k] = betaVec[k] * (1 - np.sum(betaVec[:k])) betaVec[-1] = 1 - np.sum(betaVec[:-1]) print betaVec assert np.allclose(betaVec.sum(), 1.0) exactVals[ii] = cDir_exact(alpha, betaVec) boundVals[ii] = cDir_surrogate(alpha, betaVec) assert np.all(exactVals >= boundVals) pylab.plot(KVals, exactVals, 'k-', linewidth=LINEWIDTH, label=labels[0]) pylab.plot(KVals, boundVals, 'r--', linewidth=LINEWIDTH, label=labels[1]) index = -1 pylab.text(KVals[index]+.25, boundVals[index], txtlabel, fontsize=LEGENDSIZE-8) pylab.xlim([0, np.max(KVals)+7.5]) pylab.gca().set_xticks([0, 10, 20, 30, 40, 50]) pylab.xlabel("K", fontsize=FONTSIZE) pylab.ylabel("cDir function", fontsize=FONTSIZE) pylab.tick_params(axis='both', which='major', labelsize=TICKSIZE)
def save_image(nifti, anat, cluster_dict, out_path, f, image_threshold=2, texcol=1, bgcol=0, iscale=2, text=None, **kwargs): '''Saves a single nifti image. Args: nifti (str or nipy.core.api.image.image.Image): nifti file to visualize. anat (nipy.core.api.image.image.Image): anatomical nifti file. cluster_dict (dict): dictionary of clusters. f (int): index. image_threshold (float): treshold for `plot_map`. texcol (float): text color. bgcol (float): background color. iscale (float): image scale. text (Optional[str]): text for figure. **kwargs: extra keyword arguments ''' if isinstance(nifti, str): nifti = load_image(nifti) feature = nifti.get_data() elif isinstance(nifti, nipy.core.image.image.Image): feature = nifti.get_data() font = {'size': 8} rc('font', **font) coords = cluster_dict['top_clust']['coords'] if coords == None: return feature /= feature.std() imax = np.max(np.absolute(feature)) imin = -imax imshow_args = dict( vmax=imax, vmin=imin, alpha=0.7 ) coords = ([-coords[0], -coords[1], coords[2]]) plt.axis('off') plt.text(0.05, 0.8, text, horizontalalignment='center', color=(texcol, texcol, texcol)) try: plot_map(feature, xyz_affine(nifti), anat=anat.get_data(), anat_affine=xyz_affine(anat), threshold=image_threshold, cut_coords=coords, annotate=False, cmap=cmap, draw_cross=False, **imshow_args) except Exception as e: return plt.savefig(out_path, transparent=True, facecolor=(bgcol, bgcol, bgcol))