-
Notifications
You must be signed in to change notification settings - Fork 42
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
Add XY_7_2 plot #28
Merged
Merged
Add XY_7_2 plot #28
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
03baf9c
Add XY_7_2 plot
andersy005 de9e7fb
format with black
andersy005 7012f24
Update data sub-dir
andersy005 26c4800
Update data submodule tracking to latest commit
andersy005 b75b18f
Fix missing import
andersy005 9061c60
Fix tick_params
andersy005 3c3db91
Final changes
andersy005 c24141a
Merge branch 'master' of github.com:NCAR/GeoCAT-examples into scatter-4
andersy005 2d5cea4
Fix missing data point issue
andersy005 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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
""" | ||
xy_7_2_lg.py | ||
=============== | ||
An example of a double y plot: two separate line with their own unique axis. | ||
|
||
https://www.ncl.ucar.edu/Applications/Images/xy_7_2_lg.png | ||
|
||
""" | ||
|
||
############################################################################### | ||
# Import modules | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import xarray as xr | ||
|
||
################################################################################ | ||
# | ||
# open data file and extract a slice of the data | ||
# | ||
|
||
dset = xr.open_dataset("../../data/netcdf_files/TestData.xy3.nc") | ||
ds = dset.isel(case=0, time=slice(0, 35)) | ||
|
||
|
||
################################################################################ | ||
# | ||
# Create XY plot with two different Y axes | ||
# | ||
|
||
|
||
def nclize_axis(ax): | ||
""" | ||
Utility function to make plots look like NCL plots | ||
""" | ||
import matplotlib.ticker as tic | ||
|
||
ax.tick_params(labelsize="small") | ||
ax.minorticks_on() | ||
ax.xaxis.set_minor_locator(tic.AutoMinorLocator(n=3)) | ||
ax.yaxis.set_minor_locator(tic.AutoMinorLocator(n=3)) | ||
|
||
# length and width are in points and may need to change depending on figure size etc. | ||
ax.tick_params( | ||
"both", | ||
length=8, | ||
width=1.5, | ||
which="major", | ||
bottom=True, | ||
top=True, | ||
left=True, | ||
right=True, | ||
) | ||
ax.tick_params( | ||
"both", | ||
length=5, | ||
width=0.75, | ||
which="minor", | ||
bottom=True, | ||
top=True, | ||
left=True, | ||
right=True, | ||
) | ||
|
||
|
||
fig, ax1 = plt.subplots(figsize=(12, 12)) | ||
|
||
fontsize = 24 | ||
labelsize = 20 | ||
color = "blue" | ||
linestyle = "-" | ||
nclize_axis(ax1) | ||
ax1.set_xlabel(ds.time.long_name, fontsize=fontsize) | ||
ax1.set_ylabel(f"{ds.T.long_name} [solid]", fontsize=fontsize) | ||
ax1.plot(ds.time, ds.T, color=color, linestyle=linestyle, linewidth=2.0) | ||
ax1.tick_params(axis="both", labelsize=labelsize) | ||
ax1.set_xlim(xmin=1970, xmax=1973) | ||
ax1.set_ylim(ymin=0.0, ymax=16.0) | ||
ax1.set_yticks(np.arange(0, 16, 3)) | ||
|
||
|
||
ax2 = ax1.twinx() | ||
nclize_axis(ax2) | ||
|
||
|
||
color = "red" | ||
linestyle = "--" | ||
# we already handled the x-label with ax1 | ||
ax2.set_ylabel(f"{ds.P.long_name} [dash]", fontsize=fontsize) | ||
ax2.plot(ds.time, ds.P, color=color, linestyle=linestyle, linewidth=2.0) | ||
ax2.tick_params(axis="both", labelsize=labelsize) | ||
ax2.set_ylim(ymin=1008.0, ymax=1024.0) | ||
ax2.set_yticks(np.arange(1008, 1024, 3)) | ||
|
||
|
||
plt.title("Curves Offset", fontsize=fontsize) | ||
|
||
fig.tight_layout() | ||
plt.show() |
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.
Although explicit initialization of variables such as
fontsize
andlabelsize
contribute to the readability of the code since they are used as is in both plots, how about using variables such ascolor
andlinestyle
with immediate values within plot codes instead of explicitly initializing them (because they have changing values for each plot)?