Skip to content

Commit 60cf92d

Browse files
committed
respond to alessio
1 parent 8ccef3f commit 60cf92d

3 files changed

Lines changed: 113 additions & 15 deletions

File tree

doc/how_to/customize_a_plot.rst

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ Customize a plot
33

44
The ``SpikeInterface`` widgets are designed to have reasonable default
55
plotting options, but sometimes you’ll want to make adjustments to the
6-
plots. For doing this, we expose the underlying ``matplotlib`` objects for you to
7-
edit. Let’s see how to do this in an example. First, let’s make some synthetic
8-
data and compute some extensions which can be used for plotting.
6+
plots. For doing this, we expose the underlying ``matplotlib`` objects
7+
for you to edit. Let’s see how to do this in an example. First, let’s
8+
make some synthetic data and compute some extensions which can be used
9+
for plotting.
910

10-
.. code:: ipython3
11+
.. code::
1112
1213
import spikeinterface.full as si
1314
import matplotlib.pyplot as plt
@@ -37,7 +38,7 @@ widgets <https://spikeinterface.readthedocs.io/en/stable/modules/widgets.html#av
3738
for more!). These functions output a ``widget``. We’ll assign the unit
3839
locations widget to ``fig_units``.
3940

40-
.. code:: ipython3
41+
.. code::
4142
4243
fig_units = si.plot_unit_locations(sorting_analyzer)
4344
@@ -56,17 +57,18 @@ locations widget to ``fig_units``.
5657
.. image:: customize_a_plot_files/customize_a_plot_4_1.png
5758

5859

59-
By playing with the matplotlib objects, we gain access to the full matplotlib
60-
machinery: adding custom titles, axis labels, ticks, more plots etc.
61-
Let’s cusomtise our unit locations plot (Note: the
62-
``SpikeInterface`` Team does not endorse the following style conventions):
60+
By getting with the matplotlib objects, we gain access to the full
61+
``matplotlib`` machinery: adding custom titles, axis labels, ticks, more
62+
plots etc. Let’s cusomtise our unit locations plot. (Note: the
63+
``SpikeInterface`` Team does not endorse the following style
64+
conventions):
6365

64-
.. code:: ipython3
66+
.. code::
6567
6668
# Get the widget
6769
fig_units = si.plot_unit_locations(sorting_analyzer)
6870
69-
# Modify the widget's `axis`` to set the title and xes labels
71+
# Modify the widget's `axis`` to set the title and axes labels
7072
fig_units.ax.set_title("My favorite units", fontname = "Comic Sans MS")
7173
fig_units.ax.set_xlabel("x probe location (um)")
7274
fig_units.ax.set_xlabel("y probe location (um)")
@@ -89,10 +91,9 @@ Let’s cusomtise our unit locations plot (Note: the
8991
9092
9193
92-
9394
.. parsed-literal::
9495
95-
<spikeinterface.widgets.unit_locations.UnitLocationsWidget at 0x17f719cd0>
96+
<spikeinterface.widgets.unit_locations.UnitLocationsWidget at 0x147bda550>
9697
9798
9899
@@ -107,7 +108,7 @@ to do this is to set up your figure and axes first, then tell
107108
``SpikeInterface`` which axes it should attach the widget plot to.
108109
Here’s an example of making a unit summary plot.
109110

110-
.. code:: ipython3
111+
.. code::
111112
112113
import matplotlib.pyplot as plt
113114
fig, axs = plt.subplots(ncols=2, nrows=1)

examples/how_to/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ with `nbconvert`. Here are the steps (in this example for the `get_started`):
1414

1515
```
1616
>>> jupytext --to notebook get_started.py
17-
>>> jupytext --set-formats ipynb,py get_started.ipynb
17+
>>> jupytext --set-formats ipynb.py get_started.ipynb
1818
```
1919

2020
2. Run the notebook
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)