Skip to content

reverse order in which stackplot elements are added to axes - #19591

Closed
pseyfert wants to merge 2 commits into
matplotlib:mainfrom
pseyfert:stackplot-order
Closed

pseyfert wants to merge 2 commits into
matplotlib:mainfrom
pseyfert:stackplot-order

Conversation

@pseyfert

@pseyfert pseyfert commented Feb 26, 2021 •

Copy link
Copy Markdown
Contributor

PR Summary

This will does not alter the order in which stackplot elements are show
on the axes, colors are used, labels are assigned, etc. It is only
(meant to) change the order in which handles are stored in the axes
object and thus in which legend entries appear by default. With this
change the top most entry in the legend should be the top most plotted
stack plot component.

In an example (derived from https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/stackplot_demo.html)

import numpy as np
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [1, 1, 2, 3, 5]
y2 = [0, 4, 2, 6, 8]
y3 = [1, 3, 5, 7, 9]

y4 = [23, 23, 23, 24, 25]

y = np.vstack([y1, y2, y3])

labels = ["Fibonacci ", "Evens", "Odds"]

fig, ax = plt.subplots()
ax.plot(x, y4, label="staying above")
ax.stackplot(x, y1, y2, y3, labels=labels)
ax.legend(loc='upper left')
plt.show()

In the example, the bottom most component of the stackplot is 'Fibonacci', yet by default 'Fibonacci' will be the top most entry in the legend. This reverse order causes mental overhead when reading a plot and becomes even more problematic for colorblind readers or once the color pallet is cycled through more than once.

I find the example in https://codeyarns.com/tech/2019-09-12-how-to-reverse-order-of-legend-in-matplotlib.html insufficient to address that inconvenience as the fix is needed for every usage and breaks in the example as I only want to affect the order of stackplot elements but the 'staying above' entry should remain where it is in the legend.

I enabled "Allow edits by maintainers".

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

pytest error

==================================================================================================================== test session starts ====================================================================================================================
platform linux -- Python 3.9.1+, pytest-6.0.2, py-1.10.0, pluggy-0.13.0
rootdir: /tmp/mpl/src/matplotlib, configfile: pytest.ini, testpaths: lib
plugins: flake8-1.0.6
collected 0 items / 1 error                                                                                                                                                                                                                                 

========================================================================================================================== ERRORS ===========================================================================================================================
_______________________________________________________________________________________________________________ ERROR collecting test session _______________________________________________________________________________________________________________
/usr/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1030: in _gcd_import
    ???
<frozen importlib._bootstrap>:1007: in _find_and_load
    ???
<frozen importlib._bootstrap>:972: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:228: in _call_with_frames_removed
    ???
<frozen importlib._bootstrap>:1030: in _gcd_import
    ???
<frozen importlib._bootstrap>:1007: in _find_and_load
    ???
<frozen importlib._bootstrap>:972: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:228: in _call_with_frames_removed
    ???
<frozen importlib._bootstrap>:1030: in _gcd_import
    ???
<frozen importlib._bootstrap>:1007: in _find_and_load
    ???
<frozen importlib._bootstrap>:986: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:680: in _load_unlocked
    ???
<frozen importlib._bootstrap_external>:790: in exec_module
    ???
<frozen importlib._bootstrap>:228: in _call_with_frames_removed
    ???
lib/matplotlib/__init__.py:107: in <module>
    from . import _api, cbook, docstring, rcsetup
lib/matplotlib/cbook/__init__.py:31: in <module>
    from matplotlib import _api, _c_internal_utils
E   ImportError: cannot import name '_c_internal_utils' from partially initialized module 'matplotlib' (most likely due to a circular import) (/tmp/mpl/src/matplotlib/lib/matplotlib/__init__.py)
================================================================================================================== short test summary info ==================================================================================================================
ERROR  - ImportError: cannot import name '_c_internal_utils' from partially initialized module 'matplotlib' (most likely due to a circular import) (/tmp/mpl/src/matplotlib/lib/matplotlib/__init__.py)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
===================================================================================================================== 1 error in 0.18s ======================================================================================================================

