def makeplot_recurrence(sequence, sequence2, recurrence_table, f_sequence, s_sequence): recurrence_table = np.array( np.reshape(recurrence_table, (sequence2, sequence)).tolist()) # create list of coordinates for requrence plot x_rqa = [] y_rqa = [] for y in range(0, 2 * sequence2, 2): for x in range(0, 2 * sequence, 2): if recurrence_table[int(y / 2)][int(x / 2)] == 1: x_rqa.append(x) y_rqa.append((2 * sequence2 - y - 1)) multi_plot = plt.figure(figsize=(12, 9)) multi_plot.clear() ax = multi_plot.add_subplot(1, 1, 1) # ax1 = plt.subplot2grid((8, 8), (0, 0), colspan=6, rowspan=2) # ax1.plot(list(range(len(count_y))), count_y) # plt.ylim(0, max(count_y) + 1) # plt.xlim(0,len(count_y)-1) # ax2 = plt.subplot2grid((8, 8), (2, 0), rowspan=6, colspan=6) if sequence > 100 or sequence2 > 100: size = 0.5 else: size = 3 wsp = [[x, y] for x, y in zip(x_rqa, y_rqa)] print(wsp) if wsp: wsp_spr = wsp[0] x = [] y = [] while len(wsp) > 1: wsp.remove(wsp_spr) if [wsp_spr[0] + 2, wsp_spr[1] - 2] in wsp: x.append(wsp_spr[0]) y.append(wsp_spr[1]) wsp_spr = [wsp_spr[0] + 2, wsp_spr[1] - 2] print(x, y) else: x.append(wsp_spr[0]) y.append(wsp_spr[1]) wsp_spr = wsp[0] print(x, y) if len(x) == 1: ax.plot(x, y, 'ro', markersize=1.5) else: ax.plot(x, y, 'r') x = [] y = [] if len(wsp) == 1: x.append(wsp[0][0]) y.append(wsp[0][1]) if len(x) == 1: ax.plot(x, y, 'ro', markersize=1.5) else: ax.plot(x, y, 'r') x = [] y = [] plt.xlim(-1, (2 * sequence) - 2) plt.ylim(0, 2 * sequence2) plt.xticks(range(0, 2 * sequence, 2), f_sequence) plt.yticks(range(1, 2 * sequence2 + 1, 2), s_sequence[::-1]) minorLocator = MultipleLocator(5) minor_xticks = np.arange(1, 2 * sequence, 2) minor_yticks = np.arange(0, 2 * sequence2, 2) ax.xaxis.tick_top() ax.set_xticks(minor_xticks, minor=True) ax.set_yticks(minor_yticks, minor=True) plt.grid(which='minor', alpha=0.5) multi_plot.canvas.draw() multi_plot.savefig('static/RQA.png')
def drawmatrix_channels(in_m, channel_names=None, fig=None, x_tick_rot=0, size=None, cmap=plt.cm.RdBu_r, colorbar=True, color_anchor=None, title=None): r"""Creates a lower-triangle of the matrix of an nxn set of values. This is the typical format to show a symmetrical bivariate quantity (such as correlation or coherence between two different ROIs). Parameters ---------- in_m: nxn array with values of relationships between two sets of rois or channels channel_names (optional): list of strings with the labels to be applied to the channels in the input. Defaults to '0','1','2', etc. fig (optional): a matplotlib figure cmap (optional): a matplotlib colormap to be used for displaying the values of the connections on the graph title (optional): string to title the figure (can be like '$\alpha$') color_anchor (optional): determine the mapping from values to colormap if None, min and max of colormap correspond to min and max of in_m if 0, min and max of colormap correspond to max of abs(in_m) if (a,b), min and max of colormap correspond to (a,b) Returns ------- fig: a figure object """ N = in_m.shape[0] ind = np.arange(N) # the evenly spaced plot indices def channel_formatter(x, pos=None): thisind = np.clip(int(x), 0, N - 1) return channel_names[thisind] if fig is None: fig = plt.figure() if size is not None: fig.set_figwidth(size[0]) fig.set_figheight(size[1]) w = fig.get_figwidth() h = fig.get_figheight() ax_im = fig.add_subplot(1, 1, 1) # If you want to draw the colorbar: if colorbar: divider = make_axes_locatable(ax_im) ax_cb = divider.new_vertical(size="10%", pad=0.1, pack_start=True) fig.add_axes(ax_cb) # Make a copy of the input, so that you don't make changes to the original # data provided m = in_m.copy() # Null the upper triangle, so that you don't get the redundant and the # diagonal values: idx_null = triu_indices(m.shape[0]) m[idx_null] = np.nan # Extract the minimum and maximum values for scaling of the # colormap/colorbar: max_val = np.nanmax(m) min_val = np.nanmin(m) if color_anchor is None: color_min = min_val color_max = max_val elif color_anchor == 0: bound = max(abs(max_val), abs(min_val)) color_min = -bound color_max = bound else: color_min = color_anchor[0] color_max = color_anchor[1] # The call to imshow produces the matrix plot: im = ax_im.imshow(m, origin='upper', interpolation='nearest', vmin=color_min, vmax=color_max, cmap=cmap) # Formatting: ax = ax_im ax.grid(True) # Label each of the cells with the row and the column: if channel_names is not None: for i in range(0, m.shape[0]): if i < (m.shape[0] - 1): ax.text(i - 0.3, i, channel_names[i], rotation=x_tick_rot) if i > 0: ax.text(-1, i + 0.3, channel_names[i], horizontalalignment='right') ax.set_axis_off() ax.set_xticks(np.arange(N)) ax.xaxis.set_major_formatter(ticker.FuncFormatter(channel_formatter)) fig.autofmt_xdate(rotation=x_tick_rot) ax.set_yticks(np.arange(N)) ax.set_yticklabels(channel_names) ax.set_ybound([-0.5, N - 0.5]) ax.set_xbound([-0.5, N - 1.5]) # Make the tick-marks invisible: for line in ax.xaxis.get_ticklines(): line.set_markeredgewidth(0) for line in ax.yaxis.get_ticklines(): line.set_markeredgewidth(0) ax.set_axis_off() if title is not None: ax.set_title(title) # The following produces the colorbar and sets the ticks if colorbar: # Set the ticks - if 0 is in the interval of values, set that, as well # as the maximal and minimal values: if min_val < 0: ticks = [color_min, min_val, 0, max_val, color_max] # Otherwise - only set the minimal and maximal value: else: ticks = [color_min, min_val, max_val, color_max] # This makes the colorbar: cb = fig.colorbar(im, cax=ax_cb, orientation='horizontal', cmap=cmap, norm=im.norm, boundaries=np.linspace(color_min, color_max, 256), ticks=ticks, format='%.2f') # Set the current figure active axis to be the top-one, which is the one # most likely to be operated on by users later on fig.sca(ax) return fig