kymata-atlas / kymata-core

Core Kymata codebase, including statistical analysis and plotting tools
https://kymata.org
MIT License
5 stars 0 forks source link

Legend not working when using "show_only" argument with expression plot #241

Closed neukym closed 6 months ago

neukym commented 6 months ago

Recent improvements in plotting have lead to a breaking change when using the show_only argument in expression_plot() - seemingly causing causing problems with the legend - resulting in a runtime error:

Error trace:

  File "/Users/administration/Github/kymata-toolbox/.venv/lib/python3.11/site-packages/matplotlib/legend_handler.py", line 411, in _default_update_prop
    color = orig_handle.get_colors()[0]
            ~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: index 0 is out of bounds for axis 0 with size 0

Commenting out the legend is a temporay fix. Assigning to @caiw.

Full stack trace:

/Users/administration/Github/kymata-toolbox/.venv/bin/python /Users/administration/Github/kymata-toolbox/invokers/invoker_run_nkg_plotting.py 
Traceback (most recent call last):
  File "/Users/administration/Github/kymata-toolbox/invokers/invoker_run_nkg_plotting.py", line 14, in <module>
    expression_plot(expression_data[
  File "/Users/administration/Github/kymata-toolbox/kymata/plot/plot.py", line 281, in expression_plot
    leg = bottom_ax.legend(labels=not_shown, fontsize="x-small", alignment="left",
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/administration/Github/kymata-toolbox/.venv/lib/python3.11/site-packages/matplotlib/axes/_axes.py", line 323, in legend
    self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/administration/Github/kymata-toolbox/.venv/lib/python3.11/site-packages/matplotlib/legend.py", line 563, in __init__
    self._init_legend_box(handles, labels, markerfirst)
  File "/Users/administration/Github/kymata-toolbox/.venv/lib/python3.11/site-packages/matplotlib/legend.py", line 913, in _init_legend_box
    handle_list.append(handler.legend_artist(self, orig_handle,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/administration/Github/kymata-toolbox/.venv/lib/python3.11/site-packages/matplotlib/legend_handler.py", line 129, in legend_artist
    artists = self.create_artists(legend, orig_handle,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/administration/Github/kymata-toolbox/.venv/lib/python3.11/site-packages/matplotlib/legend_handler.py", line 424, in create_artists
    self.update_prop(legline, orig_handle, legend)
  File "/Users/administration/Github/kymata-toolbox/.venv/lib/python3.11/site-packages/matplotlib/legend_handler.py", line 88, in update_prop
    self._update_prop(legend_handle, orig_handle)
  File "/Users/administration/Github/kymata-toolbox/.venv/lib/python3.11/site-packages/matplotlib/legend_handler.py", line 79, in _update_prop
    self._default_update_prop(legend_handle, orig_handle)
  File "/Users/administration/Github/kymata-toolbox/.venv/lib/python3.11/site-packages/matplotlib/legend_handler.py", line 411, in _default_update_prop
    color = orig_handle.get_colors()[0]
            ~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: index 0 is out of bounds for axis 0 with size 0

Process finished with exit code 1
caiw commented 6 months ago

Assigning to @caiw.

👍

caiw commented 6 months ago

@neukym I can't reproduce this. E.g. running the demo_plotting.ipynb, which features use of show_only, runs without error. Can you send an example where it breaks?

neukym commented 6 months ago

I'll send it via teams

caiw commented 6 months ago

Now reproduced with example Andy sent.

caiw commented 6 months ago

Error occurs where a function is both explicitly hidden and also explicitly given a colour. ~Easy fix!~ Not an immediately straightforward fix!