def plot_profile(self): if self.y_min is None: self.y_min = [None] if self.y_max is None: self.y_max = [None] if not self.color_list: cmap_plot = plt.get_cmap('jet') if self.numlines > 1: # kmeans, so we need to color by cluster self.color_list = cmap_plot( np.arange(self.numlines, dtype=float) / float(self.numlines)) else: self.color_list = cmap_plot( np.arange(self.numplots, dtype=float) / float(self.numplots)) if (self.numlines > 1 and len(self.color_list) < self.numlines) or\ (self.numlines == 1 and len(self.color_list) < self.numplots): sys.exit("\nThe given list of colors is too small, " "at least {} colors are needed\n".format(self.numlines)) for color in self.color_list: if not pltcolors.is_color_like(color): sys.exit("\nThe color name {} is not valid. Check " "the name or try with a html hex string " "for example #eeff22".format(color)) if self.image_format == "plotly": return self.plotly_profile() first = True ax_list = [] for plot in range(self.numplots): localYMin = None localYMax = None col = plot % self.plots_per_row row = int(plot / float(self.plots_per_row)) if (row == 0 and col == 0) or len(self.y_min) > 1 or len(self.y_max) > 1: ax = self.fig.add_subplot(self.grids[row, col]) else: ax = self.fig.add_subplot(self.grids[row, col], sharey=ax_list[0]) if self.per_group: title = self.hm.matrix.group_labels[plot] if row != 0 and len(self.y_min) == 1 and len(self.y_max) == 1: plt.setp(ax.get_yticklabels(), visible=False) tickIdx = plot % self.hm.matrix.get_num_samples() else: title = self.hm.matrix.sample_labels[plot] if col != 0 and len(self.y_min) == 1 and len(self.y_max) == 1: plt.setp(ax.get_yticklabels(), visible=False) tickIdx = plot ax.set_title(title) for data_idx in range(self.numlines): if self.per_group: _row, _col = plot, data_idx else: _row, _col = data_idx, plot if localYMin is None or self.y_min[_col % len( self.y_min)] < localYMin: localYMin = self.y_min[_col % len(self.y_min)] if localYMax is None or self.y_max[_col % len( self.y_max)] > localYMax: localYMax = self.y_max[_col % len(self.y_max)] sub_matrix = self.hm.matrix.get_matrix(_row, _col) if self.per_group: label = sub_matrix['sample'] else: label = sub_matrix['group'] if self.numlines > 1: coloridx = data_idx else: coloridx = plot plot_single(ax, sub_matrix['matrix'], self.averagetype, self.color_list[coloridx], label, plot_type=self.plot_type) # remove the numbers of the y axis for all plots plt.setp(ax.get_yticklabels(), visible=False) if col == 0 or len(self.y_min) > 1 or len(self.y_max) > 1: # add the y axis label for the first plot # on each row and make the numbers and ticks visible plt.setp(ax.get_yticklabels(), visible=True) ax.axes.set_ylabel(self.y_axis_label) """ # reduce the number of yticks by half num_ticks = len(ax.get_yticks()) yticks = [ax.get_yticks()[i] for i in range(1, num_ticks, 2)] ax.set_yticks(yticks) """ totalWidth = sub_matrix['matrix'].shape[1] xticks, xtickslabel = self.getTicks(tickIdx) if np.ceil(max(xticks)) != float(totalWidth): tickscale = float(totalWidth) / max(xticks) xticks_use = [x * tickscale for x in xticks] ax.axes.set_xticks(xticks_use) else: ax.axes.set_xticks(xticks) ax.axes.set_xticklabels(xtickslabel, rotation=self.label_rotation) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax.xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') if first and self.plot_type not in ['heatmap', 'overlapped_lines']: ax.legend(loc=self.legend_location.replace('-', ' '), ncol=1, prop=self.font_p, frameon=False, markerscale=0.5) if len(self.y_min) == 1 and len(self.y_max) == 1: first = False """ ax.legend(bbox_to_anchor=(-0.05, -1.13, 1., 1), loc='upper center', ncol=1, mode="expand", prop=font_p, frameon=False, markerscale=0.5) """ lims = ax.get_ylim() if localYMin is not None: lims = (localYMin, lims[1]) if localYMax is not None: lims = (lims[0], localYMax) if lims[0] >= lims[1]: lims = (lims[0], lims[0] + 1) ax.set_ylim(lims) ax_list.append(ax) plt.subplots_adjust(wspace=0.05, hspace=0.3) plt.tight_layout() plt.savefig(self.out_file_name, dpi=self.dpi, format=self.image_format) plt.close()
def addProfilePlot(hm, plt, fig, grids, iterNum, iterNum2, perGroup, averageType, yAxisLabel, color_list, yMin, yMax, wspace, hspace, colorbar_position, label_rotation=0.0): """ A function to add profile plots to the given figure, possibly in a custom grid subplot which mimics a tight layout (if wspace and hspace are not None) """ if wspace is not None and hspace is not None: if colorbar_position == 'side': gridsSub = gridspec.GridSpecFromSubplotSpec( 1, iterNum, subplot_spec=grids[0, :-1], wspace=wspace, hspace=hspace) else: gridsSub = gridspec.GridSpecFromSubplotSpec( 1, iterNum, subplot_spec=grids[0, :], wspace=wspace, hspace=hspace) ax_list = [] for sample_id in range(iterNum): if perGroup: title = hm.matrix.group_labels[sample_id] else: title = hm.matrix.sample_labels[sample_id] if sample_id > 0 and len(yMin) == 1 and len(yMax) == 1: ax_profile = fig.add_subplot(grids[0, sample_id], sharey=ax_list[0]) else: if wspace is not None and hspace is not None: ax_profile = fig.add_subplot(gridsSub[0, sample_id]) else: ax_profile = fig.add_subplot(grids[0, sample_id]) ax_profile.set_title(title) for group in range(iterNum2): if perGroup: sub_matrix = hm.matrix.get_matrix(sample_id, group) line_label = sub_matrix['sample'] else: sub_matrix = hm.matrix.get_matrix(group, sample_id) line_label = sub_matrix['group'] plot_single(ax_profile, sub_matrix['matrix'], averageType, color_list[group], line_label, plot_type='simple') if sample_id > 0 and len(yMin) == 1 and len(yMax) == 1: plt.setp(ax_profile.get_yticklabels(), visible=False) if sample_id == 0 and yAxisLabel != '': ax_profile.set_ylabel(yAxisLabel) xticks, xtickslabel = hm.getTicks(sample_id) if np.ceil(max(xticks)) != float(sub_matrix['matrix'].shape[1]): tickscale = float(sub_matrix['matrix'].shape[1]) / max(xticks) xticks_use = [x * tickscale for x in xticks] ax_profile.axes.set_xticks(xticks_use) else: ax_profile.axes.set_xticks(xticks) ax_profile.axes.set_xticklabels(xtickslabel, rotation=label_rotation) ax_list.append(ax_profile) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax_profile.xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') # It turns out that set_ylim only takes np.float64s localYMin = yMin[sample_id % len(yMin)] localYMax = yMax[sample_id % len(yMax)] lims = ax_list[0].get_ylim() if localYMin: lims = (np.float64(localYMin), lims[1]) if localYMax: lims = (lims[0], np.float64(localYMax)) if lims[0] >= lims[1]: lims = (lims[0], lims[0] + 1) if len(yMin) == 1 and len(yMax) == 1: ax_list[0].set_ylim(lims) else: ax_list[-1].set_ylim(lims) return ax_list
def plot_profile(self): if self.y_min is None: self.y_min = [None] if self.y_max is None: self.y_max = [None] if not self.color_list: cmap_plot = plt.get_cmap('jet') if self.numlines > 1: # kmeans, so we need to color by cluster self.color_list = cmap_plot( np.arange(self.numlines, dtype=float) / float(self.numlines)) else: self.color_list = cmap_plot( np.arange(self.numplots, dtype=float) / float(self.numplots)) if (self.numlines > 1 and len(self.color_list) < self.numlines) or\ (self.numlines == 1 and len(self.color_list) < self.numplots): sys.exit("\nThe given list of colors is too small, " "at least {} colors are needed\n".format(self.numlines)) for color in self.color_list: if not pltcolors.is_color_like(color): sys.exit("\nThe color name {} is not valid. Check " "the name or try with a html hex string " "for example #eeff22".format(color)) if self.image_format == "plotly": return self.plotly_profile() first = True ax_list = [] globalYmin = np.inf globalYmax = -np.inf for plot in range(self.numplots): localYMin = None localYMax = None col = plot % self.plots_per_row row = int(plot / float(self.plots_per_row)) if (row == 0 and col == 0) or len(self.y_min) > 1 or len(self.y_max) > 1: ax = self.fig.add_subplot(self.grids[row, col]) else: ax = self.fig.add_subplot(self.grids[row, col]) if self.per_group: title = self.hm.matrix.group_labels[plot] if row != 0 and len(self.y_min) == 1 and len(self.y_max) == 1: plt.setp(ax.get_yticklabels(), visible=False) tickIdx = plot % self.hm.matrix.get_num_samples() else: title = self.hm.matrix.sample_labels[plot] if col != 0 and len(self.y_min) == 1 and len(self.y_max) == 1: plt.setp(ax.get_yticklabels(), visible=False) tickIdx = plot ax.set_title(title) for data_idx in range(self.numlines): if self.per_group: _row, _col = plot, data_idx else: _row, _col = data_idx, plot if localYMin is None or self.y_min[col % len( self.y_min)] < localYMin: localYMin = self.y_min[col % len(self.y_min)] if localYMax is None or self.y_max[col % len( self.y_max)] > localYMax: localYMax = self.y_max[col % len(self.y_max)] sub_matrix = self.hm.matrix.get_matrix(_row, _col) if self.per_group: label = sub_matrix['sample'] else: label = sub_matrix['group'] if self.numlines > 1: coloridx = data_idx else: coloridx = plot plot_single(ax, sub_matrix['matrix'], self.averagetype, self.color_list[coloridx], label, plot_type=self.plot_type) globalYmin = min(np.float64(globalYmin), ax.get_ylim()[0]) globalYmax = max(globalYmax, ax.get_ylim()[1]) # Exclude ticks from all but one subplot by default if col > 0 and len(self.y_min) == 1 and len(self.y_max) == 1: plt.setp(ax.get_yticklabels(), visible=False) totalWidth = sub_matrix['matrix'].shape[1] xticks, xtickslabel = self.getTicks(tickIdx) if np.ceil(max(xticks)) != float(totalWidth - 1): tickscale = float(totalWidth) / max(xticks) xticks_use = [x * tickscale for x in xticks] ax.axes.set_xticks(xticks_use) else: ax.axes.set_xticks(xticks) ax.axes.set_xticklabels(xtickslabel, rotation=self.label_rotation) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax.xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') if first and self.y_axis_label != '': ax.set_ylabel(self.y_axis_label) if first and self.plot_type not in ['heatmap', 'overlapped_lines']: ax.legend(loc=self.legend_location.replace('-', ' '), ncol=1, prop=self.font_p, frameon=False, markerscale=0.5) if len(self.y_min) == 1 and len(self.y_max) == 1: first = False ax_list.append(ax) # It turns out that set_ylim only takes np.float64s for sample_id, subplot in enumerate(ax_list): localYMin = self.y_min[sample_id % len(self.y_min)] localYMax = self.y_max[sample_id % len(self.y_max)] lims = [globalYmin, globalYmax] if localYMin is not None: if localYMax is not None: lims = (np.float64(localYMin), np.float64(localYMax)) else: lims = (np.float64(localYMin), lims[1]) elif localYMax is not None: lims = (lims[0], np.float64(localYMax)) if lims[0] >= lims[1]: lims = (lims[0], lims[0] + 1) ax_list[sample_id].set_ylim(lims) plt.subplots_adjust(wspace=0.05, hspace=0.3) plt.tight_layout() plt.savefig(self.out_file_name, dpi=self.dpi, format=self.image_format) plt.close()
def plotMatrix(hm, outFileName, colorMapDict={'colorMap': 'binary', 'missingDataColor': 'black'}, plotTitle='', xAxisLabel='', yAxisLabel='', regionsLabel='', zMin=None, zMax=None, yMin=None, yMax=None, averageType='median', reference_point_label='TSS', startLabel='TSS', endLabel="TES", heatmapHeight=25, heatmapWidth=7.5, perGroup=False, whatToShow='plot, heatmap and colorbar', plotType='simple', image_format=None, legend_location='upper-left'): matrix_flatten = None if zMin is None: matrix_flatten = hm.matrix.flatten() # try to avoid outliers by using np.percentile zMin = np.percentile(matrix_flatten, 1.0) if np.isnan(zMin): zMin = None if zMax is None: if matrix_flatten is None: matrix_flatten = hm.matrix.flatten() # try to avoid outliers by using np.percentile zMax = np.percentile(matrix_flatten, 98.0) if np.isnan(zMax): zMax = None plt.rcParams['font.size'] = 8.0 fontP = FontProperties() # fontP.set_size('small') showSummaryPlot = False showColorbar = False if whatToShow == 'plot and heatmap': showSummaryPlot = True elif whatToShow == 'heatmap and colorbar': showColorbar = True elif whatToShow == 'plot, heatmap and colorbar': showSummaryPlot = True showColorbar = True grids = prepare_layout(hm.matrix, (heatmapWidth, heatmapHeight), showSummaryPlot, showColorbar, perGroup) # figsize: w,h tuple in inches figwidth = heatmapWidth / 2.54 figheight = heatmapHeight / 2.54 if showSummaryPlot: # the summary plot ocupies a height # equal to the fig width figheight += figwidth numsamples = hm.matrix.get_num_samples() total_figwidth = figwidth * numsamples if showColorbar: total_figwidth += 1 / 2.54 fig = plt.figure(figsize=(total_figwidth, figheight)) hm.parameters['upstream'] hm.parameters['downstream'] hm.parameters['body'] hm.parameters['bin size'] xticks, xtickslabel = getProfileTicks(hm, reference_point_label, startLabel, endLabel) xticks_heat, xtickslabel_heat = get_heatmap_ticks(hm, reference_point_label, startLabel, endLabel) fig.suptitle(plotTitle, y=1 - (0.06 / figheight)) # colormap for the heatmap if colorMapDict['colorMap']: cmap = plt.get_cmap(colorMapDict['colorMap']) if colorMapDict['colorList'] and len(colorMapDict['colorList']) > 0: cmap = matplotlib.colors.LinearSegmentedColormap.from_list( 'my_cmap', colorMapDict['colorList'], N=colorMapDict['colorNumber']) # color map for the summary plot (profile) on top of the heatmap cmap_plot = plt.get_cmap('jet') numgroups = hm.matrix.get_num_groups() if perGroup: color_list = cmap_plot(np.arange(hm.matrix.get_num_samples()) / hm.matrix.get_num_samples()) else: color_list = cmap_plot(np.arange(numgroups) / numgroups) cmap.set_bad(colorMapDict['missingDataColor']) # nans are printed using this color # check if matrix is reference-point based using the upstream >0 value # and is sorted by region length. If this is # the case, prepare the data to plot a border at the regions end if hm.parameters['upstream'] > 0 and \ hm.matrix.sort_using == 'region_length' and \ hm.matrix.sort_method != 'no': _regions = hm.matrix.get_regions() regions_length_in_bins = [] for _group in _regions: _reg_len = [] for ind_reg in _group: _len = ind_reg['end'] - ind_reg['start'] _reg_len.append((hm.parameters['upstream'] + _len) / hm.parameters['bin size']) # print hm.parameters['upstream'] + (_len / hm.parameters['bin size']) regions_length_in_bins.append(_reg_len) else: regions_length_in_bins = None first_group = 0 # helper variable to place the title per sample/group for sample in range(hm.matrix.get_num_samples()): sample_idx = sample for group in range(numgroups): group_idx = group # add the respective profile to the # summary plot sub_matrix = hm.matrix.get_matrix(group, sample) if showSummaryPlot: if perGroup: sample_idx = sample + 2 # plot + spacer else: group += 2 # plot + spacer first_group = 1 if perGroup: ax = fig.add_subplot(grids[sample_idx, group]) else: ax = fig.add_subplot(grids[group, sample]) if group == first_group and not showSummaryPlot and not perGroup: title = hm.matrix.sample_labels[sample] ax.set_title(title) rows, cols = sub_matrix['matrix'].shape interpolation_type = None if rows >= 1000 and cols >= 200 else 'nearest' img = ax.imshow(sub_matrix['matrix'], aspect='auto', interpolation=interpolation_type, origin='upper', vmin=zMin, vmax=zMax, cmap=cmap, extent=[0, cols, rows, 0]) # plot border at the end of the regions # if ordered by length if regions_length_in_bins is not None: x_lim = ax.get_xlim() y_lim = ax.get_ylim() ax.plot(regions_length_in_bins[group_idx], np.arange(len(regions_length_in_bins[group_idx])), '--', color='black', linewidth=0.5, dashes=(3, 2)) ax.set_xlim(x_lim) ax.set_ylim(y_lim) if perGroup: ax.axes.set_xlabel(sub_matrix['group']) if sample < hm.matrix.get_num_samples() - 1: ax.axes.get_xaxis().set_visible(False) else: ax.axes.get_xaxis().set_visible(False) ax.axes.set_xlabel(xAxisLabel) ax.axes.set_yticks([]) if perGroup and group == 0: ax.axes.set_ylabel(sub_matrix['sample']) elif not perGroup and sample == 0: ax.axes.set_ylabel(sub_matrix['group']) # add xticks to the bottom heatmap (last group) ax.axes.get_xaxis().set_visible(True) ax.axes.set_xticks(xticks_heat) ax.axes.set_xticklabels(xtickslabel_heat, size=8) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax.xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') ax.get_xaxis().set_tick_params( which='both', top='off', direction='out') # plot the profiles on top of the heatmaps if showSummaryPlot: ax_list = [] if perGroup: iterNum = numgroups else: iterNum = hm.matrix.get_num_samples() # plot each of the profiles for sample_id in range(iterNum): if perGroup: title = hm.matrix.group_labels[sample_id] else: title = hm.matrix.sample_labels[sample_id] if sample_id > 0: ax_profile = fig.add_subplot(grids[0, sample_id], sharey=ax_list[0]) else: ax_profile = fig.add_subplot(grids[0, sample_id]) ax_profile.set_title(title) if perGroup: iterNum2 = hm.matrix.get_num_samples() else: iterNum2 = numgroups for group in range(iterNum2): if perGroup: sub_matrix = hm.matrix.get_matrix(sample_id, group) line_label = sub_matrix['sample'] else: sub_matrix = hm.matrix.get_matrix(group, sample_id) line_label = sub_matrix['group'] plot_single(ax_profile, sub_matrix['matrix'], averageType, color_list[group], line_label, plot_type='simple') if sample_id > 0: plt.setp(ax_profile.get_yticklabels(), visible=False) if sample_id == 0 and yAxisLabel != '': ax_profile.set_ylabel(yAxisLabel) ax_profile.axes.set_xticks(xticks) ax_profile.axes.set_xticklabels(xtickslabel) ax_list.append(ax_profile) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax_profile.xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') # reduce the number of yticks by half ax_list[0].set_ylim(yMin, yMax) num_ticks = len(ax_list[0].get_yticks()) yticks = [ax_list[0].get_yticks()[i] for i in range(1, num_ticks, 2)] ax_list[0].set_yticks(yticks) if legend_location != 'none': ax_list[-1].legend(loc=legend_location.replace('-', ' '), ncol=1, prop=fontP, frameon=False, markerscale=0.5) if showColorbar: if showSummaryPlot: # we dont want to colorbar to extend # over the profiles row grid_start = 2 else: grid_start = 0 ax = fig.add_subplot(grids[grid_start:, -1]) fig.colorbar(img, cax=ax) plt.subplots_adjust(wspace=0.10, hspace=0.025, top=0.85, bottom=0, left=0.04, right=0.96) plt.savefig(outFileName, bbox_inches='tight', pdd_inches=0, dpi=200, format=image_format)
def plotMatrix(hm, outFileName, colorMapDict={ 'colorMap': ['binary'], 'missingDataColor': 'black', 'alpha': 1.0 }, plotTitle='', xAxisLabel='', yAxisLabel='', regionsLabel='', zMin=None, zMax=None, yMin=None, yMax=None, averageType='median', reference_point_label='TSS', startLabel='TSS', endLabel="TES", heatmapHeight=25, heatmapWidth=7.5, perGroup=False, whatToShow='plot, heatmap and colorbar', image_format=None, legend_location='upper-left', box_around_heatmaps=True, dpi=200): matrix_flatten = None if zMin is None: matrix_flatten = hm.matrix.flatten() # try to avoid outliers by using np.percentile zMin = np.percentile(matrix_flatten, 1.0) if np.isnan(zMin): zMin = [None] else: zMin = [zMin] # convert to list to support multiple entries if zMax is None: if matrix_flatten is None: matrix_flatten = hm.matrix.flatten() # try to avoid outliers by using np.percentile zMax = np.percentile(matrix_flatten, 98.0) if np.isnan(zMax): zMax = [None] else: zMax = [zMax] plt.rcParams['font.size'] = 8.0 fontP = FontProperties() showSummaryPlot = False showColorbar = False if whatToShow == 'plot and heatmap': showSummaryPlot = True elif whatToShow == 'heatmap and colorbar': showColorbar = True elif whatToShow == 'plot, heatmap and colorbar': showSummaryPlot = True showColorbar = True # colormap for the heatmap if colorMapDict['colorMap']: cmap = [] for color_map in colorMapDict['colorMap']: cmap.append(plt.get_cmap(color_map)) cmap[-1].set_bad(colorMapDict['missingDataColor'] ) # nans are printed using this color if colorMapDict['colorList'] and len(colorMapDict['colorList']) > 0: # make a cmap for each color list given cmap = [] for color_list in colorMapDict['colorList']: cmap.append( matplotlib.colors.LinearSegmentedColormap.from_list( 'my_cmap', color_list.replace(' ', '').split(","), N=colorMapDict['colorNumber'])) cmap[-1].set_bad(colorMapDict['missingDataColor'] ) # nans are printed using this color if len(cmap) > 1 or len(zMin) > 1 or len(zMax) > 1: # position color bar below heatmap when more than one # heatmap color is given colorbar_position = 'below' else: colorbar_position = 'side' grids = prepare_layout(hm.matrix, (heatmapWidth, heatmapHeight), showSummaryPlot, showColorbar, perGroup, colorbar_position) # figsize: w,h tuple in inches figwidth = heatmapWidth / 2.54 figheight = heatmapHeight / 2.54 if showSummaryPlot: # the summary plot ocupies a height # equal to the fig width figheight += figwidth numsamples = hm.matrix.get_num_samples() if perGroup: num_cols = hm.matrix.get_num_groups() else: num_cols = numsamples total_figwidth = figwidth * num_cols if showColorbar: if colorbar_position == 'below': figheight += 1 / 2.54 else: total_figwidth += 1 / 2.54 fig = plt.figure(figsize=(total_figwidth, figheight)) xticks, xtickslabel = getProfileTicks(hm, reference_point_label, startLabel, endLabel) xticks_heat, xtickslabel_heat = get_heatmap_ticks(hm, reference_point_label, startLabel, endLabel) fig.suptitle(plotTitle, y=1 - (0.06 / figheight)) # color map for the summary plot (profile) on top of the heatmap cmap_plot = plt.get_cmap('jet') numgroups = hm.matrix.get_num_groups() if perGroup: color_list = cmap_plot( np.arange(hm.matrix.get_num_samples()) / hm.matrix.get_num_samples()) else: color_list = cmap_plot(np.arange(numgroups) / numgroups) alpha = colorMapDict['alpha'] # check if matrix is reference-point based using the upstream >0 value # and is sorted by region length. If this is # the case, prepare the data to plot a border at the regions end if hm.parameters['upstream'] > 0 and \ hm.matrix.sort_using == 'region_length' and \ hm.matrix.sort_method != 'no': _regions = hm.matrix.get_regions() regions_length_in_bins = [] for _group in _regions: _reg_len = [] for ind_reg in _group: _len = ind_reg['end'] - ind_reg['start'] _reg_len.append((hm.parameters['upstream'] + _len) / hm.parameters['bin size']) regions_length_in_bins.append(_reg_len) else: regions_length_in_bins = None first_group = 0 # helper variable to place the title per sample/group for sample in range(hm.matrix.get_num_samples()): sample_idx = sample for group in range(numgroups): group_idx = group # add the respective profile to the # summary plot sub_matrix = hm.matrix.get_matrix(group, sample) if showSummaryPlot: if perGroup: sample_idx = sample + 2 # plot + spacer else: group += 2 # plot + spacer first_group = 1 if perGroup: ax = fig.add_subplot(grids[sample_idx, group]) # the remainder (%) is used to iterate # over the available color maps (cmap). # if the user only provided, lets say two # and there are 10 groups, colormaps they are reused every # two groups. cmap_idx = group_idx % len(cmap) zmin_idx = group_idx % len(zMin) zmax_idx = group_idx % len(zMax) else: ax = fig.add_subplot(grids[group, sample]) # see above for the use of '%' cmap_idx = sample % len(cmap) zmin_idx = sample % len(zMin) zmax_idx = sample % len(zMax) if group == first_group and not showSummaryPlot and not perGroup: title = hm.matrix.sample_labels[sample] ax.set_title(title) if box_around_heatmaps is False: # Turn off the boxes around the individual heatmaps ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) rows, cols = sub_matrix['matrix'].shape interpolation_type = None if rows >= 1000 and cols >= 200 else 'nearest' img = ax.imshow(sub_matrix['matrix'], aspect='auto', interpolation=interpolation_type, origin='upper', vmin=zMin[zmin_idx], vmax=zMax[zmax_idx], cmap=cmap[cmap_idx], alpha=alpha, extent=[0, cols, rows, 0]) img.set_rasterized(True) # plot border at the end of the regions # if ordered by length if regions_length_in_bins is not None: x_lim = ax.get_xlim() y_lim = ax.get_ylim() ax.plot(regions_length_in_bins[group_idx], np.arange(len(regions_length_in_bins[group_idx])), '--', color='black', linewidth=0.5, dashes=(3, 2)) ax.set_xlim(x_lim) ax.set_ylim(y_lim) if perGroup: ax.axes.set_xlabel(sub_matrix['group']) if sample < hm.matrix.get_num_samples() - 1: ax.axes.get_xaxis().set_visible(False) else: ax.axes.get_xaxis().set_visible(False) ax.axes.set_xlabel(xAxisLabel) ax.axes.set_yticks([]) if perGroup and group == 0: ax.axes.set_ylabel(sub_matrix['sample']) elif not perGroup and sample == 0: ax.axes.set_ylabel(sub_matrix['group']) # add labels to last block in a column if (perGroup and sample == numsamples - 1) or \ (not perGroup and group_idx == numgroups - 1): # add xticks to the bottom heatmap (last group) ax.axes.get_xaxis().set_visible(True) ax.axes.set_xticks(xticks_heat) ax.axes.set_xticklabels(xtickslabel_heat, size=8) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax.xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') ax.get_xaxis().set_tick_params(which='both', top='off', direction='out') if showColorbar and colorbar_position == 'below': # draw a colormap per each heatmap below the last block if perGroup: col = group_idx else: col = sample ax = fig.add_subplot(grids[-1, col]) tick_locator = ticker.MaxNLocator(nbins=3) cbar = fig.colorbar(img, cax=ax, alpha=alpha, orientation='horizontal', ticks=tick_locator) labels = cbar.ax.get_xticklabels() ticks = cbar.ax.get_xticks() if ticks[0] == 0: # if the label is at the start of the colobar # move it a bit inside to avoid overlapping # with other labels labels[0].set_horizontalalignment('left') if ticks[-1] == 1: # if the label is at the end of the colobar # move it a bit inside to avoid overlapping # with other labels labels[-1].set_horizontalalignment('right') # cbar.ax.set_xticklabels(labels, rotation=90) # plot the profiles on top of the heatmaps if showSummaryPlot: ax_list = [] if perGroup: iterNum = numgroups else: iterNum = hm.matrix.get_num_samples() # plot each of the profiles for sample_id in range(iterNum): if perGroup: title = hm.matrix.group_labels[sample_id] else: title = hm.matrix.sample_labels[sample_id] if sample_id > 0: ax_profile = fig.add_subplot(grids[0, sample_id], sharey=ax_list[0]) else: ax_profile = fig.add_subplot(grids[0, sample_id]) ax_profile.set_title(title) if perGroup: iterNum2 = hm.matrix.get_num_samples() else: iterNum2 = numgroups for group in range(iterNum2): if perGroup: sub_matrix = hm.matrix.get_matrix(sample_id, group) line_label = sub_matrix['sample'] else: sub_matrix = hm.matrix.get_matrix(group, sample_id) line_label = sub_matrix['group'] plot_single(ax_profile, sub_matrix['matrix'], averageType, color_list[group], line_label, plot_type='simple') if sample_id > 0: plt.setp(ax_profile.get_yticklabels(), visible=False) if sample_id == 0 and yAxisLabel != '': ax_profile.set_ylabel(yAxisLabel) ax_profile.axes.set_xticks(xticks) ax_profile.axes.set_xticklabels(xtickslabel) ax_list.append(ax_profile) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax_profile.xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') # It turns out that set_ylim only takes np.float64s if yMin: yMin = np.float64(yMin) if yMax: yMax = np.float64(yMax) ax_list[0].set_ylim(yMin, yMax) # reduce the number of yticks by half num_ticks = len(ax_list[0].get_yticks()) yticks = [ax_list[0].get_yticks()[i] for i in range(1, num_ticks, 2)] ax_list[0].set_yticks(yticks) if legend_location != 'none': ax_list[-1].legend(loc=legend_location.replace('-', ' '), ncol=1, prop=fontP, frameon=False, markerscale=0.5) if showColorbar and colorbar_position != 'below': if showSummaryPlot: # we don't want to colorbar to extend # over the profiles and spacer top rows grid_start = 2 else: grid_start = 0 ax = fig.add_subplot(grids[grid_start:, -1]) fig.colorbar(img, cax=ax, alpha=alpha) if box_around_heatmaps: plt.subplots_adjust(wspace=0.10, hspace=0.025, top=0.85, bottom=0, left=0.04, right=0.96) else: # When no box is plotted the space between heatmaps is reduced plt.subplots_adjust(wspace=0.05, hspace=0.01, top=0.85, bottom=0, left=0.04, right=0.96) plt.savefig(outFileName, bbox_inches='tight', pdd_inches=0, dpi=dpi, format=image_format) plt.close()
def plot_profile(self): if not self.color_list: cmap_plot = plt.get_cmap('jet') if self.numlines > 1: # kmeans, so we need to color by cluster self.color_list = cmap_plot(np.arange(self.numlines, dtype=float) / self.numlines) else: self.color_list = cmap_plot(np.arange(self.numplots, dtype=float) / self.numplots) if (self.numlines > 1 and len(self.color_list) < self.numlines) or\ (self.numlines == 1 and len(self.color_list) < self.numplots): sys.exit("\nThe given list of colors is too small, " "at least {} colors are needed\n".format(self.numlines)) for color in self.color_list: if not pltcolors.is_color_like(color): sys.exit("\nThe color name {} is not valid. Check " "the name or try with a html hex string " "for example #eeff22".format(color)) first = True ax_list = [] for plot in range(self.numplots): col = plot % self.plots_per_row row = int(plot / self.plots_per_row) if row == 0 and col == 0: ax = self.fig.add_subplot(self.grids[row, col]) else: ax = self.fig.add_subplot(self.grids[row, col], sharey=ax_list[0]) if self.per_group: title = self.hm.matrix.group_labels[plot] if row != 0: plt.setp(ax.get_yticklabels(), visible=False) else: title = self.hm.matrix.sample_labels[plot] if col != 0: plt.setp(ax.get_yticklabels(), visible=False) ax.set_title(title) for data_idx in range(self.numlines): if self.per_group: _row, _col = plot, data_idx else: _row, _col = data_idx, plot sub_matrix = self.hm.matrix.get_matrix(_row, _col) if self.per_group: label = sub_matrix['sample'] else: label = sub_matrix['group'] if self.numlines > 1: coloridx = data_idx else: coloridx = plot plot_single(ax, sub_matrix['matrix'], self.averagetype, self.color_list[coloridx], label, plot_type=self.plot_type) # remove the numbers of the y axis for all plots plt.setp(ax.get_yticklabels(), visible=False) if col == 0: # add the y axis label for the first plot # on each row and make the numbers and ticks visible plt.setp(ax.get_yticklabels(), visible=True) ax.axes.set_ylabel(self.y_axis_label) """ # reduce the number of yticks by half num_ticks = len(ax.get_yticks()) yticks = [ax.get_yticks()[i] for i in range(1, num_ticks, 2)] ax.set_yticks(yticks) """ ax.axes.set_xticks(self.xticks) ax.axes.set_xticklabels(self.xtickslabel) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax.xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') if first and self.plot_type not in ['heatmap', 'overlapped_lines']: ax.legend(loc=self.legend_location.replace('-', ' '), ncol=1, prop=self.font_p, frameon=False, markerscale=0.5) first = False """ ax.legend(bbox_to_anchor=(-0.05, -1.13, 1., 1), loc='upper center', ncol=1, mode="expand", prop=font_p, frameon=False, markerscale=0.5) """ lims = ax.get_ylim() if self.y_min is not None: lims = (self.y_min, lims[1]) if self.y_max is not None: lims = (lims[0], self.y_max) if lims[0] >= lims[1]: lims = (lims[0], lims[0] + 1) ax.set_ylim(lims) ax_list.append(ax) plt.subplots_adjust(wspace=0.05, hspace=0.3) plt.tight_layout() plt.savefig(self.out_file_name, dpi=200, format=self.image_format) plt.close()
def addProfilePlot(hm, plt, fig, grids, iterNum, iterNum2, perGroup, averageType, xticks, xtickslabel, yAxisLabel, color_list, yMin, yMax, wspace, hspace, colorbar_position): """ A function to add profile plots to the given figure, possibly in a custom grid subplot which mimics a tight layout (if wspace and hspace are not None) """ if wspace is not None and hspace is not None: if colorbar_position == 'side': gridsSub = gridspec.GridSpecFromSubplotSpec(1, iterNum, subplot_spec=grids[0, :-1], wspace=wspace, hspace=hspace) else: gridsSub = gridspec.GridSpecFromSubplotSpec(1, iterNum, subplot_spec=grids[0, :], wspace=wspace, hspace=hspace) ax_list = [] for sample_id in range(iterNum): if perGroup: title = hm.matrix.group_labels[sample_id] else: title = hm.matrix.sample_labels[sample_id] if sample_id > 0 and len(yMin) == 1 and len(yMax) == 1: ax_profile = fig.add_subplot(grids[0, sample_id], sharey=ax_list[0]) else: if wspace is not None and hspace is not None: ax_profile = fig.add_subplot(gridsSub[0, sample_id]) else: ax_profile = fig.add_subplot(grids[0, sample_id]) ax_profile.set_title(title) for group in range(iterNum2): if perGroup: sub_matrix = hm.matrix.get_matrix(sample_id, group) line_label = sub_matrix['sample'] else: sub_matrix = hm.matrix.get_matrix(group, sample_id) line_label = sub_matrix['group'] plot_single(ax_profile, sub_matrix['matrix'], averageType, color_list[group], line_label, plot_type='simple') if sample_id > 0 and len(yMin) == 1 and len(yMax) == 1: plt.setp(ax_profile.get_yticklabels(), visible=False) if sample_id == 0 and yAxisLabel != '': ax_profile.set_ylabel(yAxisLabel) if np.ceil(max(xticks)) != float(sub_matrix['matrix'].shape[1]): tickscale = float(sub_matrix['matrix'].shape[1]) / max(xticks) xticks_use = [x * tickscale for x in xticks] ax_profile.axes.set_xticks(xticks_use) else: ax_profile.axes.set_xticks(xticks) ax_profile.axes.set_xticklabels(xtickslabel) ax_list.append(ax_profile) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax_profile.xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') # It turns out that set_ylim only takes np.float64s localYMin = yMin[sample_id % len(yMin)] localYMax = yMax[sample_id % len(yMax)] lims = ax_list[0].get_ylim() if localYMin: lims = (np.float64(localYMin), lims[1]) if localYMax: lims = (lims[0], np.float64(localYMax)) if lims[0] >= lims[1]: lims = (lims[0], lims[0] + 1) if len(yMin) == 1 and len(yMax) == 1: ax_list[0].set_ylim(lims) else: ax_list[-1].set_ylim(lims) return ax_list
def plot_profile(self): if self.y_min is None: self.y_min = [None] if self.y_max is None: self.y_max = [None] if not self.color_list: cmap_plot = plt.get_cmap('jet') if self.numlines > 1: # kmeans, so we need to color by cluster self.color_list = cmap_plot(np.arange(self.numlines, dtype=float) / float(self.numlines)) else: self.color_list = cmap_plot(np.arange(self.numplots, dtype=float) / float(self.numplots)) if (self.numlines > 1 and len(self.color_list) < self.numlines) or\ (self.numlines == 1 and len(self.color_list) < self.numplots): sys.exit("\nThe given list of colors is too small, " "at least {} colors are needed\n".format(self.numlines)) for color in self.color_list: if not pltcolors.is_color_like(color): sys.exit("\nThe color name {} is not valid. Check " "the name or try with a html hex string " "for example #eeff22".format(color)) if self.image_format == "plotly": return self.plotly_profile() first = True ax_list = [] globalYmin = np.inf globalYmax = -np.inf for plot in range(self.numplots): localYMin = None localYMax = None col = plot % self.plots_per_row row = int(plot / float(self.plots_per_row)) if (row == 0 and col == 0) or len(self.y_min) > 1 or len(self.y_max) > 1: ax = self.fig.add_subplot(self.grids[row, col]) else: ax = self.fig.add_subplot(self.grids[row, col]) if self.per_group: title = self.hm.matrix.group_labels[plot] if row != 0 and len(self.y_min) == 1 and len(self.y_max) == 1: plt.setp(ax.get_yticklabels(), visible=False) tickIdx = plot % self.hm.matrix.get_num_samples() else: title = self.hm.matrix.sample_labels[plot] if col != 0 and len(self.y_min) == 1 and len(self.y_max) == 1: plt.setp(ax.get_yticklabels(), visible=False) tickIdx = plot ax.set_title(title) for data_idx in range(self.numlines): if self.per_group: _row, _col = plot, data_idx else: _row, _col = data_idx, plot if localYMin is None or self.y_min[col % len(self.y_min)] < localYMin: localYMin = self.y_min[col % len(self.y_min)] if localYMax is None or self.y_max[col % len(self.y_max)] > localYMax: localYMax = self.y_max[col % len(self.y_max)] sub_matrix = self.hm.matrix.get_matrix(_row, _col) if self.per_group: label = sub_matrix['sample'] else: label = sub_matrix['group'] if self.numlines > 1: coloridx = data_idx else: coloridx = plot plot_single(ax, sub_matrix['matrix'], self.averagetype, self.color_list[coloridx], label, plot_type=self.plot_type) globalYmin = min(np.float64(globalYmin), ax.get_ylim()[0]) globalYmax = max(globalYmax, ax.get_ylim()[1]) # remove the numbers of the y axis for all plots plt.setp(ax.get_yticklabels(), visible=False) if col == 0 or len(self.y_min) > 1 or len(self.y_max) > 1: # add the y axis label for the first plot # on each row and make the numbers and ticks visible plt.setp(ax.get_yticklabels(), visible=True) ax.axes.set_ylabel(self.y_axis_label) totalWidth = sub_matrix['matrix'].shape[1] xticks, xtickslabel = self.getTicks(tickIdx) if np.ceil(max(xticks)) != float(totalWidth): tickscale = float(totalWidth) / max(xticks) xticks_use = [x * tickscale for x in xticks] ax.axes.set_xticks(xticks_use) else: ax.axes.set_xticks(xticks) ax.axes.set_xticklabels(xtickslabel, rotation=self.label_rotation) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax.xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') if first and self.plot_type not in ['heatmap', 'overlapped_lines']: ax.legend(loc=self.legend_location.replace('-', ' '), ncol=1, prop=self.font_p, frameon=False, markerscale=0.5) if len(self.y_min) == 1 and len(self.y_max) == 1: first = False ax_list.append(ax) # It turns out that set_ylim only takes np.float64s for sample_id, subplot in enumerate(ax_list): localYMin = self.y_min[sample_id % len(self.y_min)] localYMax = self.y_max[sample_id % len(self.y_max)] lims = [globalYmin, globalYmax] if localYMin is not None: if localYMax is not None: lims = (np.float64(localYMin), np.float64(localYMax)) else: lims = (np.float64(localYMin), lims[1]) elif localYMax is not None: lims = (lims[0], np.float64(localYMax)) if lims[0] >= lims[1]: lims = (lims[0], lims[0] + 1) ax_list[sample_id].set_ylim(lims) plt.subplots_adjust(wspace=0.05, hspace=0.3) plt.tight_layout() plt.savefig(self.out_file_name, dpi=self.dpi, format=self.image_format) plt.close()
def plotMatrix(hm, outFileName, colorMapDict={ 'colorMap': 'binary', 'missingDataColor': 'black' }, plotTitle='', xAxisLabel='', yAxisLabel='', regionsLabel='', zMin=None, zMax=None, yMin=None, yMax=None, averageType='median', reference_point_label='TSS', startLabel='TSS', endLabel="TES", heatmapHeight=25, heatmapWidth=7.5, perGroup=False, whatToShow='plot, heatmap and colorbar', plotType='simple', image_format=None, legend_location='upper-left'): matrix_flatten = None if zMin is None: matrix_flatten = hm.matrix.flatten() # try to avoid outliers by using np.percentile zMin = np.percentile(matrix_flatten, 1.0) if np.isnan(zMin): zMin = None if zMax is None: if matrix_flatten is None: matrix_flatten = hm.matrix.flatten() # try to avoid outliers by using np.percentile zMax = np.percentile(matrix_flatten, 98.0) if np.isnan(zMax): zMax = None plt.rcParams['font.size'] = 8.0 fontP = FontProperties() # fontP.set_size('small') showSummaryPlot = False showColorbar = False if whatToShow == 'plot and heatmap': showSummaryPlot = True elif whatToShow == 'heatmap and colorbar': showColorbar = True else: showSummaryPlot = True showColorbar = True grids = prepare_layout(hm.matrix, (heatmapWidth, heatmapHeight), showSummaryPlot, showColorbar, perGroup) # figsize: w,h tuple in inches figwidth = heatmapWidth / 2.54 figheight = heatmapHeight / 2.54 if showSummaryPlot: # the summary plot ocupies a height # equal to the fig width figheight += figwidth numsamples = hm.matrix.get_num_samples() total_figwidth = figwidth * numsamples if showColorbar: total_figwidth += 1 / 2.54 fig = plt.figure(figsize=(total_figwidth, figheight)) hm.parameters['upstream'] hm.parameters['downstream'] hm.parameters['body'] hm.parameters['bin size'] xticks, xtickslabel = getProfileTicks(hm, reference_point_label, startLabel, endLabel) xticks_heat, xtickslabel_heat = get_heatmap_ticks(hm, reference_point_label, startLabel, endLabel) fig.suptitle(plotTitle, y=1 - (0.06 / figheight)) # colormap for the heatmap if colorMapDict['colorMap']: cmap = plt.get_cmap(colorMapDict['colorMap']) if colorMapDict['colorList'] and len(colorMapDict['colorList']) > 0: cmap = matplotlib.colors.LinearSegmentedColormap.from_list( 'my_cmap', colorMapDict['colorList'], N=colorMapDict['colorNumber']) # color map for the summary plot (profile) on top of the heatmap cmap_plot = plt.get_cmap('jet') numgroups = hm.matrix.get_num_groups() if perGroup: color_list = cmap_plot( np.arange(hm.matrix.get_num_samples()) / hm.matrix.get_num_samples()) else: color_list = cmap_plot(np.arange(numgroups) / numgroups) cmap.set_bad( colorMapDict['missingDataColor']) # nans are printed using this color # check if matrix is reference-point based using the upstream >0 value # and is sorted by region length. If this is # the case, prepare the data to plot a border at the regions end if hm.parameters['upstream'] > 0 and \ hm.matrix.sort_using == 'region_length' and \ hm.matrix.sort_method != 'no': _regions = hm.matrix.get_regions() regions_length_in_bins = [] for _group in _regions: _reg_len = [] for ind_reg in _group: _len = ind_reg['end'] - ind_reg['start'] _reg_len.append((hm.parameters['upstream'] + _len) / hm.parameters['bin size']) # print hm.parameters['upstream'] + (_len / hm.parameters['bin size']) regions_length_in_bins.append(_reg_len) else: regions_length_in_bins = None first_group = 0 # helper variable to place the title per sample/group for sample in range(hm.matrix.get_num_samples()): sample_idx = sample for group in range(numgroups): group_idx = group # add the respective profile to the # summary plot sub_matrix = hm.matrix.get_matrix(group, sample) if showSummaryPlot: if perGroup: sample_idx = sample + 2 # plot + spacer else: group += 2 # plot + spacer first_group = 1 if perGroup: ax = fig.add_subplot(grids[sample_idx, group]) else: ax = fig.add_subplot(grids[group, sample]) if group == first_group and not showSummaryPlot and not perGroup: title = hm.matrix.sample_labels[sample] ax.set_title(title) rows, cols = sub_matrix['matrix'].shape interpolation_type = 'bicubic' if rows > 200 and \ cols > 1000 else 'nearest' img = ax.imshow(sub_matrix['matrix'], aspect='auto', interpolation=interpolation_type, origin='upper', vmin=zMin, vmax=zMax, cmap=cmap, extent=[0, cols, rows, 0]) # plot border at the end of the regions # if ordered by length if regions_length_in_bins is not None: x_lim = ax.get_xlim() y_lim = ax.get_ylim() ax.plot(regions_length_in_bins[group_idx], np.arange(len(regions_length_in_bins[group_idx])), '--', color='black', linewidth=0.5, dashes=(3, 2)) ax.set_xlim(x_lim) ax.set_ylim(y_lim) if perGroup: ax.axes.set_xlabel(sub_matrix['group']) if sample < hm.matrix.get_num_samples() - 1: ax.axes.get_xaxis().set_visible(False) else: ax.axes.get_xaxis().set_visible(False) ax.axes.set_xlabel(xAxisLabel) ax.axes.set_yticks([]) if perGroup and group == 0: ax.axes.set_ylabel(sub_matrix['sample']) elif not perGroup and sample == 0: ax.axes.set_ylabel(sub_matrix['group']) # add xticks to the bottom heatmap (last group) ax.axes.get_xaxis().set_visible(True) ax.axes.set_xticks(xticks_heat) ax.axes.set_xticklabels(xtickslabel_heat, size=8) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax.xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') ax.get_xaxis().set_tick_params(which='both', top='off', direction='out') # plot the profiles on top of the heatmaps if showSummaryPlot: ax_list = [] if perGroup: iterNum = numgroups else: iterNum = hm.matrix.get_num_samples() # plot each of the profiles for sample_id in range(iterNum): if perGroup: title = hm.matrix.group_labels[sample_id] else: title = hm.matrix.sample_labels[sample_id] if sample_id > 0: ax_profile = fig.add_subplot(grids[0, sample_id], sharey=ax_list[0]) else: ax_profile = fig.add_subplot(grids[0, sample_id]) ax_profile.set_title(title) if perGroup: iterNum2 = hm.matrix.get_num_samples() else: iterNum2 = numgroups for group in range(iterNum2): if perGroup: sub_matrix = hm.matrix.get_matrix(sample_id, group) line_label = sub_matrix['sample'] else: sub_matrix = hm.matrix.get_matrix(group, sample_id) line_label = sub_matrix['group'] plot_single(ax_profile, sub_matrix['matrix'], averageType, color_list[group], line_label, plot_type='simple') if sample_id > 0: plt.setp(ax_profile.get_yticklabels(), visible=False) if sample_id == 0 and yAxisLabel != '': ax_profile.set_ylabel(yAxisLabel) ax_profile.axes.set_xticks(xticks) ax_profile.axes.set_xticklabels(xtickslabel) ax_list.append(ax_profile) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax_profile.xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') # reduce the number of yticks by half num_ticks = len(ax_list[0].get_yticks()) yticks = [ax_list[0].get_yticks()[i] for i in range(1, num_ticks, 2)] ax_list[0].set_yticks(yticks) ax_list[0].set_ylim(yMin, yMax) if legend_location != 'none': ax_list[-1].legend(loc=legend_location.replace('-', ' '), ncol=1, prop=fontP, frameon=False, markerscale=0.5) if showColorbar: if showSummaryPlot: # we dont want to colorbar to extend # over the profiles row grid_start = 2 else: grid_start = 0 ax = fig.add_subplot(grids[grid_start:, -1]) fig.colorbar(img, cax=ax) plt.subplots_adjust(wspace=0.05, hspace=0.025, top=0.85, bottom=0, left=0.04, right=0.96) plt.savefig(outFileName, bbox_inches='tight', pdd_inches=0, dpi=200, format=image_format)