@jklymak

jklymak commented Feb 26, 2021

Copy link
Copy Markdown
Member

This seems reasonable to me. However, you will need to make the tests pass, and this is a breaking change (i.e. imagine someone has used the workaround, now their plot legend will be wrong) so we need to have some sort of deprecation path.

@jklymak
jklymak marked this pull request as draft February 26, 2021 21:04
@tacaswell tacaswell added this to the v3.5.0 milestone Feb 28, 2021
@tacaswell tacaswell added the API: changes Changes to the public API, typically requiring deprecation. label Feb 28, 2021
@pseyfert

pseyfert commented Mar 1, 2021

Copy link
Copy Markdown
Contributor Author

okay, eventually figured out how to run the tests. is there any established transition/deprecation path in matplotlib?

@jklymak

jklymak commented Mar 1, 2021

Copy link
Copy Markdown
Member

I think you'll have to do a song and dance with a kwarg that default to the old behavior but warns, and then defaults to the new behavior? Unless this is so obviously desirable that the old way was a bug, in which case we could maybe just have a kwarg to give the old behavior.

@QuLogic QuLogic modified the milestones: v3.5.0, v3.6.0 Aug 21, 2021
@pseyfert

Copy link
Copy Markdown
Contributor Author

Sorry for the long silence.

I have an image to illustrate the original use case. In a stack plot like this, where colors get re-used, we were wondering "are the thick bands entries 1 and 2 or 9 and 10?". The plot is with my change and now in examples/lines_bars_and_markers/stackplot_demo.py.
stack_example

Talking to a few colleagues it's not clear to me that breaking the current behaviour is desired. While on the use case example, the updated ordering is appreciated, some also said the current behaviour would be obvious and expected. The name of the new option 'top_to_bottom' is up for discussion. It may seem clear from what it does internally (plot the areas top to bottom or bottom to top) but doesn't capture well what the observable change for a user would be.

(Going through the checklist from the template now)

@pseyfert
pseyfert marked this pull request as ready for review December 28, 2021 17:11
@jklymak
jklymak marked this pull request as draft February 1, 2022 08:32
@jklymak

jklymak commented Feb 1, 2022

Copy link
Copy Markdown
Member

I'm popping to draft as this needs to pass the tests. However, from the comment above, its not clear if this change is desired?

@pseyfert

Copy link
Copy Markdown
Contributor Author

Imo the feature is desired, I'm bodging around it in production. If other users will appreciate turning it on-by-default or if off-by-default is a better setting is unclear to me.

@QuLogic QuLogic modified the milestones: v3.6.0, v3.7.0 Jul 5, 2022
@pseyfert
pseyfert marked this pull request as ready for review October 21, 2022 18:49
@pseyfert

Copy link
Copy Markdown
Contributor Author

okay, found the button to remove draft status.

let me just point out atm the branch is such that the default is unchanged.

This will does not alter the order in which stackplot elements are show
on the axes, colors are used, labels are assigned, etc. It is only
(meant to) change the order in which handles are stored in the axes
object and thus in which legend entries appear by default. With this
change the top most entry in the legend should be the top most plotted
stack plot component.
@tacaswell tacaswell added New feature and removed status: needs revision status: needs documentation API: changes Changes to the public API, typically requiring deprecation. labels Oct 21, 2022
tacaswell
tacaswell previously approved these changes Oct 21, 2022

@tacaswell tacaswell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could 🚲 🏠 the bottom_area and not_bottom_areas internal function names a bit, but I do not think it is worth holding over that.

@tacaswell

tacaswell commented Oct 21, 2022 •

Copy link
Copy Markdown
Member

@pseyfert Looks good! I added / removed a bunch of labels, I think they are now correct (no API change, has docs, has a test, and is a new feature).

@timhoffm timhoffm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation is good.

I'm not quite happy with the name top_to_bottom. It's unclear what this means. My first intuition on that name was, that for data [y1, y2, y3] it will plot y1 above y2 above y3.

