Visitar URL original
[Bug]: subplot_mosaic bug with empty_sentinel and sharey=True · Issue #25417 · matplotlib/matplotlib · GitHub
The Wayback Machine - https://web.archive.org/web/20230311113758/https://github.com/matplotlib/matplotlib/issues/25417
Skip to content
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]: subplot_mosaic bug with empty_sentinel and sharey=True #25417

Open
apicco opened this issue Mar 9, 2023 · 12 comments
Open

[Bug]: subplot_mosaic bug with empty_sentinel and sharey=True #25417

apicco opened this issue Mar 9, 2023 · 12 comments

Comments

@apicco
Copy link

apicco commented Mar 9, 2023

Bug summary

When an empty sentinel is used at the beginning of a row, with the sharey option True, the tick labelling on the y-axis isn't shown.

Code for reproduction

from matplotlib import pyplot as plt
import numpy as np

data = [ np.array( [ i for i in range( 100 ) ] ) , np.random.random( 100 ) ]

fig1 = plt.figure()

# create a mosaic with an empty_sentinel 
# at the END of the row. 
# 'sharey=True' works.
subplots = fig1.subplot_mosaic(
        """
        AB.
        CDE
        """ , sharex = True , sharey = True )

subplots[ "A" ].plot( data[0] , data[1] )
subplots[ "B" ].plot( data[0] , data[1] )
subplots[ "C" ].plot( data[0] , data[1] )
subplots[ "D" ].plot( data[0] , data[1] )
subplots[ "E" ].plot( data[0] , data[1] )

subplots[ "C" ].set_xlabel( 'A variable' )
subplots[ "D" ].set_xlabel( 'A variable' )
subplots[ "E" ].set_xlabel( 'A variable' )

subplots[ "A" ].set_ylabel( 'Some data' )
subplots[ "C" ].set_ylabel( 'Some data' )

plt.savefig( 'mosaic_working.pdf' )

# create a mosaic with an empty_sentinel 
# at the start of the row. 
# 'sharey=True' DOESN'T work: the y ticks
# are missing.
fig2 = plt.figure()

subplots = fig2.subplot_mosaic(
        """
        .AB
        CDE
        """ , sharey = True )

subplots[ "A" ].plot( data[0] , data[1] )
subplots[ "B" ].plot( data[0] , data[1] )
subplots[ "C" ].plot( data[0] , data[1] )
subplots[ "D" ].plot( data[0] , data[1] )
subplots[ "E" ].plot( data[0] , data[1] )

subplots[ "C" ].set_xlabel( 'A variable' )
subplots[ "D" ].set_xlabel( 'A variable' )
subplots[ "E" ].set_xlabel( 'A variable' )

subplots[ "A" ].set_ylabel( 'Some data' )
subplots[ "C" ].set_ylabel( 'Some data' )

plt.savefig( 'mosaic_not_working.pdf' )

Actual outcome

mosaic_working.pdf
mosaic_not_working.pdf

Expected outcome

The y-axis in the first subplot of the top row should have y ticks.

Additional information

No response

Operating system

OS

Matplotlib Version

3.7.0

Matplotlib Backend

No response

Python version

3.11

Jupyter version

No response

Installation

pip

@oscargus
Copy link
Contributor

oscargus commented Mar 9, 2023

This comes from

if not ss.is_first_col(): # Remove left label/ticklabels/offsettext.

as is_first_column will correctly say that it is not the first column...

The subplotspec is not enough to determine this, but one will have to look at the GridSpec instead. However, I am not sure that the GridSpec has access to the original subplot_mosaic information.

A similar issue is seen if you drop the "E" entry and use sharex=True.

@jklymak
Copy link
Member

jklymak commented Mar 9, 2023

This is a feature request rather than a bug per-se. We don't have any mechanism for deciding post facto if a general axes has any axes to its left or below.

@apicco
Copy link
Author

apicco commented Mar 9, 2023

But the ylabel (or xlabel) is assigned correctly. Could the axis label share the same mechanism for assigning axes ticks? (just throwing out the thought... I hope it helps). Thanks for looking into it.

