-
Notifications
You must be signed in to change notification settings - Fork 413
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
copy() method for declarative interface #1719
Comments
Of use for this will be the |
So the thinking here would be to have some structure at the end for the user that might look something like the following... from datetime import datetime, timedelta
from metpy.plots import declarative
from metpy.units import units
import xarray as xr
# Set the date/time of the model run
# Update time to within the past two weeks
date = datetime(2021, 5, 19, 12)
# Remote access to the dataset from the UCAR site
ds = xr.open_dataset('https://thredds.ucar.edu/thredds/dodsC/grib'
f'/NCEP/GFS/Global_onedeg/GFS_Global_onedeg_{date:%Y%m%d}_{date:%H%M}.grib2')
# Subset data to be just over the U.S. for plotting purposes
ds = ds.sel(lat=slice(70,10), lon=slice(360-150, 360-55))
cntr = declarative.ContourPlot()
cntr.data = ds
cntr.field = 'Geopotential_height_isobaric'
cntr.level = 500 * units.hPa
cntr.time = date
cntr.contours = list(range(0, 10000, 60))
cntr.clabels = True
cntr2 = cntr.copy()
cntr2.field = 'Temperature_isobaric'
cntr2.contours = list(range(-40, 41, 5))
# Set the attributes for the map
# and put the contours on the map
panel = declarative.MapPanel()
panel.area = [-125, -74, 20, 55]
panel.projection = 'lcc'
panel.layers = ['states', 'coastline', 'borders']
panel.title = f'500-hPa Geopotential Heights at {cntr.time}'
panel.plots = [cntr, cntr2]
# Set the attributes for the panel
# and put the panel in the figure
pc = declarative.PanelContainer()
pc.size = (15, 15)
pc.panels = [panel]
# Show the figure
pc.show() The idea is that there is a reduced number of attribute settings for the second set of contours. Less typing would ostensibly lead to less errors (at least one hopes!). |
It turns out that Python has a built-in copy() method that can be used on any object. I've tested it with several objects from the declarative interface and it appears to work properly. To create a copy, make sure to In the above example from @kgoebber, importing |
Neat! It does work. I think it would be great if we made this a true method off of the various plotting objects (e.g., |
I've been able to make some simple adjustments to the declarative interface to include a copy() method in each of those classes (such as On another note, I discovered that copying a
It would be helpful to know if other people are getting this same error or have any insights into a potential solution. |
MapPanel needed an explicity written __copy__() to make sure the copy process worked correctly. All other classes in declarative.py did not need it. A .copy() method is also provided for ease of use.
MapPanel needed an explicity written __copy__() to make sure the copy process worked correctly. All other classes in declarative.py did not need it. A .copy() method is also provided for ease of use.
It would be helpful if you could copy an object (e.g.
ContourPlot
) and then be able to modify the attributes that need to differ. This could help reduce repetition in some map generation.The text was updated successfully, but these errors were encountered: