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

How to do axis label sharing with geographic axis? · Issue #155 · Ultraplot/UltraPlot · GitHub

Repository navigation

How to do axis label sharing with geographic axis? #155

Closed

Description

How to do axis label sharing with geographic axis?

import ultraplot as pplt
fig, axs = pplt.subplots(ncols=2, nrows=2, projection='cyl', refwidth='5cm', share='all')
axs.format(lonlim=(0, 60), latlim=(-20, 20),
           lonlines=15, latlines=10, lonlabels='both', latlabels='both')

Like the code above, share='all' doesn't seems work.

Activity

  1. cvanelteren commented on Apr 12, 2025

    Collaborator

    You're right—this isn't currently supported and would mainly make sense for rectilinear projects. That said, it’s definitely a feature we could consider adding. You can check out the relevant commit here: 2085c2f1. If you're interested, feel free to open a PR to kick things off!

  2. cvanelteren commented on Apr 13, 2025

    Collaborator

    Thanks for the suggestion! I took a look, and to support this properly, the GeoAxis object would need some extended functionality for shared axes. That would involve patching in methods similar to those used by CartesianAxes, while also ensuring all projections involved are rectilinear (and the same). The patching would be possible relatively easily but requires proper testing and disproportionate use for a feature of which I think is rather niche (but do correct me when I am wrong).

    Since this is a fairly niche use case, a quick workaround for now would be to manually disable the labels. I agree it's a worthwhile improvement, and I might revisit this when I have a bit more time to work on it -- otherwise I am willing to review pull requests in the mean time.

  3. cvanelteren commented on Apr 14, 2025

    Collaborator

    Update:

    Think I figured something out:

    Still in a pretty rough state but it is getting somewhere. More to follow soon 👀

  4. cvanelteren commented on Apr 14, 2025

    Collaborator

    @gepcel can you checkout #159

  5. cvanelteren commented on Apr 14, 2025

    Collaborator


    🥳

  6. gepcel commented on Apr 15, 2025

    CollaboratorAuthor

    I'm not sure I did everything right. But I got an error.

    In [1]: import ultraplot as pplt
    
    In [2]: fig, axs = pplt.subplots(ncols=2, nrows=2, projection='cyl', refwidth='5cm', share='all')
    
    In [3]: axs.format(lonlim=(0, 60), latlim=(-20, 20),
       ...:            lonlines=15, latlines=10, lonlabels='both', latlabels='both')
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    Cell In[3], line 1
    ----> 1 axs.format(lonlim=(0, 60), latlim=(-20, 20),
          2            lonlines=15, latlines=10, lonlabels='both', latlabels='both')
    
    File ~\Downloads\ultraplot-feat-shared-geo-new\ultraplot\gridspec.py:1630, in SubplotGrid.format(self, **kwargs)
       1600 @docstring._snippet_manager
       1601 def format(self, **kwargs):
       1602     """
       1603     Call the ``format`` command for the `~SubplotGrid.figure`
       1604     and every axes in the grid.
       (...)   1628     ultraplot.config.Configurator.context
       1629     """
    -> 1630     self.figure.format(axs=self, **kwargs)
    
    File ~\Downloads\ultraplot-feat-shared-geo-new\ultraplot\internals\warnings.py:118, in _rename_kwargs.<locals>._decorator.<locals>._deprecate_kwargs_wrapper(*args, **kwargs)
        113         key_new = key_new.format(value)
        114     _warn_ultraplot(
        115         f"Keyword {key_old!r} was deprecated in version {version} and may "
        116         f"be removed in {next_release()}. Please use {key_new!r} instead."
        117     )
    --> 118 return func_orig(*args, **kwargs)
    
    File ~\Downloads\ultraplot-feat-shared-geo-new\ultraplot\figure.py:1694, in Figure.format(self, axs, figtitle, suptitle, suptitle_kw, llabels, leftlabels, leftlabels_kw, rlabels, rightlabels, rightlabels_kw, blabels, bottomlabels, bottomlabels_kw, tlabels, toplabels, toplabels_kw, rowlabels, collabels, includepanels, **kwargs)
       1687 for ax in axs:
       1688     kw = {
       1689         key: value
       1690         for cls, kw in kws.items()
       1691         for key, value in kw.items()
       1692         if isinstance(ax, cls) and not classes.add(cls)
       1693     }
    -> 1694     ax.format(rc_kw=rc_kw, rc_mode=rc_mode, skip_figure=True, **kw, **kwargs)
       1696 # Warn unused keyword argument(s)
       1697 kw = {
       1698     key: value
       1699     for name in kws.keys() - classes
       1700     for key, value in kws[name].items()
       1701 }
    
    File ~\Downloads\ultraplot-feat-shared-geo-new\ultraplot\axes\geo.py:884, in GeoAxes.format(self, extent, round, lonlim, latlim, boundinglat, longrid, latgrid, longridminor, latgridminor, ticklen, lonticklen, latticklen, latmax, nsteps, lonlocator, lonlines, latlocator, latlines, lonminorlocator, lonminorlines, latminorlocator, latminorlines, lonlocator_kw, lonlines_kw, latlocator_kw, latlines_kw, lonminorlocator_kw, lonminorlines_kw, latminorlocator_kw, latminorlines_kw, lonformatter, latformatter, lonformatter_kw, latformatter_kw, labels, latlabels, lonlabels, rotatelabels, loninline, latinline, inlinelabels, dms, labelpad, labelcolor, labelsize, labelweight, **kwargs)
        882 latlabels = _not_none(latlabels, labels)
        883 # Set the ticks
    --> 884 self._toggle_ticks(lonlabels, "x")
        885 self._toggle_ticks(latlabels, "y")
        886 lonarray = self._to_label_array(lonlabels, lon=True)
    
    File ~\Downloads\ultraplot-feat-shared-geo-new\ultraplot\axes\geo.py:596, in GeoAxes._toggle_ticks(self, label, which)
        594 case "all" | "both":
        595     if which == "x":
    --> 596         self.xaxis.set_position("both")
        597     else:
        598         self.yaxis.set_position("both")
    
    AttributeError: 'XAxis' object has no attribute 'set_position'
    
  7. cvanelteren commented on Apr 15, 2025

    Collaborator

    Yes my bad I need to change the typo

  8. cvanelteren commented on Apr 15, 2025

    Collaborator

    @gepcel fixed the typos. Should be good now I think. hold on, both sides are not behaving correctly, the others are fine. Edit 2: Actually try it, not sure if this is what you wanted or you want the inner labels to turn off too

  9. cvanelteren commented on Apr 15, 2025

    Collaborator

    There are still some bugs to figure with the automatic sharing. I will try to limit some.

  10. cvanelteren commented on Apr 15, 2025

    Collaborator

    Note to self: A few things need to happen.

    • Axes are automatically shared in UltraPlot. The checks needs to be expanded such that it turns off the sharing when the sharing won't be possible. In principle mixing cartopy and basemap should work fine for rectilinear projections but there are some quirks that still exist in this context
    • Constructively building a figure such as in test_geographic_multiple_projections will fail unless sharing is explicitly turned off which is not nice from a user experience.
  11. cvanelteren commented on Apr 15, 2025

    Collaborator

    We could also just turn off sharing by default; which would be the easiest fix I guess.

  12. gepcel commented on Apr 15, 2025

    CollaboratorAuthor

    What should axis sharing do when it's on both sides? For example with a single axes, yticklabel is shown on both left and right sides.

    fig, ax = pplt.subplot(refwidth='3cm')
    ax.format(yticklabelloc='both')
    pplt.show()
    

    And with subplots, the first axis gets yticklabel on both left and right sides, but the second gets no yticklabels.

    fig, ax = pplt.subplots(ncols=2, refwidth='3cm', share='all')
    ax.format(yticklabelloc='both')
    pplt.show()
    

    And this holds for geoaxes, too. With subplots, both subplots get latlabels on both left and right sides.

    fig, ax = pplt.subplots(ncols=2, refwidth='3cm', proj='cyl',share='all')
    ax.format(latlabels='both')
    pplt.show()
    

    I may be wrong, but I'm expecting something like:

  13. cvanelteren commented on Apr 16, 2025

    Collaborator

    I see. I have some local changes that make this possible. It is a bit tricky as UltraPlot turns on axis sharing by the default which causes issues with projection classes. I am making some internal changes to see if this is possible.

  14. 9 remaining items

  15. gepcel commented on Apr 18, 2025

    CollaboratorAuthor

    This may be quite a niche requirement, so no worries, and feel free to skip this.

  16. cvanelteren commented on Apr 24, 2025

    Collaborator

    @gepcel think I can make it work.

  17. cvanelteren commented on Apr 25, 2025

    Collaborator

    @gepcel checkout the PR. Think it should be working properly now.

  18. gepcel commented on Apr 25, 2025

    CollaboratorAuthor

    Works well as expected.

  19. cvanelteren commented on Apr 25, 2025

    Collaborator

    Cool! Will need a code review before merging. Keeping it open until merged.

  20. gepcel commented on Apr 25, 2025

    CollaboratorAuthor

    Since this is open, this works for GeoAxes. Are this supposed to work for CartesianAxes when xticklabelloc='both' or yticklabelloc='both'? I think GeoAxes is good enough.

  21. cvanelteren commented on Apr 25, 2025

    Collaborator

    Could you please clarify? The PR targets mostly GeoAxes sharing and the behavior for Cartesian plots is supposed to be unchanged. An example may help.

  22. gepcel commented on Apr 25, 2025

    CollaboratorAuthor

    Can you try:

    import ultraplot as pplt
    fig, axs = pplt.subplots(ncols=2, nrows=2,share='all',refwidth='2cm')
    axs.format(xlim=(0,60),ylim=(-20,20), xticklabelloc='t', yticklabelloc='r')
    pplt.show()
  23. cvanelteren commented on Apr 25, 2025

    Collaborator

    Ah I see... that is a different bug.

    To explain what is happening. The axes are shared which makes the left most and bottom most subplot the references. If you set the ticks to be top and right, it updates them for the references. What you want, I think, is set the top labels on for the top and right labels for the right most axes.

  24. cvanelteren commented on Apr 25, 2025

    Collaborator

    I got a fix for this but it is rather uggly...let's discuss in #176.

  25. cvanelteren commented on May 8, 2025

    Collaborator

    @gepcel can you checkout the PR again. This ended up a lot more work than I thought and I am a bit hesitant to merge this without proper testing. Internally I had to add features that are frowned upon by matplotlib. I added the ability to toggle axis sharing on and off. There we also numerous bugs with the sharing caused by the difference in backends that deviate from the default matplotlib standards. For example, cartopy does not actually create tick labels but merely puts text in the correct places, whereas basemap handles this completely differently.

  26. cvanelteren commented on May 11, 2025

    Collaborator
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

    bugSomething isn't workingenhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL