FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

LogNorm ticklabels on colorbar · Issue #12503 · matplotlib/matplotlib · GitHub

Repository navigation

LogNorm ticklabels on colorbar #12503

Closed as not planned
Closed as not planned

Description

Bug report

Bug summary

I think position of ticklabels on colorbars with LogNorm is sometimes weird, and I believe it wasn't like this in earlier versions, but I haven't checked that now. Namely, in the example below, I would expect the lowest ticklabel be at 5x10-1, not 6x10-1 as it is now.

Code for reproduction

plt.imshow(np.random.random(15**2).reshape((15, 15))+0.5, norm=LogNorm(vmax=2, vmin=0.5), cmap='coolwarm')
plt.colorbar()

Actual outcome

Matplotlib version

  • Operating system: Ubuntu 16.04
  • Matplotlib version: 2.2.2
  • Matplotlib backend (print(matplotlib.get_backend())): inline
  • Python version: 3.5.2
  • Jupyter version (if applicable): Jupyter Lab 0.33.6

Activity

  1. ImportanceOfBeingErnest commented on Oct 12, 2018

    Member

    At least in matplotlib 2.0.2, the resulting image is the same as shown above.
    In matplotlib 1.5.3 the resulting image is

    In current master, the result is

    where something strange is happending with the formatting, probably the same issue as #12488.

    That being said, I think having the lower end ticked and labeled if it falls on a "useful" number may be more appealing; but expectations might be different here for different cases, such that finding a good default is not obvious.

    You can decide to have all minor ticks labelled (which would result in crowded axes here) or none (which would not show any label except the10^1), or, you can have "nice" labels (the default). This is hardcoded to be [1,2,3,4,6,10]*10^exp

    c = np.logspace(0, 1, int(b)//2 + 1, base=b)

    On a by-case basis you may of course use your own locations

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.colors import LogNorm
    from matplotlib.ticker import LogFormatterSciNotation
    
    plt.imshow(np.random.random(15**2).reshape((15, 15))+0.5, 
               norm=LogNorm(vmax=2, vmin=0.5), cmap='coolwarm')
    
    
    class MF(LogFormatterSciNotation):
        def set_locs(self, locs=None):
            self._sublabels = set([1,2,5,10])
    
    cb = plt.colorbar(format=MF())
    
    plt.show()
    

    Having an interface for those of the style LogFormatter(..., sublabels=None) would be an option to add. In case sublabels are provided, minor_thresholds would be ignored, and self._sublabels = sublabels being set.

  2. jklymak commented on Oct 12, 2018

    Member

    Just to clarify. On 3.0.0 the auto ticks should be the same as for any other log axis. That is a change as before colorbars drew their own ticks and labels.

  3. ImportanceOfBeingErnest commented on Oct 12, 2018

    Member

    Yep, so the problem can of course be reformulated as to whether there should be an option to customize automatic ticking in

    plt.xscale("log")
    plt.xlim(0.5, 2)
    

  4. anntzer commented on Oct 12, 2018

    Contributor

    I think @Phlya may not see the formatting (superscript vs inline math) issue because of some setting in his rcParams? @Phlya can you provide them?

  5. Phlya commented on Oct 12, 2018

    ContributorAuthor

    Here is my matplotlibrc, I think everything is commented out...
    matplotlibrc.zip
    And I also have sns.set_style("ticks") in case it might be important.

  6. ImportanceOfBeingErnest commented on Oct 12, 2018

    Member

    No, for the code shown here, the "formatting (superscript vs inline math) issue" does not appear in version 2.2.2 (which @Phlya is using). Neither in matplotlib 3.0.0. (sorry, wrong env used)
    It only appears in the master branch and (edit ->) in matplotlib 3.0.0.

  7. anntzer commented on Oct 12, 2018

    Contributor

    I also see it with mpl3.0.0.

  8. jklymak commented on Oct 12, 2018

    Member

    Having an interface for those of the style LogFormatter(..., sublabels=None) would be an option to add. In case sublabels are provided, minor_thresholds would be ignored, and self._sublabels = sublabels being set.

    This seems a reasonable feature request to me. I actually am not sure I like the defaults....

  9. assigned and unassigned on Oct 13, 2018
  10. anntzer commented on Oct 13, 2018

    Contributor

    In this specific case, it seems like the minortick labeler should just check what is the lowest minor tick and adjust itself accordingly?

  11. Phlya commented on Nov 1, 2019

    ContributorAuthor

    I keep stumbling upon this issue - I often use LogNorm with values around 1 in the range as used above.

    I was just now trying to reproduce the custom subclass from #12503 (comment) to fix this, but it doesn't seem to work:

    class MF(LogFormatter):
        def set_locs(self, locs=None):
            self._sublabels = set([1,2,3,4,5,6,7,8,9,10]) # All levels just as an example
    im = plt.imshow(np.linspace(0.5, 2, 100).reshape((10, 10)), norm=LogNorm(0.5, 2))
    cb = plt.colorbar(format=MF())

    I am using mpl 3.1.1

    Any ideas? Is there another way to fix this?

    The problem is that I always want to show the bottom tick, but even adding with cb.set_ticks([0.5] doesn't do anything.

  12. Phlya commented on Nov 1, 2019

    ContributorAuthor

    Even more strange, perhaps, is that with vmin=0.25, vmax=4 the colorbar has no tick labels apart from 1.

  13. jklymak commented on Nov 1, 2019

    Member

    PRs certainly welcome for this - someone would have to dig into the guts of the LogFormatter for minor ticks. That would be great, but it is unusual to use log scales on such a small range of data, hence it hasn't received much attention. Note the simpler minimal example is

    import matplotlib.pyplot as plt
    
    plt.xscale("log")
    plt.xlim(0.25, 5)
    plt.show()
    
  14. ImportanceOfBeingErnest commented on Nov 1, 2019

    Member

    The workaround from above would now need to look like

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.colors import LogNorm
    from matplotlib.ticker import LogFormatterSciNotation
    
    plt.imshow(np.random.random(15**2).reshape((15, 15))+0.5, 
               norm=LogNorm(vmax=2, vmin=0.5), cmap='coolwarm')
    
    class MF(LogFormatterSciNotation):
        def set_locs(self, locs=None):
            self._sublabels = set([1,2,5,10])
    
    cb = plt.colorbar(format=MF())
    cb.ax.yaxis.set_minor_formatter(MF())
    
    plt.show()
    

    but it's still a workaround and uses some private attribute, so a more sustainable solution is clearly desireable.

  15. anntzer commented on Nov 2, 2019

    Contributor

    Would be nice to figure out why #12865 doesn't help here.

  16. github-actions commented on May 18, 2023

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  17. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Jun 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.status: inactiveMarked by the “Stale” Github Action

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL