-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
[Bug]: subfigures messes up with fig.legend zorder #22011
Comments
It is true that subfigures are drawn after other figure artists, and hence will cover the figure artists. Subfugures are new enough that I think we could easily change the order - it does seem that if someone adds a figure-level artist they would want it to show up above any subfigures. |
Are there any updates or workaround? I have tried changing zorder on the figure's legend, but it does not work. |
Good first issue - notes for new contributorsThis issue is suited to new contributors because it does not require understanding of the Matplotlib internals. To get started, please see our contributing guide. We do not assign issues. Check the Development section in the sidebar for linked pull requests (PRs). If there are none, feel free to start working on it. If there is an open PR, please collaborate on the work by reviewing it rather than duplicating it in a competing PR. If something is unclear, please reach out on any of our communication channels. |
There is no work around, currently the sub-figures do not participate in the z-order matplotlib/lib/matplotlib/figure.py Lines 3100 to 3105 in 0f3f768
matplotlib/lib/matplotlib/figure.py Lines 2292 to 2302 in 0f3f768
The fix is to add them to the artists returned by I think this is a good first issue as it only requires understanding a few methods in the |
If there is no rush, I'd like to get started on this as my first contribution to |
@cgadal please go ahead! Our contributors’ guide is here: |
I've made the following changes so far: main...cgadal:matplotlib:subfigure-zorder
This seems to be enough to solve this issue, and the user can properly control the import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1, 10, 10)
y1 = x
y2 = -x
fig = plt.figure(constrained_layout=True)
subfigs = fig.subfigures(nrows=1, ncols=2)
for subfig in subfigs:
axarr = subfig.subplots(2, 1)
for ax in axarr.flatten():
(l1,) = ax.plot(x, y1, label="line1")
(l2,) = ax.plot(x, y2, label="line2")
subfigs[0].set_zorder(6)
l = fig.legend(handles=[l1, l2], loc="upper center", ncol=2)
plt.savefig("subfigures_figlegend2.png", dpi=200)
plt.show() Remaining question:
|
Not off the top of my head. I think subfigures is relatively well tested so if this breaks something we will know. |
@tacaswell Done here: #26926 . Also, current test are running fine, and I added a Whats new entry note. I'm still unsure about what to add ad an API change note though. |
Closes #22011: Changes to SubFigures so it behaves like a regular artist
Bug summary
When using subfigures, legends called with
fig.legend()
are hidden behind the subfigure background and the axes background.Code for reproduction
Actual outcome
With
ax.set_facecolor('none')
andsubfig.set_facecolor('none')
commented:With
ax.set_facecolor('none')
andsubfig.set_facecolor('none')
not commented, i.e transparent backgrounds:Expected outcome
Legend should be on the top of the backgrounds, as produced when building the figure with
plt.subplots()
:Additional information
ax.legend()
andsubfig.legend()
.Operating system
Ubuntu 20
Matplotlib Version
3.5.0
Matplotlib Backend
QtAgg
Python version
3.8.10
Jupyter version
No response
Installation
pip
The text was updated successfully, but these errors were encountered: