Skip to content

[Bug]: Setting norm by string doesn't work for hexbin #28105

Closed

Description

@anntzer

Bug summary

In #20752 I added support for imshow(..., norm="log"), and documented that the feature is supported in particular by hexbin as well; but this doesn't actually work for hexbin, because hexbin tries to run stuff on the norm before normalizing strings.

Code for reproduction

from pylab import *
hexbin(rand(100), rand(100), norm="log")

Actual outcome

  File ".../matplotlib/axes/_axes.py", line 5247, in hexbin
    if norm.vmin is None and norm.vmax is None:
       ^^^^^^^^^
AttributeError: 'str' object has no attribute 'vmin'

Expected outcome

no error

Additional information

Probably the block

        # autoscale the norm with current accum values if it hasn't been set
        if norm is not None:
            if norm.vmin is None and norm.vmax is None:
                norm.autoscale(accum)

can just be moved down a bit after collection.set_norm(norm) (then we can get back the canonicalized norm from the collection). I haven't looked at how this interacts with the call to _scale_norm just below, though.

Operating system

any

Matplotlib Version

3.9.0.dev1523+g2723052176

Matplotlib Backend

any

Python version

3.12

Jupyter version

no

Installation

None

Activity

  1. added this to the v3.10.0 milestone on Apr 19, 2024
  2. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Apr 19, 2024
  3. github-actions commented on Apr 19, 2024

    @github-actions

    Good first issue - notes for new contributors

    This 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
    .

  4. added a commit that references this issue on Apr 19, 2024
  5. clementgilli commented on Apr 19, 2024

    @clementgilli

    I put this block after `collection.set_norm(norm) in order to call the norm's getter.

    # autoscale the norm with current accum values if it hasn't been set
            if norm is not None:
                if collection.norm.vmin is None and collection.norm.vmax is None:
                    collection.norm.autoscale()
    

    Seems to solve the issue.

  6. added a commit that references this issue on Apr 29, 2024
  7. swapnil-patil8767 commented on Jun 17, 2024

    @swapnil-patil8767

    To fix this issue, the code needs to be modified so that the string is converted to a proper normalization object before attempting to use it. The suggestion is to move the block of code that handles normalization ('norm') further down after it has been properly set.

    This block checks and attempts to autoscale norm.
    code-

        if norm is not None:
             if norm.vmin is None and norm.vmax is None:
                   norm.autoscale(accum)
    

    updated code :

    def hexbin(x, y, norm=None, **kwargs):
        ...
        # Existing code for setting up the hexbin collection
        ...
        # Set the norm on the collection
        collection.set_norm(norm)
    
        # autoscale the norm with current accum values if it hasn't been set
        if norm is not None:
            if norm.vmin is None and norm.vmax is None:
                norm.autoscale(accum)
        
        # Continue with the rest of the hexbin logic
        ...
        return collection
    
    
  8. QuLogic commented on Jun 17, 2024

    @QuLogic
    Member

    This was already fixed by #28106.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions