|
| 1 | +# --- |
| 2 | +# jupyter: |
| 3 | +# jupytext: |
| 4 | +# cell_metadata_filter: -all |
| 5 | +# formats: py:light,ipynb |
| 6 | +# text_representation: |
| 7 | +# extension: .py |
| 8 | +# format_name: light |
| 9 | +# format_version: '1.5' |
| 10 | +# jupytext_version: 1.17.0 |
| 11 | +# kernelspec: |
| 12 | +# display_name: .venv |
| 13 | +# language: python |
| 14 | +# name: python3 |
| 15 | +# --- |
| 16 | + |
| 17 | +# # Customize a plot |
| 18 | + |
| 19 | +# The `SpikeInterface` widgets are designed to have reasonable default plotting options, but |
| 20 | +# sometimes you'll want to make adjustments to the plots. For doing this, we expose the underlying |
| 21 | +# `matplotlib` objects for you to edit. Let's see how to do this in an example. First, |
| 22 | +# let's make some synthetic data and compute some extensions which can be used for plotting. |
| 23 | + |
| 24 | +# + |
| 25 | +import spikeinterface.full as si |
| 26 | +import matplotlib.pyplot as plt |
| 27 | + |
| 28 | +recording, sorting = si.generate_ground_truth_recording(seed=1205) |
| 29 | +sorting_analyzer = si.create_sorting_analyzer(sorting=sorting, recording=recording) |
| 30 | +sorting_analyzer.compute({"random_spikes": {'seed': 1205}, "templates": {}, "unit_locations": {}}) |
| 31 | + |
| 32 | +unit_locations = sorting_analyzer.get_extension("unit_locations").get_data() |
| 33 | +# - |
| 34 | + |
| 35 | +# Now we can plot the `unit_locations` and `unit_templates` using the approperiate widgets |
| 36 | +# (see the [full list of widgets](https://spikeinterface.readthedocs.io/en/stable/modules/widgets.html#available-plotting-functions) |
| 37 | +# for more!). These functions output a `widget`. We'll assign the unit locations widget to `fig_units`. |
| 38 | + |
| 39 | +# + |
| 40 | +fig_units = si.plot_unit_locations(sorting_analyzer) |
| 41 | + |
| 42 | +# Each widget contains a `matplotlib` figure and axis: |
| 43 | +print(type(fig_units.figure)) |
| 44 | +print(type(fig_units.ax)) |
| 45 | +# - |
| 46 | + |
| 47 | +# By getting with the matplotlib objects, we gain access to the full `matplotlib` machinery: adding custom |
| 48 | +# titles, axis labels, ticks, more plots etc. Let's cusomtise our unit locations plot. (Note: the |
| 49 | +# `SpikeInterface` Team does not endorse the following style conventions): |
| 50 | + |
| 51 | +# + |
| 52 | +# Get the widget |
| 53 | +fig_units = si.plot_unit_locations(sorting_analyzer) |
| 54 | + |
| 55 | +# Modify the widget's `axis`` to set the title and axes labels |
| 56 | +fig_units.ax.set_title("My favorite units", fontname = "Comic Sans MS") |
| 57 | +fig_units.ax.set_xlabel("x probe location (um)") |
| 58 | +fig_units.ax.set_xlabel("y probe location (um)") |
| 59 | + |
| 60 | +# You can also set custom ticks |
| 61 | +fig_units.ax.set_xticks([-60,-30,unit_locations[0,0],30,60]) |
| 62 | +fig_units.ax.set_xticklabels([-60,-30,"unit_0_x",30,60]) |
| 63 | +fig_units.ax.set_yticks([-40,-20,0,unit_locations[0,1],40]) |
| 64 | +fig_units.ax.set_yticklabels([-40,-20,0,"unit_0_y",40]) |
| 65 | + |
| 66 | +# Change the limits of the plot |
| 67 | +fig_units.ax.set_xlim((-30,50)) |
| 68 | +fig_units.ax.set_ylim((-50,50)) |
| 69 | + |
| 70 | +# And add extra information on the plot |
| 71 | +fig_units.ax.text(unit_locations[6,0], unit_locations[6,1]+5, s="UNIT 6!!!", fontname="Courier") |
| 72 | + |
| 73 | +fig_units |
| 74 | +# - |
| 75 | + |
| 76 | +# Beautiful!!! |
| 77 | +# |
| 78 | +# You can also combine figures into a multi-figure plot. The easiest way to do this is to set up your figure and axes first, then tell `SpikeInterface` which axes it should attach the widget plot to. Here's an example of making a unit summary plot. |
| 79 | + |
| 80 | +# + |
| 81 | +import matplotlib.pyplot as plt |
| 82 | +fig, axs = plt.subplots(ncols=2, nrows=1) |
| 83 | + |
| 84 | +unit_id=8 |
| 85 | +si.plot_unit_locations(sorting_analyzer=sorting_analyzer, ax=axs[0]) |
| 86 | +si.plot_unit_templates(sorting_analyzer, axes=[axs[1]], unit_ids=[f'{unit_id}']) |
| 87 | + |
| 88 | +axs[0].plot([unit_locations[8,0], unit_locations[8,0]+50], [unit_locations[8,1], unit_locations[8,1]+50]) |
| 89 | +axs[0].text(unit_locations[8,0]+52, unit_locations[8,1]+52, s=f"Unit {unit_id}") |
| 90 | +axs[0].set_title("Unit location", fontsize=10) |
| 91 | + |
| 92 | +fig.suptitle(f"Unit {unit_id} summary", fontfamily="Comic Sans MS", fontsize=20) |
| 93 | + |
| 94 | +fig.tight_layout() |
| 95 | +# - |
| 96 | + |
| 97 | +# For more details on what you can do using matplotlib, check out their [extensive documentation](https://matplotlib.org/stable/) |
0 commit comments