#!/usr/bin/env python # coding: utf-8 # # Enhancements to Plotting Capabilities in MetPy # Connor Cozad, Ryan May, Drew Camron
UCAR/Unidata: Boulder, CO
Contact: 23ccozad@gmail.com, rmay@ucar.edu, dcamron@ucar.edu # MetPy is an open-source Python library for reading, visualizing and performing calculations with weather data. During summer 2021, we made various improvements to MetPy’s plotting interface, many of which are included in the version 1.1.0 release. # # This notebook demonstrates how we created the examples in our poster, and showcases some of the ways that students and researchers can put these improvements to use. # ## Getting Started # First, we need to import MetPy and other libraries/tools we'll be using for the examples below. # In[1]: from datetime import datetime import geopandas import xarray as xr from metpy.cbook import get_test_data from metpy.plots import BarbPlot, ContourPlot, FilledContourPlot, MapPanel, PanelContainer, PlotGeometry from metpy.units import units # ## Easily Zoom In / Out of Maps # Simply adding a ‘+’ or ‘-’ suffix when specifying a region will zoom in or out of the region, beginning in MetPy 1.1.0. # # In this example, we demonstrate this feature by creating three separate maps: one is not zoomed at all, one is zoomed in by adding a '+' suffix, one is zoomed out by adding a '-' suffix. # In[2]: # MetPy's plotting interface doesn't allow us to create a map without giving it some data to plot. # For the sake of demonstrating zooming in/out, we plot a dataset, and then make the plotted data transparent. data = xr.open_dataset(get_test_data('narr_example.nc')) contour = ContourPlot() contour.data = data contour.field = 'Temperature' contour.level = 700 * units.hPa contour.linecolor = 'none' # Contour lines are transparent # Add plotted data to map and customize map panel = MapPanel() panel.plots = [contour] panel.area = 'co' # Center the map on Colorado panel.title = ' ' panel.layers = ['lakes', 'coastline', 'borders', 'usstates', 'land', 'ocean'] # Draw the map pc = PanelContainer() pc.size = (15, 15) pc.panels = [panel] pc.draw() # In[3]: # Duplicate the panel from above, but zoom it in panel_zoom_in = panel.copy() panel_zoom_in.area = 'co+' # NEW FEATURE: The '+' suffix zooms in on Colorado # Draw the map pc = PanelContainer() pc.size = (15, 15) pc.panels = [panel_zoom_in] pc.draw() # In[4]: # Duplicate the panel from above, but zoom it out panel_zoom_out = panel_zoom_in.copy() panel_zoom_out.area = 'co-' # NEW FEATURE: The '-' suffix zooms out on Colorado # Draw the map pc = PanelContainer() pc.size = (15, 15) pc.panels = [panel_zoom_out] pc.draw() # ## Plot Common Formats of GIS Vector Data # MetPy’s upcoming PlotGeometry class allows users to plot data read from geoJSON and shapefile formats. # # In this example, we show how you can plot the Day 1 Convective Outlook from NOAA Storm Prediction Center, using a geoJSON file from their website (https://www.spc.noaa.gov/gis/). # In[5]: # Use geopandas to read geoJSON file into a geopandas geodataframe spc_day_1 = geopandas.read_file('spc_day1otlk_20210714_1300.geojson') spc_day_1 # In[6]: # NEW FEATURE: Give the geopandas columns to the new PlotGeometry class to be plotted geo = PlotGeometry() geo.geometry = spc_day_1['geometry'] geo.stroke = spc_day_1['stroke'] geo.fill = spc_day_1['fill'] geo.labels = spc_day_1['LABEL'] geo.label_fontsize = 'xx-large' # Add plotted data to map and customize map panel = MapPanel() panel.plots = [geo] panel.area = [-120, -75, 25, 50] panel.projection = 'lcc' panel.title = 'NOAA SPC Day 1 Convective Outlook' panel.title_fontsize = 24 panel.layers = ['lakes', 'land', 'ocean', 'states', 'coastline', 'borders'] # Draw the map pc = PanelContainer() pc.size = (15, 15) pc.panels = [panel] pc.show() # ## Adjust Font Sizes for Text in Plots # MetPy 1.1.0 includes new attributes to set the font size for plot titles, contour labels, color bar labels, and station plot text. # # In this example, we demonstrate how to customize the size of the map title, contour labels, and color bar labels. # In[7]: # Open a dataset to plot data = xr.open_dataset(get_test_data('GFS_test.nc', as_file_obj=False)) # Create a filled contour plot of 700mb relative humidity cfill = FilledContourPlot() cfill.data = data cfill.field = 'Relative_humidity_isobaric' cfill.level = 700 * units.hPa cfill.time = datetime(2010, 10, 26, 12) cfill.colormap = 'summer_r' cfill.colorbar = 'horizontal' cfill.colorbar_fontsize = 'xx-large' # NEW FEATURE: Adjust font size of color bar labels # Create a contour plot of MSLP cntr = ContourPlot() cntr.data = data cntr.field = 'Pressure_reduced_to_MSL_msl' cntr.time = datetime(2010, 10, 26, 12) cntr.plot_units = 'hPa' cntr.contours = list(range(965, 1030, 5)) cntr.clabels = True cntr.label_fontsize = 16 # NEW FEATURE: Adjust font size of contour labels # Add plotted data to map and customize map panel = MapPanel() panel.plots = [cfill, cntr] panel.area = [-125, -70, 20, 55] panel.projection = 'lcc' panel.title = 'Mean Sea Level Pressure (hPa) and 700 hPa Relative Humidity (%)' panel.title_fontsize = 24 # NEW FEATURE: Adjust font size of map title panel.layers = ['coastline', 'borders', 'usstates'] # Draw the map pc = PanelContainer() pc.size = (15, 15) pc.panels = [panel, panel] pc.draw() # ## Duplicate Plot Objects # MetPy 1.1.0 introduces a `copy` method for all plot-related classes. # # In this example, we create a barb plot with blue barbs, and then easily make the same plot, except with green barbs, using the `copy` method. # In[8]: # Open a dataset to plot data = xr.open_dataset(get_test_data('GFS_test.nc', as_file_obj=False)) # Create a barb plot of 300mb winds barbs = BarbPlot() barbs.data = data barbs.time = datetime(2010, 10, 26, 12) barbs.field = ['u-component_of_wind_isobaric', 'v-component_of_wind_isobaric'] barbs.level = 300 * units.hPa barbs.skip = (2, 2) barbs.barblength = 14 barbs.plot_units = 'knot' barbs.color = 'darkblue' # Add plotted data to map and customize map panel = MapPanel() panel.plots = [barbs] panel.area = 'co' panel.projection = 'lcc' panel.title = ' ' panel.layers = ['lakes', 'coastline', 'borders', 'usstates', 'land', 'ocean'] # Draw the map pc = PanelContainer() pc.size = (15, 15) pc.panels = [panel] pc.draw() # In[9]: # Copy barb plot from above and change color to green barbs = barbs.copy() # NEW FEATURE: Duplicate objects with built-in copy method barbs.color = 'green' # Add plotted data to map and customize map panel = MapPanel() panel.plots = [barbs] panel.area = 'co' panel.projection = 'lcc' panel.title = ' ' panel.layers = ['lakes', 'coastline', 'borders', 'usstates', 'land', 'ocean'] # Draw the map pc = PanelContainer() pc.size = (15, 15) pc.panels = [panel] pc.draw()