def tick_labels(self, labels): self._ui_tick_labels = labels if labels != 'auto': set_tick_labels = [] for label in labels: txt = _utl.raw_string(label) set_tick_labels.append('$\sf{' + txt + '}$') self.mpl_bar.set_ticklabels(set_tick_labels)
def gen_label(self, str_list, font_size = _mpl.rcParams['font.size']): """ Generates a color bar label with or without units in parenthesis Parameters ---------- str_list: list of str Label text and units in LaTeX syntax. font_size: float, optional font size (in points) """ self.str_list = str_list #Initialize the storage list self.anno = [] #Get rid of escape characters (ex: \n) for i in range(len(str_list)): str_list[i] = _utl.raw_string(str_list[i]) #Create the label #(Label position will be wrong to begin with. Once we know the size of #the label, we will move it into the proper position.) x = _np.array([0.0,0.0]) #Generate text and add object to the list #(I originally attempted to specify the font via the keyword #"fontname =", but that does not work when using mathtext. You must #specify the font using the "mpl.rcParams['mathtext.XXX'] = " command.) self.anno.append(self.parent_bar.annotate('$' + str_list[0] + '$', \ x, 'axes mm', verticalalignment = 'baseline', fontsize = font_size)) #If units have been specified: if len(str_list) > 1: #Get size of label so we know where to put units var_bbox = self.parent_bar.get_obj_bbox(self.anno[0]) #Define units spacing from variable text x[0] = var_bbox.x1 + 3.1 * font_size/18.0 #Generate units and add object to the list #(I originally attempted to specify the font via the keyword #"fontname =", but that does not work when using mathtext. You #must specify the font using the #"mpl.rcParams['mathtext.XXX'] = " command.) self.anno.append(self.parent_bar.annotate(\ '$\mathsf{(' + str_list[1] + ')}$', x, 'axes mm', \ verticalalignment = 'baseline', fontsize = font_size))
def gen_label(self, str_list, font_size = _mpl.rcParams['font.size']): """ Generates an axis label with or without units in parenthesis Parameters ---------- str_list: list of str Label text and units in LaTeX syntax. font_size: float, optional font size (in points) """ #Save the input string self.str_list = str_list #Initialize the storage list #(The first item in the list should be the bottom left-most annotation) self.anno = [] #Calculate font size in mm units font_size_mm = font_size * 25.4/72.0 #Get rid of escape characters (ex: \n) for i in range(len(str_list)): str_list[i] = _utl.raw_string(str_list[i]) # var_text = _utl.raw_string(var_text) # if units is not None: # units = _utl.raw_string(units) #Create the label #(Label position will be wrong to begin with. Once we know the size of #the label, we will move it into the proper position.) x = _np.array([0.0, 0.0]) if self.orient == 0: #Horizontal Label #Define axis label text spacing from arrowhead and units spacing from #variable text txt_spacing = _np.array([4.0 * font_size/18.0, 3.1 * font_size/18.0]) if self.arrow_bool: #Print arrow so that head lines up with the first tick mark self.arrow = self.gen_arrow(x, ['axes mm', 'axes mm'], \ length = self.parent_ax.sdim) #Specify the x coordinate offset to the left edge of the text. x[0] = txt_spacing[0] else: self.arrow = None x[0] = 0.0 #Specify the y coordinate of the bottom edge of the text. We need #a y offset to get baseline aligned text to align with the #arrowhead. (We use baseline aligned text instead of center #aligned so that the units match up with the text.) x[1] = - font_size_mm * 0.28 #Generate text and add object to the list #(I originally attempted to specify the font via the keyword #"fontname =", but that does not work when using mathtext. You must #specify the font using the "mpl.rcParams['mathtext.XXX'] = " command.) self.anno.append(self.parent_ax.annotate('$' + str_list[0] + '$', x, \ 'axes mm', verticalalignment = 'baseline', fontsize = font_size)) #If units have been specified: if len(str_list) > 1: #Get size of label so we know where to put units label_bbox = self.parent_ax.get_obj_bbox(self.anno[0]) #Generate units and add object to the list #(I originally attempted to specify the font via the keyword #"fontname =", but that does not work when using mathtext. You #must specify the font using the #"mpl.rcParams['mathtext.XXX'] = " command.) x[0] = label_bbox.x1 + txt_spacing[1] self.anno.append(self.parent_ax.annotate(\ '$\mathsf{(' + str_list[1] + ')}$', x, 'axes mm', \ verticalalignment = 'baseline', fontsize = font_size)) else: #Vertical Label #Define axis label text spacing from arrowhead and units spacing from #variable text txt_spacing = _np.array([3.3 * font_size/18.0, 2.2 * font_size/18.0]) if self.arrow_bool: #Print arrow at first tick mark self.arrow = self.gen_arrow(x, ['axes mm', 'axes mm'], \ length = self.parent_ax.sdim) #Specify the y coordinate offset to the bottom edge of the text. x[1] = txt_spacing[0] else: self.arrow = None x[1] = 0.0 #If units have been specified: if len(str_list) > 1: #Create units and add object to the list #(I originally attempted to specify the font via the keyword #"fontname =", but that does not work when using mathtext. You #must specify the font using the "mpl.rcParams['mathtext.XXX'] = " #command.) self.anno.append(self.parent_ax.annotate(\ '$\mathsf{(' + str_list[1] + ')}$', x, 'axes mm', \ fontsize = font_size, verticalalignment = 'bottom', \ horizontalalignment = 'center')) #Get size of units so we know where to put the label units_bbox = self.parent_ax.get_obj_bbox(self.anno[0]) #Specify the y coordinate offset to the bottom edge of the text. x[1] = units_bbox.y1 + txt_spacing[1] #Generate text and add object to the list #(I originally attempted to specify the font via the keyword #"fontname =", but that does not work when using mathtext. You must #specify the font using the "mpl.rcParams['mathtext.XXX'] = " command.) self.anno.append(self.parent_ax.annotate('$' + str_list[0] + '$', x, 'axes mm', fontsize = font_size, verticalalignment = 'bottom', \ horizontalalignment = 'center'))