[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[python #CWW-995296]: Plotting pygrib file with matplotlib
- Subject: [python #CWW-995296]: Plotting pygrib file with matplotlib
- Date: Fri, 04 Dec 2020 16:44:27 -0700
Greetings!
Well this is a strange one. I found a copy of some similar data here:
https://thredds-test.unidata.ucar.edu/thredds/catalog/grib/NCEP/NBM/Ocean/National_Blend_Ocean_20201110_1200.grib2/catalog.html?dataset=grib/NCEP/NBM/Ocean/National_Blend_Ocean_20201110_1200.grib2
Those lines are caused by contours wrapping from the right side of the map
domain to the left. While this makes sense in some contexts, what's weird here
is that the longitudes seem to only be going 0->180, and then back around. In
fact, looking at a single row of longitudes in this data, I saw multiple places
where it jumped from 0 to -180. I think that's a bug in pygrib. You can work
around it like I've done in a modified version of your code using
`numpy.unwrap` (which for future reference is much more easy to work with when
attached as text rather than a screenshot):
import pygrib
import matplotlib
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import numpy as np
filename = 'blend.t00z.pop12.oc.grib2'
read = pygrib.open(filename)
numbers = read[1]
lats, lons = numbers.latlons()
lons2 = np.unwrap(lons, discont=180)
figure = plt.figure(1, figsize=(14, 14))
ax = plt.subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.set_extent([-130, -65, 20, 55])
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.STATES)
ax.contour(lons2, lats, numbers.values, transform=ccrs.PlateCarree())
cbar = ax.imshow(numbers.values)
plt.colorbar(cbar)
ax.set_title("fasdf")
Hope that helps,
Ryan
> Hello. We are working on a python assignment for our Synoptic Lab. The goal
> of the assignment is to create a program that plots NBM data on a map, and
> while we are able to plot data, there are horizontal lines that show up in
> the output. When trying different forecast hours, or a different parameter
> for the plot, the amount of lines differs. I have provided screenshots of the
> code and what the program is outputting. Is there any way that this can be
> resolved? Thank you.
>
Ticket Details
===================
Ticket ID: CWW-995296
Department: Support Python
Priority: Low
Status: Closed
===================
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata
inquiry tracking system and then made publicly available through the web. If
you do not want to have your interactions made available in this way, you must
let us know in each email you send to us.