roybens / MEA_Analysis

Code for analyzing our HD-MEA data
5 stars 6 forks source link

Legends on sorted Rasters ( IPN ANALYSIS) #18

Open mandarmp opened 3 months ago

mandarmp commented 3 months ago

/home/mmp/.local/lib/python3.10/site-packages/IPython/core/events.py:89: UserWarning: Creating legend with loc="best" can be slow with large amounts of data. func(*args, kwargs) /home/mmp/.local/lib/python3.10/site-packages/IPython/core/pylabtools.py:152: UserWarning: Creating legend with loc="best" can be slow with large amounts of data. fig.canvas.print_figure(bytes_io, kw)

mandarmp commented 3 months ago

Plot raster plot with bursts highlighted for all units

def plot_raster_with_bursts(ax, spike_times, bursts, sorted_units=None, title_suffix=""): y_offset = 0 units_to_plot = sorted_units if sorted_units else list(spike_times.keys())

# Handles for the legend
handles = []

for unit in units_to_plot:
    times = spike_times[unit]
    unit_bursts = bursts[unit]

    # Plot all spike times for this unit
    regular_spikes,=ax.plot(times, np.ones_like(times) + y_offset, '|', color='royalblue', markersize=1, rasterized=True,label="Spikes with AP Freq < 10Hz")

    # Highlight bursts for this unit
    for burst in unit_bursts:
        burst_spikes,=ax.plot(burst, np.ones_like(burst) + y_offset, '|', color='black', markersize=1, rasterized=True,label="Spikes with AP Freq > 10Hz")

    y_offset += 1  # Move to the next unit

ax.set_xlabel('Time (s)')
num_units = len(units_to_plot)
yticks = [1, num_units//3, 2*num_units//3, num_units]
ax.set_yticks(yticks)
ax.set_yticklabels(yticks)
ax.set_ylabel('Units')
ax.set_title(f'Raster Plot with Bursts Highlighted {title_suffix}')
# Adding legend
if regular_spikes:
    handles.append(regular_spikes)
if burst_spikes:
    handles.append(burst_spikes)
if handles:
    ax.legend(handles=handles)
return ax