@oscargus
Copy link
Contributor

oscargus commented Mar 9, 2023

You set the ylabel on the "A"-Axes, so that should work (you can e.g. set it on the "B" axes as well). The problem is that the "A"-Axes doesn't know that it is the left-most as the only current way to determine that relies on it being in column 0 (while it is currently in column 1, but doesn't know that there is nothing in column 0).

@jklymak
Copy link
Member

jklymak commented Mar 9, 2023

Just to be clear, just as you manually set the ylabel for "A" you can also change the tick params to show the tick labels manually:

subplots["A"].tick_params(labelleft=True)

@tacaswell
Copy link
Member

I am going to label this as "medium difficulty" as it will require reading and understanding some esoteric part of the code base and developing a robust way to detect the "outer most Axes", but "good first issue" as there is no public API design and should be easy to test (both the dense case and the sparse case).

This has come up a number of times (but I have failed to find the linked issue, I know at BNL we have some special logic to put the labels on the second to last row if the bottom row is not visible...slightly easier case because we know it is the dense case and we are the ones making some of the bottom row invisible) so there is definitely a desire for this sort of functionality and a user expectation that "remove all but outer" should mean "outer most visible Axes".

I think @oscargus correctly identified that any of this logic should go in _label_outer_*axis on the Axes object (at least as a first theory).

@tacaswell tacaswell added this to the future releases milestone Mar 9, 2023
@tacaswell tacaswell added Difficulty: Medium Good first issue Open a pull request against these issues if there are no active ones! labels Mar 9, 2023
@jklymak
Copy link
Member

jklymak commented Mar 9, 2023

The typical way to do this, before subplot_mosaic was to remove the sentinel. At that point the axes labeling had already been setup, and if we wanted to decide "outer" again accounting for the removal we would need to call something to do so. With subplot mosaic having sentinels, I suppose it's possible to decide outer before the user gets the axes, but I think this would need to happen independently of the general "outer" logic that we used before.

More fundamentally, I'm not sure this behaviour is universally desirable. For instance:

fig, subplots = plt.subplot_mosaic(
        """
        ABC
        DEF
        GH.
        """ , sharex = True, sharey=True, layout='constrained')

gives
Share
but if we label F subplots['F'].tick_params(labelbottom=True)

Share2

which in my opinion looks pretty bad. I guess you could argue that constrained layout shouldn't add the extra vertical space, but thats not how CL works.

@tacaswell tacaswell removed the Good first issue Open a pull request against these issues if there are no active ones! label Mar 9, 2023
@tacaswell
Copy link
Member

Removed good first issue as there is not consensus we want this ;)

I think the issue with CL is there independent of how automatic we make the removal.

Maybe the best option is this case is to turn on all of the tick labels and still only remove them on the outer most rows / columns?

@oscargus
Copy link
Contributor

oscargus commented Mar 9, 2023

It is quite easy to come up with weird cases where it is not obvious what the "correct" solution looks like when it comes to labeling, e.g.

fig, subplots = plt.subplot_mosaic(
        """
        ABB
        .BB
        CD.
        """ , sharex = True, sharey = True)

So maybe the solution is to discuss this a bit in the documentation and in an example?

@jklymak
Copy link
Member

jklymak commented Mar 9, 2023

"sharex/y" means two things: link the axes limits, so interactive use makes the axes track one another.

Additionally for grids this has meant removing inner tick labels

I personally consider it bad API that we do both with the same kwarg, but that ship sailed many years ago. I personally feel that for any special layouts (empty axes), we should just let people choose to label or not by hand.

@anntzer
Copy link
Contributor

anntzer commented Mar 10, 2023

IMO the current behavior is correct (e.g. from @jklymak's example, or from @oscargus's point that it is tricky to formulate an otherwise generally correct rule) and agree with @jklymak that we should just ask users to label by hand if the current behavior does not suit them.

@tacaswell
Copy link
Member

ok, I am convinced this is not a change we want to make.

I think the action here is to add an example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants