Repository navigation
reverse order in which stackplot elements are added to axes #19591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||
| Order of legend entries in stackplots | ||||||
| ------------------------------------- | ||||||
|
|
||||||
| 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 | ||||||
|
|
||||||
| from matplotlib import pyplot as plt | ||||||
| plt.x = [1, 2, 3, 4, 5] | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| 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, top_to_bottom=True) | ||||||
| ax.legend(loc='upper left') | ||||||
| plt.show() | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,7 +16,7 @@ | |||||||||
|
|
||||||||||
|
|
||||||||||
| def stackplot(axes, x, *args, | ||||||||||
| labels=(), colors=None, baseline='zero', | ||||||||||
| labels=(), top_to_bottom=False, colors=None, baseline='zero', | ||||||||||
| **kwargs): | ||||||||||
| """ | ||||||||||
| Draw a stacked area plot. | ||||||||||
|
|
@@ -30,7 +30,7 @@ def stackplot(axes, x, *args, | |||||||||
| calls is legal:: | ||||||||||
|
|
||||||||||
| stackplot(x, y) # where y has shape (M, N) | ||||||||||
| stackplot(x, y1, y2, y3) # where y1, y2, y3, y4 have length N | ||||||||||
| stackplot(x, y1, y2, y3) # where y1, y2, y3 have length N | ||||||||||
|
|
||||||||||
| baseline : {'zero', 'sym', 'wiggle', 'weighted_wiggle'} | ||||||||||
| Method used to calculate the baseline: | ||||||||||
|
|
@@ -47,6 +47,14 @@ def stackplot(axes, x, *args, | |||||||||
| A sequence of labels to assign to each data series. If unspecified, | ||||||||||
| then no labels will be applied to artists. | ||||||||||
|
|
||||||||||
| top_to_bottom : bool, optional | ||||||||||
| When true, the top most component of the stackplot will be plotted | ||||||||||
| first. This has the effect that the top most plot component will appear | ||||||||||
| 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. | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| colors : list of color, optional | ||||||||||
| A sequence of colors to be cycled through and used to color the stacked | ||||||||||
| areas. The sequence need not be exactly the same length as the number | ||||||||||
|
|
@@ -70,11 +78,12 @@ def stackplot(axes, x, *args, | |||||||||
|
|
||||||||||
| y = np.row_stack(args) | ||||||||||
|
|
||||||||||
| labels = iter(labels) | ||||||||||
| iter_method = reversed if top_to_bottom else iter | ||||||||||
| labels = iter_method(labels) | ||||||||||
| if colors is not None: | ||||||||||
| colors = itertools.cycle(colors) | ||||||||||
| colors = itertools.cycle(iter_method(colors)) | ||||||||||
| else: | ||||||||||
| colors = (axes._get_lines.get_next_color() for _ in y) | ||||||||||
| colors = iter_method(list(axes._get_lines.get_next_color() for _ in y)) | ||||||||||
|
|
||||||||||
| # Assume data passed has not been 'stacked', so stack it here. | ||||||||||
| # We'll need a float buffer for the upcoming calculations. | ||||||||||
|
|
@@ -111,17 +120,29 @@ def stackplot(axes, x, *args, | |||||||||
| first_line = center - 0.5 * total | ||||||||||
| stack += first_line | ||||||||||
|
|
||||||||||
| # Color between x = 0 and the first array. | ||||||||||
| coll = axes.fill_between(x, first_line, stack[0, :], | ||||||||||
| facecolor=next(colors), label=next(labels, None), | ||||||||||
| **kwargs) | ||||||||||
| coll.sticky_edges.y[:] = [0] | ||||||||||
| r = [coll] | ||||||||||
|
|
||||||||||
| # Color between array i-1 and array i | ||||||||||
| for i in range(len(y) - 1): | ||||||||||
| r.append(axes.fill_between(x, stack[i, :], stack[i + 1, :], | ||||||||||
| facecolor=next(colors), | ||||||||||
| label=next(labels, None), | ||||||||||
| **kwargs)) | ||||||||||
| return r | ||||||||||
| r = [] | ||||||||||
|
|
||||||||||
| def bottom_area(): | ||||||||||
| # Color between x = 0 and the first array. | ||||||||||
| coll = axes.fill_between(x, first_line, stack[0, :], | ||||||||||
| facecolor=next(colors), | ||||||||||
| label=next(labels, None), | ||||||||||
| **kwargs) | ||||||||||
| coll.sticky_edges.y[:] = [0] | ||||||||||
| r.append(coll) | ||||||||||
|
|
||||||||||
| def not_bottom_areas(): | ||||||||||
| # Color between array i-1 and array i | ||||||||||
| for i in iter_method(range(len(y) - 1)): | ||||||||||
| r.append(axes.fill_between(x, stack[i, :], stack[i + 1, :], | ||||||||||
| facecolor=next(colors), | ||||||||||
| label=next(labels, None), | ||||||||||
| **kwargs)) | ||||||||||
| if top_to_bottom: | ||||||||||
| not_bottom_areas() | ||||||||||
| bottom_area() | ||||||||||
| else: | ||||||||||
| bottom_area() | ||||||||||
| not_bottom_areas() | ||||||||||
|
|
||||||||||
| return list(iter_method(r)) | ||||||||||
Binary file added
BIN
+27.6 KB
lib/matplotlib/tests/baseline_images/test_legend/legend_stackplot_sorted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.