def draw(self): dpi = self.prefs['dpi'] ax_xsize = self.ax.get_window_extent().width ax_ysize = self.ax.get_window_extent().height nLabels = len(self.labels) nColumns = min(self.prefs['legend_max_columns'], int(ax_xsize / self.column_width)) maxRows = self.prefs['legend_max_rows'] nRows_ax = int(ax_ysize / 1.6 / self.prefs['text_size']) nRows_label = nLabels / nColumns + (nLabels % nColumns != 0) nRows = max(1, min(min(nRows_label, maxRows), nRows_ax)) maxLabels = nColumns * nRows - 1 self.ax.set_xlim(0., float(ax_xsize)) self.ax.set_ylim(-float(ax_ysize), 0.) legend_text_size, legend_text_padding = self.__get_legend_text_size() legend_text_size_point = pixelToPoint(legend_text_size, dpi) box_width = legend_text_size legend_offset = (ax_xsize - nColumns * self.column_width) / 2 nc = 0 # self.labels.reverse() for label, num in self.labels: num_flag = self.prefs.get('legend_numbers', True) percent_flag = self.prefs.get('legend_unit', '') if num_flag: if percent_flag == "%": num = "%.1f" % num + '%' else: num = "%.1f" % num else: num = None color = self.palette.getColor(label) row = nc % nRows column = nc / nRows if row == nRows - 1 and column == nColumns - 1 and nc != nLabels - 1: last_text = '... plus %d more' % (nLabels - nc) self.ax.text(float(column * self.column_width) + legend_offset, -float(row * 1.6 * box_width), last_text, horizontalalignment='left', verticalalignment='top', size=legend_text_size_point) break else: self.ax.text(float(column * self.column_width) + 2. * box_width + legend_offset, -row * 1.6 * box_width, str(label), horizontalalignment='left', verticalalignment='top', size=legend_text_size_point) if num is not None: self.ax.text(float((column + 1) * self.column_width) - 2 * box_width + legend_offset, -float(row * 1.6 * box_width), str(num), horizontalalignment='right', verticalalignment='top', size=legend_text_size_point) box = Rectangle((float(column * self.column_width) + legend_offset, -float(row * 1.6 * box_width) - box_width), box_width, box_width) box.set_ec('black') box.set_linewidth(pixelToPoint(0.5, dpi)) box.set_fc(color) self.ax.add_patch(box) nc += 1
def draw(self): dpi = self.prefs['dpi'] ax_xsize = self.ax.get_window_extent().width ax_ysize = self.ax.get_window_extent().height nLabels = len(self.labels) nColumns = min(self.prefs['legend_max_columns'],int(ax_xsize/self.column_width)) maxRows = self.prefs['legend_max_rows'] nRows_ax = int(ax_ysize/1.6/self.prefs['text_size']) nRows_label = nLabels/nColumns + (nLabels%nColumns != 0) nRows = max(1,min(min(nRows_label,maxRows),nRows_ax )) maxLabels = nColumns*nRows - 1 self.ax.set_xlim(0.,float(ax_xsize)) self.ax.set_ylim(-float(ax_ysize),0.) legend_text_size,legend_text_padding = self.__get_legend_text_size() legend_text_size_point = pixelToPoint(legend_text_size,dpi) box_width = legend_text_size legend_offset = (ax_xsize - nColumns*self.column_width)/2 nc = 0 #self.labels.reverse() for label,num in self.labels: num_flag = self.prefs.get('legend_numbers',True) percent_flag = self.prefs.get('legend_unit','') if num_flag: if percent_flag == "%": num = "%.1f" % num +'%' else: num = "%.1f" % num else: num = None color = self.palette.getColor(label) row = nc%nRows column = nc/nRows if row == nRows-1 and column == nColumns-1 and nc != nLabels-1: last_text = '... plus %d more' % (nLabels-nc) self.ax.text(float(column*self.column_width)+legend_offset,-float(row*1.6*box_width), last_text,horizontalalignment='left', verticalalignment='top',size=legend_text_size_point) break else: self.ax.text(float(column*self.column_width)+2.*box_width+legend_offset,-row*1.6*box_width, str(label),horizontalalignment='left', verticalalignment='top',size=legend_text_size_point) if num is not None: self.ax.text(float((column+1)*self.column_width)-2*box_width+legend_offset,-float(row*1.6*box_width), str(num),horizontalalignment='right', verticalalignment='top',size=legend_text_size_point) box = Rectangle((float(column*self.column_width)+legend_offset,-float(row*1.6*box_width)-box_width), box_width,box_width) box.set_ec('black') box.set_linewidth(pixelToPoint(0.5,dpi)) box.set_fc(color) self.ax.add_patch(box) nc += 1
class CellObject: def __init__(self, row, col, value, fig, axes): self.row = row self.col = col self.value = value self.fig, self.axes = fig, axes self.start() def start(self): if self.value == 0: self.ec = (0,0,0,0.1) self.text_color = (0,0,0,0) else: self.ec = (0,0,0,1) self.text_color = (1,1,1,1) self.fc = color[self.value] self.center = [self.col, self.row] self.rect = Rectangle([self.col-0.5, self.row-0.5], \ width = 1, height = 1, fc = self.fc, ec = self.ec, \ linewidth = 2) self.text = self.axes.text(self.col, self.row, str(self.value), \ color = self.text_color, \ ha = 'center', va = 'center', \ fontweight = 'bold') self.axes.add_patch(self.rect) self.fig.canvas.draw() def update(self, draw = False): if self.value == 0: self.ec = (0,0,0,0.1) self.text_color = (0,0,0,0) else: self.ec = (0,0,0,1) self.text_color = (1,1,1,1) self.fc = color[min(self.value, 2048)] self.rect.set_fc(self.fc) self.rect.set_ec(self.ec) self.text.set_color(self.text_color) self.text.set_text(str(self.value)) if draw: self.fig.canvas.draw()