-
Notifications
You must be signed in to change notification settings - Fork 388
Fix masking for initial state plotting #512
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
Fix masking for initial state plotting #512
Conversation
Cells below maxLevelCell are no longer included.
|
I also switched the text with max/min in the plot to a monospace font so the columns align better. |
| import matplotlib | ||
| matplotlib.use('Agg') | ||
| import matplotlib.pyplot as plt | ||
| from matplotlib.font_manager import FontProperties |
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.
Usually, you need to switch to the Agg backend before importing plt. I guess that isn't required in the most recent matplotlib but it's still safer to do it in this order. PEP8 will complain about imports not being at the top but it is necessary in this case.
| plt.subplot(3, 3, 4) | ||
| varName = 'temperature' | ||
| var = ncfile.variables[varName] | ||
| plt.hist(np.ndarray.flatten(var[:]), bins=100, log=True) |
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.
The mask takes care of of flattening the array in this case but a more efficient way of flattening a typical numpy array is to call its ravel method: var[:].ravel()
| txt = txt + ' {:9.2e}'.format(np.min(var)) + \ | ||
| ' {:9.2e}'.format(np.max(var)) + ' ' + varName + '\n' |
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.
I didn't have the energy to change all the formatting, but I would not use str addition like this. For example, here, I would do:
| txt = txt + ' {:9.2e}'.format(np.min(var)) + \ | |
| ' {:9.2e}'.format(np.max(var)) + ' ' + varName + '\n' | |
| txt = '{}{:9.2e} {:9.2e} {}\n'.format(txt, np.amin(var), np.amax(var), varName) |
I think amin and amax are considered to be the better than the aliases min and max because it clarifies that you want the min/max over the array vs. minimum and maximum, which give the element-wise min/max between two arrays.
| font = FontProperties() | ||
| font.set_family('monospace') | ||
| font.set_size(12) | ||
| print(txt) | ||
| plt.subplot(3, 3, 1) | ||
| plt.text(0, 1, txt, fontsize=12, verticalalignment='top') | ||
| plt.text(0, 1, txt, verticalalignment='top', fontproperties=font) |
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.
Apparently, this is how you change fonts. Never had to do this before.
|
@xylar you are too good! Thanks for the changes. I appreciate the python improvements as well! |

Cells below
maxLevelCellare no longer included.