Skip to content

Instantly share code, notes, and snippets.

@blaylockbk
blaylockbk / python_colorbar_on_axes.py
Last active March 22, 2023 14:20
Adding colorbar on axes
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
data1 = np.random.random([5,5])
data2 = np.random.random([5,5])
fig = plt.figure(1, figsize=[16, 4])
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
this_plot = ax1.pcolormesh(data1, cmap='magma')
@blaylockbk
blaylockbk / datetime_axis_format.py
Last active March 30, 2018 14:47
formating datetime on axis
# Method 1: when you are using plt
from matplotlib.dates import DateFormatter
formatter = DateFormatter('%Y-%m-%d %H:%M:%S')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)
# Method 2: when you have already axessed the axes
from matplotlib.dates import DateFormatter
formatter = DateFormatter('%Y-%m-%d %H:%M:%S')
ax1.xaxis.set_major_formatter(formatter)
@blaylockbk
blaylockbk / HRRR_S3_fastDwnld.py
Last active March 24, 2017 18:15
Fast download of grib2 files from HRRR S3
# Brian Blaylock
# March 24, 2017 Yesterday Salt Lake had lots of rain
"""
Fast download of HRRR grib2 files with threading
"""
from queue import Queue
from threading import Thread
from datetime import datetime, timedelta
@blaylockbk
blaylockbk / HRRR_S3.py
Last active November 28, 2018 03:00
Get data from the Mesowest HRRR S3 Archive
# Brian Blaylock
# March 14, 2017 It's Pi Day!! (3.14)
"""
Get data from a HRRR grib2 file on the MesoWest HRRR S3 Archive
Requires cURL on your linux system
"""
import os
@blaylockbk
blaylockbk / download_HRRR_variable_with_curl.py
Last active July 26, 2017 17:06
Download a single HRRR variable with cURL
# Brian Blaylock
# March 10, 2017
# updated: July 26, 2017
"""
Download a single variable from the HRRR archive using cURL
Steps:
1) Read the lines from the Metadata .idx file
2) Identify the byte range for the variable of interest
@blaylockbk
blaylockbk / download_HRRR_full_grib2.py
Last active December 10, 2018 17:01
Download a range of files from the MesoWest Pando HRRR archive
# Brian Blaylock
# February 13, 2018
# Updated December 10, 2018 for Python 3
"""
Download archived HRRR files from MesoWest Pando S3 archive system.
Please register before downloading from our HRRR archive:
http://hrrr.chpc.utah.edu/hrrr_download_register.html
@blaylockbk
blaylockbk / legends.py
Last active April 12, 2018 16:12
Python Legends
# More here: http://matplotlib.org/api/legend_api.html
# No boarder, transparent frame
legend = ax1.legend(frameon=True, framealpha=.5)
legend.get_frame().set_linewidth(0)
# Limit scatterpoints in legend
plt.legend(scatterpoints=1)
@blaylockbk
blaylockbk / special_string_labels.py
Last active March 9, 2021 18:31
Special String Formating and Characters
# Greek Letters
plt.title(r'$\theta$')
plt.title(r'$\alpha$')
plt.title(r'$\Delta$')
# Superscript (wind speed m/s)
plt.ylabel(r'Wind Speed (m s$\mathregular{^{-1}}$)')
# Subscript (CO_2, theta_surface)
plt.ylabel(r'CO$_2$ (ppm)')
@blaylockbk
blaylockbk / range_of_datetimes.py
Last active February 8, 2018 21:52
Range of Datetimes
from datetime import datetime, timedelta
# create a list of dates from a starting date for a certian number of hours
base = datetime(2017, 5, 10)
hours = 36
DATES = np.array([base + timedelta(hours=x) for x in range(0, hours)])
# create a list of datetimes between two dates for each hour
sDATE = datetime(2017, 5, 10)
eDATE = datetime(2017, 5, 12)
@blaylockbk
blaylockbk / mkdir_if_none.py
Last active September 20, 2018 21:18
make a directory if it doesn't exist, like a save dir, with os.path
import os
DIR = '/this/is/the/directory/name/'
if not os.path.exists(DIR):
os.makedirs(DIR)