Maybe something like draw_top_first? Name suggestions welcome.

above the other plot components in the legend. That is, for
``stackplot(x, y1, y2, y3)``, the legend will contain y3 above y2 and
y2 above y1.
The default behaviour is plotting the bottom area first.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The default behaviour is plotting the bottom area first.
The default behaviour is plotting the bottom area first.
.. versionadded:: 3.7

.. code-block:: python

from matplotlib import pyplot as plt
plt.x = [1, 2, 3, 4, 5]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
plt.x = [1, 2, 3, 4, 5]
x = [1, 2, 3, 4, 5]


The order of entries in a legend of a `~matplotlib.axes.Axes.stackplot` can be inverted with the option ``top_to_bottom``. In the below example, a viewer would see the area for ``y3`` above ``y2``, and ``y2`` above ``y1``. Here the legend will be generated in the same way such that ``y3`` appears above ``y2`` and ``y2`` above ``y1``.

.. code-block:: python

@tacaswell tacaswell Nov 23, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. code-block:: python
.. plot::

@tacaswell
tacaswell dismissed their stale review November 23, 2022 21:44

On further discussion, I think this needs a bit more thought.

@QuLogic

QuLogic commented Nov 23, 2022

Copy link
Copy Markdown
Member

Maybe something like draw_top_first? Name suggestions welcome.

If it helps come up with names, it appears that the same problem occurs with stacked histograms. For example, if you add a legend to the histogram with multiple data sets example, then you get the following:
Figure_1

As you can see, the side-by-side plots appear to be correct (at least for LTR readings). Somehow the stack step is 'right', but stacked bar is reversed. We will likely want to fix these at the same time (though not necessarily the same PR), so a name that works for both would be good.

@story645

story645 commented Nov 23, 2022 •

Copy link
Copy Markdown
Member

We will likely want to fix these at the same time (though not necessarily the same PR), so a name that works for both would be good.

Did a quick look and plotly and ggplot both put this kinda thing as an attribute on/method to legend, and they name it traceorder ('cause handles are traces in plotly) which is a string or reverse = True, and I wonder if something general like label/legend/handle_order_reverse would work?

@timhoffm

timhoffm commented Nov 24, 2022 •

Copy link
Copy Markdown
Member

Did a quick look and plotly and ggplot both put this kinda thing as an attribute on/method to legend, and they name it traceorder ('cause handles are traces in plotly) which is a string or reverse = True, and I wonder if something general like label/legend/handle_order_reverse would work?

Wow, great thinking! 🥇 We should definitively have that option on legends. It's not only solving the issue for all the above cases and e.g. scatter(), it's also more intuitive to place the functionality at the legend as the visible element affected rather than at the plotting function - while the plotting function (and more generally the order of commands) defines an initial order, it's almost natural that the legend should have the capability to rearrange the order.

I would even go so far to say that we don't need such capability on the individual plotting functions.

@jklymak

jklymak commented Nov 24, 2022

Copy link
Copy Markdown
Member

a) it would be nice if the draw order were consistent. b) I can understand where the OP is coming from in wanting the legend to be stacked the same way the histograms would be stacked, from top to bottom. However, I would personally expect the opposite, where the first entry of the legend to be the bottommost in the stack. Perhaps more obviously, if the stacked bars were instead horizontal, I'd expect the top entry of the legend to correspond to the left-most element in the stack.

Given that there is ambiguity in what people would expect, I'm personally somewhat in favour of this just being a legend keyword helper that basically does handles=handles[::-1]; labels=labels[::-1] for the user. I'm particularly not in favour of changing the draw order given that I assume the same code is reused for horizontal stacking.

@timhoffm timhoffm mentioned this pull request Dec 13, 2022
@timhoffm

Copy link
Copy Markdown
Member

I think we have reached consensus that this should be realized via a legend parameter. I've opened a new issue for that: #24712

@pseyfert Thanks for your contribution! Even though we did not take your implementation, it inspired us how we can make this a more general feature in the legend API.

@timhoffm timhoffm closed this Dec 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants