Skip to content

Error when setting titles for a subset of axes using a list of titles #166

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

Closed
ToryDeng opened this issue Apr 19, 2025 · 4 comments
Closed

Comments

@ToryDeng
Copy link

ToryDeng commented Apr 19, 2025

Like the issue title said. Here is a reproducible example:

fig, axs = uplt.subplots(nrows=1, ncols=3)
axs[1:].format(title=["a", "b"])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[73], line 2
      1 fig, axs = uplt.subplots(nrows=1, ncols=3)
----> 2 axs[1:].format(title=["a", "b"])

File /usr/local/miniforge3/envs/experiment/lib/python3.12/site-packages/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 /usr/local/miniforge3/envs/experiment/lib/python3.12/site-packages/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 /usr/local/miniforge3/envs/experiment/lib/python3.12/site-packages/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 /usr/local/miniforge3/envs/experiment/lib/python3.12/site-packages/ultraplot/axes/cartesian.py:1350, in CartesianAxes.format(self, aspect, xloc, yloc, xspineloc, yspineloc, xoffsetloc, yoffsetloc, xwraprange, ywraprange, xreverse, yreverse, xlim, ylim, xmin, ymin, xmax, ymax, xscale, yscale, xbounds, ybounds, xmargin, ymargin, xrotation, yrotation, xformatter, yformatter, xticklabels, yticklabels, xticks, yticks, xlocator, ylocator, xminorticks, yminorticks, xminorlocator, yminorlocator, xcolor, ycolor, xlinewidth, ylinewidth, xtickloc, ytickloc, fixticks, xtickdir, ytickdir, xtickminor, ytickminor, xtickrange, ytickrange, xtickcolor, ytickcolor, xticklen, yticklen, xticklenratio, yticklenratio, xtickwidth, ytickwidth, xtickwidthratio, ytickwidthratio, xticklabelloc, yticklabelloc, xticklabeldir, yticklabeldir, xticklabelpad, yticklabelpad, xticklabelcolor, yticklabelcolor, xticklabelsize, yticklabelsize, xticklabelweight, yticklabelweight, xlabel, ylabel, xlabelloc, ylabelloc, xlabelpad, ylabelpad, xlabelcolor, ylabelcolor, xlabelsize, ylabelsize, xlabelweight, ylabelweight, xgrid, ygrid, xgridminor, ygridminor, xgridcolor, ygridcolor, xlabel_kw, ylabel_kw, xscale_kw, yscale_kw, xlocator_kw, ylocator_kw, xformatter_kw, yformatter_kw, xminorlocator_kw, yminorlocator_kw, **kwargs)
   1348 if aspect is not None:
   1349     self.set_aspect(aspect)
-> 1350 super().format(rc_kw=rc_kw, rc_mode=rc_mode, **kwargs)

File /usr/local/miniforge3/envs/experiment/lib/python3.12/site-packages/ultraplot/axes/base.py:2830, in Axes.format(self, title, title_kw, abc_kw, ltitle, lefttitle, ctitle, centertitle, rtitle, righttitle, ultitle, upperlefttitle, uctitle, uppercentertitle, urtitle, upperrighttitle, lltitle, lowerlefttitle, lctitle, lowercentertitle, lrtitle, lowerrighttitle, share_xlabels, share_ylabels, **kwargs)
   2828     self._abc_pad = units(pad)
   2829 self._update_abc(**abc_kw)
-> 2830 self._update_title(None, title, **title_kw)
   2831 self._update_title(
   2832     "left",
   2833     _not_none(ltitle=ltitle, lefttitle=lefttitle),
   2834     **title_kw,
   2835 )
   2836 self._update_title(
   2837     "center",
   2838     _not_none(ctitle=ctitle, centertitle=centertitle),
   2839     **title_kw,
   2840 )

File /usr/local/miniforge3/envs/experiment/lib/python3.12/site-packages/ultraplot/axes/base.py:2552, in Axes._update_title(self, loc, title, **kwargs)
   2550     pass
   2551 elif self.number > len(title):
-> 2552     raise ValueError(
   2553         f"Invalid title list length {len(title)} "
   2554         f"for axes with number {self.number}."
   2555     )
   2556 else:
   2557     kw["text"] = title[self.number - 1]

ValueError: Invalid title list length 2 for axes with number 3.

I can add the titles by adding an empty string:

fig, axs = uplt.subplots(nrows=1, ncols=3)
axs[1:].format(title=["", "a", "b"])

Image

although the code looks a little weird.

@cvanelteren
Copy link
Contributor

hmm would have to look at this. The doc states that it gets the properties according to the axis number. Which would explain the error; you are providing two titles but the SubplotGrid has three. I agree that it is rather unintuitive however.

@cvanelteren
Copy link
Contributor

The fix would be to make the format a property of the SubplotGrid rather than going through Figure. Changing this would require some retooling I think. We could also parse it sequentially if the subplot grid is smaller than the number of subplots. The latter may be easier now that I think of it.

@cvanelteren
Copy link
Contributor

Working on a fix see #167

@cvanelteren
Copy link
Contributor

Fixed in main

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

No branches or pull requests

2 participants