Skip to content

Matplotlib polycollection plots for global domains plot bad data after rasterization update #1386

@mkavulich

Description

@mkavulich

Version

v2025.09.0

How did you install UXarray?

Conda

What happened?

Between v2025.05 and v2025.06 (when raster image support was released), something was broken in the polycollection method of plotting with matplotlib integration. I have made a minimal reproducible example script, using Python 3.12.11 (though I have seen this with earlier Python versions as well), though I'm not sure how to include the data.

When plotting with 2025.05, things look good:

Image

When plotting with 2025.09, things look bad:

Image

And when I add the argument periodic_elements='split' to uxda.to_polycollection(), the code fails entirely:

Traceback (most recent call last):
  File "/glade/work/kavulich/MPAS/plotting/plotting_errors/2025.09/mpas_plot/MRE.py", line 51, in <module>
    plotit(dataset[var].isel(Time=0),var,0,file,ftime_dt)
  File "/glade/work/kavulich/MPAS/plotting/plotting_errors/2025.09/mpas_plot/MRE.py", line 16, in plotit
    pc=uxda.to_polycollection(periodic_elements='split')
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/glade/work/kavulich/MPAS/plotting/plotting_errors/2025.09/mpas_plot/conda/envs/mpas_plot/lib/python3.12/site-packages/uxarray/core/dataarray.py", line 293, in to_polycollection
    poly_collection = self.uxgrid.to_polycollection(**kwargs)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/glade/work/kavulich/MPAS/plotting/plotting_errors/2025.09/mpas_plot/conda/envs/mpas_plot/lib/python3.12/site-packages/uxarray/grid/grid.py", line 2278, in to_polycollection
    ) = _grid_to_matplotlib_polycollection(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: uxarray.grid.geometry._grid_to_matplotlib_polycollection() got multiple values for keyword argument 'periodic_elements'

Interestingly, these problems don't appear to affect regional MPAS domains, so I suspect that the issue has to do with periodic/global data specifically. I have observed the same issue with 2025.06 so I believe that's when the problem was introduced.
The data file is too large to upload here but it is available on NCAR GLADE at the above path; however, this should be reproducible with any global/periodic MPAS data you have lying around. Let me know if you have any questions or I can provide any more info.

What did you expect to happen?

Pretty plots, as shown above.

Can you provide a MCVE to repoduce the bug?

#!/usr/bin/env python3
import os
import time
from datetime import datetime

import matplotlib.pyplot as plt
import cartopy.feature as cfeature
import cartopy.crs as ccrs

import uxarray as ux

def plotit(uxda: ux.UxDataArray,var: str,lev: int,filepath: str,ftime) -> None:
    plotstart = time.time()

    #pc=uxda.to_polycollection(periodic_elements='split')
    pc=uxda.to_polycollection()
    pc.set_antialiased(False)
    pc.set_cmap('viridis')

    fig, ax = plt.subplots(1, 1, figsize=(8,4), dpi=300, constrained_layout=True,
                           subplot_kw=dict(projection=ccrs.PlateCarree()))

    ax.add_feature(cfeature.NaturalEarthFeature(category='cultural', scale='50m',edgecolor='k',facecolor='none',linewidth=0.2, name='admin_0_countries'))
    ax.add_feature(cfeature.NaturalEarthFeature(category='physical',color='k',facecolor='none',
                           linewidth=0.5, scale='10m', name='coastline'))

    pc.set_transform(ccrs.PlateCarree())

    coll = ax.add_collection(pc, autolim=True)
    ax.autoscale()

    plt.title('Plot of 2-meter temperature for MPAS forecast, 2025-05-14 00:00:00',fontsize=8)

    cbar = plt.colorbar(coll,ax=ax,orientation='vertical')
    cbar.set_label('Units: {units}', fontsize=8)

    plt.savefig('example.png',format='png')
    plt.close(fig)
    print(f"Done saving 'example.png'. Plot generation {time.time()-plotstart} seconds")


if __name__ == "__main__":

    file='/glade/work/kavulich/MPAS/tutorial/120km_global/rundir/history.2025-05-14_00.00.00.nc'
    var='t2m'
    timestring='2025-05-14_00:00:00'
    ftime_dt = datetime.strptime(timestring.strip(), "%Y-%m-%d_%H:%M:%S")
    dataset=ux.open_dataset(file,file)

    plotit(dataset[var].isel(Time=0),var,0,file,ftime_dt)

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

Status

📝 To-Do

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions