Skip to content

Remember metadata when passing mappables to colorbar() #81

Closed

Description

@mickaellalande

I have another suggestion of enhancement regarding what Cartopy does. By default, ProPlot uses the long_name attribute of a DataArray as a title of the figure, but not the units attribute. Here is the default behavior of Cartopy when doing a plot (data: ens_avg_snow_cyclic.zip):

import xarray as xr
import proplot as plot

ens_avg_snow_cyclic = xr.open_dataarray('ens_avg_snow_cyclic.nc')
ens_avg_snow_cyclic.plot()

cartopy_units

It automatically puts the long_name and units attribute of the DataArray beside the color bar. In addition, it writes as a title the coordinates left (I didn't do any projection, the goal is only to show the labels here).

It would be nice to at least have the units after the title in ProPlot too, in addition to the actual default behavior shown here:

f, axs = plot.subplots(proj='cyl', width=8)

m = axs[0].contourf(ens_avg_snow_cyclic, cmap='BuRd')

f.colorbar(m)

axs.format(
    geogridlinewidth=0.5, geogridcolor='gray8', geogridalpha=0.5, labels=True, 
    coast=True, ocean=True, oceancolor='gray3', latlim=(20,90),
    suptitle=str(ens_avg_snow_cyclic.season.values) + ' climatology'
)

proplot_default

Like for example (here are 3 different possibilies I am thinking):
proplot_default_1
proplot_default_2
proplot_default_3

Or having the same behavior as Cartopy like the 2nd example and putting as a title the left coordinates as coord_name = values. Here it is the season, but usually, if we select a date, it shows time = .... After it doesn't have to be like Cartopy, in my opinion adding at least the units would be already nice :)

Activity

  1. lukelbd commented on Dec 4, 2019

    @lukelbd
    Collaborator

    For now if you pass colorbar='r' or colorbar=True to your axs[0].contourf command, it will automatically label the colorbar with the long_name and units. Currently, automatic labeling is 100% controlled from plotting commands -- so it is only possible to automatically label colorbars and legends if you generate them on-the-fly by passing colorbar='r', legend='r', etc. to your plotting command.

    I think I can fix this by adding a hidden _metadata attribute to artists generated by ProPlot plotting methods, and then when legend() or colorbar() receives an artist with this attribute, it can automatically apply the labels. I already do something similar for artists passed through cmap_changer.

    I really like those examples! This feature should definitely be configurable. We can add keyword arguments to determine where the labels and units are placed, and maybe some rc options. I think the defaut behavior should be what it is now though -- generally units are not something that show up on axis labels, colorbars, and legends, not titles.

    It wil also definitely be useful if ProPlot can try to detect singleton or "scalar" dimensions and add those values to the title, like how your example shows the time slice.

  2. added this to the Version 1 milestone on Dec 4, 2019
  3. mickaellalande commented on Dec 5, 2019

    @mickaellalande
    ContributorAuthor

    Great ok, yes colorbar='r' works well! Thus:

    f, axs = plot.subplots(proj='cyl', width=8)
    
    m = axs[0].contourf(ens_avg_snow_cyclic, cmap='BuRd', colorbar='r')
    
    axs.format(
        geogridlinewidth=0.5, geogridcolor='gray8', geogridalpha=0.5, labels=True, 
        coast=True, ocean=True, oceancolor='gray3', latlim=(20,90),
        suptitle=str(ens_avg_snow_cyclic.season.values) + ' climatology', title=''
    )

    Gives the same result as the 2nd example without having to pass the units :)

    However, if I replace by colorbar=True it through an error:

    ValueError: Invalid on-the-fly location None. Must be a preset location. See Axes.colorbar.

    I guess we need to set the location first?

  4. mickaellalande commented on Dec 5, 2019

    @mickaellalande
    ContributorAuthor

    I also just realized that if I use non-cyclic data and use the option globe=True in addition to colorbar='r' the long_name and units don't show up anymore besides the color bar. It works only without the option globe=True...

  5. lukelbd commented on Dec 5, 2019

    @lukelbd
    Collaborator

    @mickaellalande Thanks for letting me know -- just fixed this in the latest version.

    https://github.com/lukelbd/proplot/blob/0b5195284964ec49ad7bdecd5c1ef47e777892e3/proplot/wrappers.py#L647-L652

    When #45 is merged these kinds of bugs will be easier to avoid.

  6. changed the title [-]ProPlot doesn't use the `units` attribute of a DataArray[/-] [+]Remember DataArray/Dataframe metadata when passing mappables to colorbar()[/+] on Jan 7, 2020
  7. changed the title [-]Remember DataArray/Dataframe metadata when passing mappables to colorbar()[/-] [+]Remember metadata when passing mappables to colorbar()[/+] on Jan 7, 2020
  8. lukelbd commented on Aug 18, 2021

    @lukelbd
    Collaborator

    This is now implemented on master -- all "autoformat" info is now attached to outgoing handles and mappables and when they are passed into legend() or colorbar() the default label is applied. Here's a very quick example:

     import proplot as pplt
     import xarray as xr
     fig, ax = pplt.subplots()
     data = xr.DataArray(np.random.rand(10, 10), attrs={'long_name': 'variable'})
     m = ax.pcolor(data)
     ax.colorbar(m, loc='b')

    cbar

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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions