diff --git a/brian2tools/plotting/base.py b/brian2tools/plotting/base.py index 28bb385d..83749c15 100644 --- a/brian2tools/plotting/base.py +++ b/brian2tools/plotting/base.py @@ -106,10 +106,7 @@ def brian_plot(brian_obj, kwds['rate_unit'] = _get_best_unit(smooth_rate) return plot_rate(brian_obj.t, smooth_rate, axes=axes, **kwds) elif isinstance(brian_obj, Morphology): - if kwds: - logger.warn('plot_dendrogram does not take any additional keyword ' - 'arguments, ignoring them.') - return plot_dendrogram(brian_obj, axes=axes) + return plot_dendrogram(brian_obj, axes=axes, **kwds) elif isinstance(brian_obj, Synapses): if len(brian_obj) == 0: raise TypeError('Synapses object does not have any synapses.') diff --git a/brian2tools/plotting/morphology.py b/brian2tools/plotting/morphology.py index 3bba0bab..dce55518 100644 --- a/brian2tools/plotting/morphology.py +++ b/brian2tools/plotting/morphology.py @@ -352,7 +352,7 @@ def plot_morphology(morphology, plot_3d=None, show_compartments=False, return axes -def plot_dendrogram(morphology, axes=None): +def plot_dendrogram(morphology, axes=None, **kwds): ''' Plot a "dendrogram" of a morphology, i.e. an abstract representation which visualizes the branching structure and the length of each section. @@ -365,6 +365,12 @@ def plot_dendrogram(morphology, axes=None): The `~matplotlib.axes.Axes` instance used for plotting. Defaults to ``None`` which means that a new `~matplotlib.axes.Axes` will be created for the plot. + **kwds + Any additional keyword arguments are passed to matplotlib's + `~matplotlib.axes.Axes.plot`, `~matplotlib.axes.Axes.vlines`, and + `~matplotlib.axes.Axes.hlines` calls. Only arguments accepted by all + three functions should be used (e.g. ``color``, ``alpha``, + ``linewidth``). Returns ------- @@ -424,9 +430,12 @@ def plot_dendrogram(morphology, axes=None): x_values = (np.array(min_index) + np.array(max_index)) / 2.0 + clip_on = kwds.pop('clip_on', False) + lw = kwds.pop('lw', kwds.pop('linewidth', 2)) + # Plot the dendogram with lengths of the vertical lines representing the # total distance to the root - plt.plot(x_values[0], length_metric[0], 'ko', clip_on=False) + plt.plot(x_values[0], length_metric[0], marker='o', clip_on=clip_on, **kwds) for sec, (x, depth) in enumerate(zip(x_values, length_metric)): child_start_idx = (sec+1)*max_children num_children = flat_morpho.morph_children_num[sec+1] @@ -434,8 +443,8 @@ def plot_dendrogram(morphology, axes=None): child_indices = children[child_start_idx:child_start_idx+num_children] child_depth = length_metric[child_indices-1] child_x = x_values[child_indices-1] - axes.vlines(child_x, depth, child_depth, clip_on=False, lw=2) - axes.hlines(depth, min(child_x), max(child_x), lw=2) + axes.vlines(child_x, depth, child_depth, clip_on=clip_on, lw=lw, **kwds) + axes.hlines(depth, min(child_x), max(child_x), lw=lw, **kwds) axes.set_xticks([]) axes.set_ylabel('distance from root (um)') axes.set_xlim(-1, terminal_counter) diff --git a/brian2tools/tests/test_plotting.py b/brian2tools/tests/test_plotting.py index 1b6fd085..e1ec6a29 100644 --- a/brian2tools/tests/test_plotting.py +++ b/brian2tools/tests/test_plotting.py @@ -122,6 +122,20 @@ def test_plot_morphology(): ax = plot_dendrogram(morpho) assert isinstance(ax, matplotlib.axes.Axes) plt.close() + # Test that styling kwargs are accepted and forwarded without error + ax = plot_dendrogram(morpho, color='red') + assert isinstance(ax, matplotlib.axes.Axes) + plt.close() + ax = plot_dendrogram(morpho, color='blue', alpha=0.5) + assert isinstance(ax, matplotlib.axes.Axes) + plt.close() + ax = plot_dendrogram(morpho, color='green', linewidth=3) + assert isinstance(ax, matplotlib.axes.Axes) + plt.close() + # brian_plot routes Morphology to plot_dendrogram, so kwargs must work there too + ax = brian_plot(morpho, color='purple') + assert isinstance(ax, matplotlib.axes.Axes) + plt.close() ax = plot_morphology(morpho) assert isinstance(ax, matplotlib.axes.Axes) plt.close() diff --git a/dev/doc_tools/plotting_examples/morphology_plots.py b/dev/doc_tools/plotting_examples/morphology_plots.py index 9b8f9232..f99015c8 100644 --- a/dev/doc_tools/plotting_examples/morphology_plots.py +++ b/dev/doc_tools/plotting_examples/morphology_plots.py @@ -13,6 +13,12 @@ brian_plot(morpho) savefig(os.path.join(fig_dir, 'plot_dendrogram.svg')) close() + +figure() +plot_dendrogram(morpho, color='red', alpha=0.7) +savefig(os.path.join(fig_dir, 'plot_dendrogram_custom.svg')) +close() + plot_morphology(morpho) # 3d plot mayavi.show() figure() diff --git a/docs_sphinx/images/plot_dendrogram_custom.svg b/docs_sphinx/images/plot_dendrogram_custom.svg new file mode 100644 index 00000000..3193a3fc --- /dev/null +++ b/docs_sphinx/images/plot_dendrogram_custom.svg @@ -0,0 +1,1372 @@ + + + + + + + + 2026-03-27T12:49:53.629811 + image/svg+xml + + + Matplotlib v3.10.8, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs_sphinx/user/plotting.rst b/docs_sphinx/user/plotting.rst index 0b8420f4..2e87c2b2 100644 --- a/docs_sphinx/user/plotting.rst +++ b/docs_sphinx/user/plotting.rst @@ -229,8 +229,18 @@ dendogram:: .. image:: ../images/plot_dendrogram.svg The `~brian2tools.plotting.morphology.plot_dendrogram` function does the same thing, but in contrast to the other -plot functions it does not allow any customization at the moment, so there is no benefit over using -`~brian2tools.plotting.base.brian_plot`. +plot functions it does not allow any customization that is not also available via +`~brian2tools.plotting.base.brian_plot`. Both functions accept additional keyword arguments (e.g. ``color``, +``alpha``, ``linewidth``) that are forwarded to the underlying `~matplotlib.axes.Axes.plot`, +`~matplotlib.axes.Axes.vlines`, and `~matplotlib.axes.Axes.hlines` calls:: + + plot_dendrogram(morpho, color='red', alpha=0.7) + +.. image:: ../images/plot_dendrogram_custom.svg + +The same customization is also possible via `~brian2tools.plotting.base.brian_plot`:: + + brian_plot(morpho, color='red', alpha=0.7) .. _plotting_morphologies: