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
# For overlaying two colorbars on the same figure | |
plt.rcParams['hatch.color'] = (1, 1, 1, .33) | |
plt.rcParams['hatch.linewidth'] = 8 | |
plt.bar([1,2], [2,5], color='.1', label='first') | |
plt.bar([1,2], [3,4], hatch='/', alpha=.33, color='g', label='second') | |
plt.legend() |
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
#========================================================================== | |
## Simple method, just deal with the '~' notation for home | |
#========================================================================== | |
import os | |
FILE = "~/path/to/file.data" | |
FILE = FILE.replace('~', os.environ['HOME']) | |
#========================================================================== | |
#========================================================================== | |
## More involved, but deals with other environment variables |
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
def str_operator(left, operator_str, right): | |
""" | |
Performs an operation when you have an operator as a string. | |
An alternative method is to use the `eval()` function, if you aren't | |
worried about the security vulnerabilities. | |
Example, `eval('a + b')` where a=5 and b=6 will result in 11. | |
Parameters | |
---------- |
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
def border(array, corner=0, direction='cw'): | |
""" | |
Extract the values arround the border of a 2d array. | |
Default settings start from top left corner and move clockwise. | |
Corners are only used once. | |
Parameters | |
---------- | |
array : array_like |
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
from datetime import datetime | |
DATE = datetime(2019, 1, 1, 12, 35) | |
print(DATE) | |
# The most common way, with formatters | |
# https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior | |
d1 = DATE.strftime('%Y %b %d, %H:%M') | |
print('The date and time is %s' % d1) |
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
def xr_infer_interval_breaks(coord, axis=0): | |
""" | |
Code from xarray: (github/pydata/xarray/plot/utils) | |
Usage: if x is a 2d array of longitudes... | |
x = _infer_interval_breaks(x, axis=1) | |
x = _infer_interval_breaks(x, axis=0) # repeat for second axis | |
""" | |
deltas = 0.5 * np.diff(coord, axis=axis) | |
first = np.take(coord, [0], axis=axis) - np.take(deltas, [0], axis=axis) | |
last = np.take(coord, [-1], axis=axis) + np.take(deltas, [-1], axis=axis) |
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
# Brian Blaylock | |
# Requres `s3fs` | |
# Website: https://s3fs.readthedocs.io/en/latest/ | |
# In Anaconda, download via conda-forge. | |
import s3fs | |
# Use the anonymous credentials to access public data | |
fs = s3fs.S3FileSystem(anon=True) |
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
# For a .csv file where there are mixed data types (column of floats, column of strings i.e. Dates) | |
# then if you set the encoding='UTF-8', the dates or column of strings will return as a string | |
# rather than a "bytes" type. | |
FILENAME = 'some_csv_file.csv' | |
data = np.genfromtxt(FILENAME, delimiter=',', names=True, dtype=None, encoding='UTF-8') | |
# Since names is set to True, the returned data can be accessed by header column name. | |
# If a column is labeled 'DATES', you can access the dates like you do a dictionary... | |
get_dates = data['DATES'] |
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
import matplotlib | |
# Generate a list of colors | |
cmap = matplotlib.cm.get_cmap('Spectral') | |
# Retrieve 19 colors from the colormap | |
colors = cmap(np.arange(19)/19) |
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
# Print a progress bar for a loop completion | |
from time import sleep | |
F = range(10) | |
num = len(F) | |
for i in F: | |
sleep(1) | |
sys.stdout.write('\r%.1f%% Complete (%s of %s)' % (i/num*100, i, num)) |