Skip to content
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

efficient plotting #24

Open
wwieder opened this issue Feb 6, 2020 · 5 comments
Open

efficient plotting #24

wwieder opened this issue Feb 6, 2020 · 5 comments
Assignees

Comments

@wwieder
Copy link
Collaborator

wwieder commented Feb 6, 2020

how do we make panel plots efficiently?

@jhamman
Copy link

jhamman commented Feb 7, 2020

Hi @wwieder,

I'll just drop a few comments off the top of my head.

  1. For multi-panel plots that are "good enough" for exploratory analysis, you may checkout out xarray's facet grid functionality: http://xarray.pydata.org/en/stable/plotting.html#faceting. I find these are super useful until I need to make the final publication quality tweaks often required.

  2. When you need more control or you have a very custom plot to make, I generally follow this pattern:

import numpy as np
import matplotlib.pyplot as plt
...

def plot_function(da, ax=None, title=None, ...):
    '''a function to make one subplot'''
    if ax is None:
        ax = plt.gca()
    da.plot(ax=ax, ...)  # more custom args
    ax.set_title(title)

fig, axes = plt.subplots(nrows=4, ncols=2, ...)

for index, ax in np.ndenumerate(axes):
    # there are various ways to do this part, index in this case is a tuple (ie `(0, 0)`)
    plot_function(ds['varname'].isel(time=index), ax=ax, title=index, ...)

fig.suptitle('Super Title')
fig.save(...)

@wwieder
Copy link
Collaborator Author

wwieder commented Feb 12, 2020 via email

@jhamman
Copy link

jhamman commented Feb 13, 2020

fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(14,12))

I think you want to breakdown which parameters go on the subplot and which ones go on the plot itself:

projection=ccrs.Robinson()   # goes on the subplot
transform=ccrs.PlateCarree()  # goes to the plot function

The way the subplots function works is that you will pass in the arguments required for each subplot via the subplot_kw keyword argument:

fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(14,12), subplot_kw=dict(projection=ccrs.Robinson()))

plt.pcolormesh(..., ax=ax, transform=ccrs.PlateCarree())

@wwieder
Copy link
Collaborator Author

wwieder commented Feb 13, 2020 via email

@jhamman
Copy link

jhamman commented Feb 14, 2020

sorry...
the pcolormesh line is basically the same as the the DataArray.plot method call:

plt.pcolormesh <->  da.plot

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