Skip to content

Conversation

@philipc2
Copy link
Member

@philipc2 philipc2 commented May 20, 2025

Closes #1268, #366

Overview

  • Adds a UxDataArray.to_raster() method, which is used to sample an unstructured grid onto a provided GeoAxes
  • Enables Matplotlib plotting workflows that rely on raster data, such as imshow, contour and contourf
  • Updated user guide section on Plotting with Matplotlib and Cartopy

Example

fig, ax = plt.subplots(
    subplot_kw={'projection': ccrs.Orthographic(central_longitude=-90, central_latitude=45)},
    constrained_layout=True,
    figsize=(10, 5),
)
scale = '110m'


ax.set_extent([-90, -80, 40, 50])  # great lakes 

var = uxds['lwupt'].isel(Time=0)

raster = var.to_raster(ax=ax)

my_plot = ax.imshow(raster, origin='lower', extent=ax.get_xlim() + ax.get_ylim())


ax.coastlines(resolution=scale, color='black', linewidth=1)
ax.add_feature(cfeature.BORDERS.with_scale(scale), linestyle=':', linewidth=0.8)
ax.add_feature(cfeature.STATES.with_scale(scale), linestyle=':', linewidth=0.8)
gl = ax.gridlines(
    draw_labels=True,
    linewidth=0.5,
    linestyle='--',
    color='gray',
    alpha=0.7,
)

lakes = NaturalEarthFeature(
    category='physical',
    name='lakes',
    scale='10m',
    edgecolor='black',
    facecolor='none',
    alpha=0.7,
)
ax.add_feature(lakes, linewidth=1, zorder=2)

gl.top_labels = False
gl.right_labels = False

ax.set_title(f"15km MPAS: {var.long_name}", pad=4, fontsize=10)

cbar = fig.colorbar(
    my_plot,
    ax=ax,
    orientation='vertical',
    shrink=0.6,
    pad=0.03,
)
label = rf"$\mathit{{{var.name}}}\ \left[\mathrm{{{var.units}}}\right]$"
cbar.set_label(label, labelpad=8, fontsize=12)

plt.show()
image

@philipc2 philipc2 requested review from erogluorhan and removed request for erogluorhan May 20, 2025 16:28
@philipc2 philipc2 self-assigned this May 20, 2025
@philipc2 philipc2 changed the title DRAFT: Native Matplotlib Plotting Routines Add imshow function for plotting with Matplotlib May 21, 2025
@philipc2 philipc2 added this to the Visualization milestone May 21, 2025
@philipc2
Copy link
Member Author

@brianpm

Let me know if you have any feedback on this. I'm curious whether you think imshow() is an appropriate name here or if you have any other ideas.

@philipc2
Copy link
Member Author

Rendering some data from the hackathon (HEALPix zoom=10) only takes about 5s on Derecho.

image

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@philipc2 philipc2 marked this pull request as ready for review May 22, 2025 18:12
@philipc2 philipc2 requested a review from kafitzgerald May 27, 2025 17:19
Copy link
Collaborator

@kafitzgerald kafitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little worried that the API here might be confusing and not particularly intuitive for users familiar with conventions in the broader scientific Python ecosystem for a couple of reasons.

The first being that it deviates from other high level plotting APIs in the scientific Python ecosystem that use .plot() and .hvplot(). This is definitely beyond the scope of this PR, but adds some potential for confusion and complexity here.

And the second that imshow() here is pretty different from how it's used in Xarray and Matplotlib (e.g. there's a good bit going on with the resampling, Cartopy is used by default, etc.). Maybe a new name would be appropriate?

That said, I don't know that I have a great suggestion for what to do here. I suspect that something chained onto the .plot() accessor would be more intuitive, but that seems a bit complicated and potentially confusing given what's already present. Maybe implementing something under uxarray.plot.mpl.plot() for a high level Matplotlib plotting interface could work?

Edited to add: I should also say it's really exciting to see this and I think folks will very much appreciate having better support for Matplotlib and the performance improvements.

@philipc2 philipc2 requested a review from kafitzgerald June 12, 2025 18:20
@philipc2
Copy link
Member Author

Should be good for another set of reviews. I've made some reccomended changes after discussing with @erogluorhan , notably changing the signature to be to_raster().

Any feedback on the update notebook would be much appriciated, especially from @brianpm

Copy link
Member

@erogluorhan erogluorhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this together! I like it overall; just see a few comments below please.

@philipc2
Copy link
Member Author

philipc2 commented Jun 23, 2025

@erogluorhan

I've added a "Conversion Methods" section in the API which can house our to_ methods and document the applications of them.

https://uxarray--1271.org.readthedocs.build/en/1271/api.html#conversion-methods

@philipc2 philipc2 requested a review from erogluorhan June 23, 2025 16:31
@erogluorhan
Copy link
Member

@erogluorhan

I've added a "Conversion Methods" section in the API which can house our to_ methods and document the applications of them.

This sounds great and sure will help users find them much easier!

Copy link
Member

@erogluorhan erogluorhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great; approving with a couple minor suggestions.

philipc2 and others added 2 commits June 23, 2025 14:07
Co-authored-by: Orhan Eroglu <32553057+erogluorhan@users.noreply.github.com>
Co-authored-by: Orhan Eroglu <32553057+erogluorhan@users.noreply.github.com>
Copy link
Collaborator

@kafitzgerald kafitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@philipc2 philipc2 merged commit 9af3d8a into main Jun 26, 2025
19 of 20 checks passed
@erogluorhan erogluorhan deleted the uxarray-mpl branch September 26, 2025 17:49
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

Successfully merging this pull request may close these issues.

Explicit Matplotlib plotting routines

7 participants