def plot_openness_by_weekday_by_semester(period: dict, ax: Axes): """ Plot openness by semester. :param period: Period over which to average the openness :param ax: Axes object in which to put the plot :return: None """ # Configuration num_weekdays = 5 init_year = 2015 init_semester = "Vår" # Get data for plot dataseries = get_openness_by_weekday_by_semester(period) num_series = len(dataseries) bar_width = 1 / (num_series + 1) # Create plot cur_year = init_year cur_semester = init_semester legend = [] for semester_index, week_bins in enumerate(dataseries): # Add bars ax.bar( [ weekday_index + semester_index * bar_width for weekday_index in range(num_weekdays) ], height=week_bins[:num_weekdays], width=bar_width, ) # Add to legend legend.append(f"{cur_semester} {cur_year}") # Update semester and year for next bars if cur_semester == "Høst": cur_semester = "Vår" cur_year += 1 else: cur_semester = "Høst" # Place legend and labels ax.legend(legend, loc="lower right") ax.set_xticklabels(("", "Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag")) ax.set_yticklabels( [f"{openness * 100:.1f}{percent()}" for openness in ax.get_yticks()] ) ax.set_ylabel("Andel åpen")
def format_axes(in_ax: matplotlib.axes, labelsize: int = 12, ticksize: int = 10, legendsize: int = 8, box: bool = False) -> None: """Formats ticks, tick label sizes, and axes label sizes for box plots. Parameters ---------- in_ax : matplotlib.axes Axes containing a plot. labelsize : int, optional Size of axes labels. ticksize : int, optional Size of tick labels. legendsize : int, optional Specifies font size of strings in legend. box : bool, optional Specifies whether or not a box plot has been plotted. Returns ------- None """ in_ax.xaxis.set_minor_locator(AutoMinorLocator()) in_ax.yaxis.set_minor_locator(AutoMinorLocator()) in_ax.xaxis.label.set_size(labelsize) in_ax.yaxis.label.set_size(labelsize) in_ax.tick_params(axis='both', which='major', direction='in', length=4, bottom=True, top=True, left=True, right=True, labelsize=ticksize, color='grey') in_ax.tick_params(axis='both', which='minor', direction='in', length=2, bottom=True, top=True, left=True, right=True, color='grey') if not box: legend = in_ax.legend(fontsize=legendsize, edgecolor=(1, 1, 1, 0.), facecolor=(1, 1, 1, 0)) legend.get_frame().set_alpha(0)
def plot_chromatograph( seq: SeqRecord, region: Tuple[int, int] = None, ax: mpl.axes = None ) -> plt.axes: """Plot Sanger chromatograph. region: include both start and end (1-based) """ if seq is None: return ax if region is None: # turn into 0 based for better indexing region_start, region_end = 0, len(seq) else: region_start = max(region[0], 0) region_end = min(region[1], len(seq) - 1) if ax is None: _, ax = plt.subplots(1, 1, figsize=(16, 6)) _colors = defaultdict( lambda: "purple", {"A": "g", "C": "b", "G": "k", "T": "r"} ) # Get signals peaks = seq.annotations["peak positions"] trace_x = seq.annotations["trace_x"] traces_y = [seq.annotations["channel " + str(i)] for i in range(1, 5)] bases = seq.annotations["channels"] xlim_left, xlim_right = peaks[region_start] - 1, peaks[region_end] + 0.5 # subset peak and sequence # TODO: this might fix the bug peak_start = peaks[0] peak_zip = [ (p, s) for i, (p, s) in enumerate(zip(peaks, seq)) if region_start <= i <= region_end ] peaks, seq = list(zip(*peak_zip)) # subset trace_x and traces_y together trace_zip = [ (x + peak_start, *ys) for x, *ys in zip(trace_x, *traces_y) if xlim_left <= x <= xlim_right ] if not trace_zip: return ax trace_x, *traces_y = list(zip(*trace_zip)) # Plot traces trmax = max(map(max, traces_y)) for base in bases: trace_y = [1.0 * ci / trmax for ci in traces_y[bases.index(base)]] ax.plot(trace_x, trace_y, color=_colors[base], lw=2, label=base) ax.fill_between( trace_x, 0, trace_y, facecolor=_colors[base], alpha=0.125 ) # Plot bases at peak positions for i, peak in enumerate(peaks): # LOGGER.debug(f"{i}, {peak}, {seq[i]}, {xlim_left + i}") ax.text( peak, -0.11, seq[i], color=_colors[seq[i]], va="center", ha="center", alpha=0.66, fontsize="x-large", fontweight="bold", ) ax.set_ylim(bottom=-0.15, top=1.05) # peaks[0] - max(2, 0.02 * (peaks[-1] - peaks[0])), # right=peaks[-1] + max(2, 0.02 * (peaks[-1] - peaks[0])), ax.set_xlim(xlim_left + 0.5, xlim_right) ax.set_xticks(peaks) ax.set_xticklabels(list(range(region_start + 1, region_end + 2))) # hide y axis ax.set_yticklabels([]) ax.get_yaxis().set_visible(False) # hide border ax.spines["left"].set_visible(False) ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) # hide grid ax.grid(False) # set legend ax.legend(loc="upper left", bbox_to_anchor=(0.95, 0.99)) return ax