-
Notifications
You must be signed in to change notification settings - Fork 11
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
Labels on SkewT lines and legend in hodograph #35
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
26ebc4e
Add line labels to special lines.
christinaholtNOAA 079665b
Add legend to hodograph.
christinaholtNOAA cae6109
Lint.
christinaholtNOAA 4fb4765
Updating info for running tests.
christinaholtNOAA 15ac8f1
Tiny bit better angles on labels.
christinaholtNOAA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,15 @@ | |
import matplotlib.font_manager as fm | ||
import matplotlib.pyplot as plt | ||
from matplotlib.ticker import FixedLocator | ||
from matplotlib.lines import Line2D | ||
import metpy.calc as mpcalc | ||
from metpy.plots import Hodograph, SkewT | ||
from metpy.units import units | ||
from mpl_toolkits.axes_grid1.inset_locator import inset_axes | ||
|
||
import adb_graphics.datahandler.grib as grib | ||
import adb_graphics.errors as errors | ||
import adb_graphics.utils as utils | ||
|
||
class SkewTDiagram(grib.profileData): | ||
|
||
|
@@ -63,13 +65,13 @@ def _add_thermo_inset(self, skew): | |
|
||
# Sure would have been nice to use a variable in the f string to | ||
# denote the format per variable. | ||
line = f"{name.upper():<7s}: {str(value):>10} {items['units']}" | ||
line = f"{name.upper():<7s} {str(value):>6} {items['units']}" | ||
lines.append(line) | ||
|
||
contents = '\n'.join(lines) | ||
|
||
# Draw the text box | ||
skew.ax.text(0.70, 0.98, contents, | ||
skew.ax.text(0.78, 0.98, contents, | ||
bbox=dict(facecolor='white', edgecolor='black', alpha=0.7), | ||
fontproperties=fm.FontProperties(family='monospace'), | ||
size=8, | ||
|
@@ -170,40 +172,44 @@ def _plot_hodograph(self, skew): | |
# | ||
agl = np.copy(self.atmo_profiles.get('gh', {}).get('data')).to('km') | ||
|
||
heights = [10, 3, 1] | ||
agl_arr = agl.magnitude | ||
for i, height in enumerate(heights): | ||
|
||
mag_top = height | ||
mag_bottom = 0 if i >= len(heights) - 1 else heights[i+1] | ||
|
||
# Use exclude later to remove values above 10km | ||
if i == 0: | ||
exclude = -np.sum(agl_arr > mag_top) | ||
|
||
# Check for the values between two levels | ||
condition = np.logical_and(agl_arr <= mag_top, agl_arr > mag_bottom) | ||
agl.magnitude[condition] = mag_top | ||
|
||
# Note: agl is now an array with values corresponding to the heights | ||
# array | ||
|
||
# Retrieve the wind data profiles | ||
u_wind = self.atmo_profiles.get('u', {}).get('data') | ||
v_wind = self.atmo_profiles.get('v', {}).get('data') | ||
|
||
# Drop the points above 10 km | ||
u_wind = u_wind.magnitude[:exclude] * u_wind.units | ||
v_wind = v_wind.magnitude[:exclude] * v_wind.units | ||
|
||
# Create an inset axes object that is 28% width and height of the | ||
# figure and put it in the upper left hand corner. | ||
ax = inset_axes(skew.ax, '25%', '25%', loc=2) | ||
h = Hodograph(ax, component_range=80.) | ||
h.add_grid(increment=20, linewidth=0.5) | ||
|
||
intervals = [0, 1, 3, 10] * agl.units | ||
colors = ['xkcd:salmon', 'xkcd:aquamarine', 'xkcd:navy blue'] | ||
line_width = 1.5 | ||
|
||
# Plot the line colored by height AGL only up to the 10km level | ||
h.plot_colormapped(u_wind, v_wind, agl[:exclude], linewidth=2) | ||
lines = h.plot_colormapped(u_wind, v_wind, agl, | ||
colors=colors, | ||
intervals=intervals, | ||
linewidth=line_width, | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This next bit was totally Stack Overflow magic. :) |
||
# Local function to create a proxy line object for creating a legend on | ||
# a LineCollection returned from plot_colormapped. Using lines and | ||
# colors from outside scope. | ||
def make_proxy(zval, idx=None, **kwargs): | ||
color = colors[idx] if idx < len(colors) else lines.cmap(zval-1) | ||
return Line2D([0, 1], [0, 1], color=color, linewidth=line_width, **kwargs) | ||
|
||
# Make a list of proxies | ||
proxies = [make_proxy(item, idx=i) for i, item in | ||
enumerate(intervals.magnitude)] | ||
|
||
# Draw the legend | ||
ax.legend(proxies[:-1], | ||
['0-1 km', '1-3 km', '3-10 km', ''], | ||
fontsize='small', | ||
loc='lower left', | ||
) | ||
|
||
@staticmethod | ||
def _plot_labels(skew): | ||
|
@@ -289,13 +295,19 @@ def _setup_diagram(self): | |
which='major', | ||
) | ||
|
||
# Add the relevant special lines | ||
# Add the relevant special lines with their labels | ||
dry_adiabats = np.arange(-40, 210, 10) * units.degC | ||
skew.plot_dry_adiabats(dry_adiabats, | ||
colors='tan', | ||
linestyles='solid', | ||
linewidth=0.7, | ||
) | ||
utils.label_lines(ax=skew.ax, | ||
lines=skew.dry_adiabats, | ||
labels=dry_adiabats.magnitude, | ||
end='top', | ||
offset=1, | ||
) | ||
|
||
moist_adiabats = np.arange(8, 36, 4) * units.degC | ||
moist_pr = np.arange(1001, 220, -10) * units.hPa | ||
|
@@ -305,6 +317,11 @@ def _setup_diagram(self): | |
linestyles='solid', | ||
linewidth=0.7, | ||
) | ||
utils.label_lines(ax=skew.ax, | ||
lines=skew.moist_adiabats, | ||
labels=moist_adiabats.magnitude, | ||
end='top', | ||
) | ||
|
||
mixing_lines = np.array([1, 2, 3, 5, 8, 12, 16, 20]).reshape(-1, 1) / 1000 | ||
mix_pr = np.arange(1001, 400, -50) * units.hPa | ||
|
@@ -313,6 +330,11 @@ def _setup_diagram(self): | |
linestyles=(0, (5, 10)), | ||
linewidth=0.7, | ||
) | ||
utils.label_lines(ax=skew.ax, | ||
lines=skew.mixing_lines, | ||
labels=mixing_lines * 1000, | ||
) | ||
|
||
return skew | ||
|
||
@property | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reduced footprint size and scooted the inset over just a tad.