This page demonstrates Python tips and tricks that I use in my everyday programming as an atmospheric science graduate student.
-Brian Blaylock

Tuesday, May 21, 2019

Cartopy: add NEXRAD mosaic image to figure

This Notebook shows how I add NEXRAD mosaic images from Iowa State University to my Cartopy maps...

https://github.com/blaylockbk/pyBKB_v3/blob/master/BB_maps/cartopy_NEXRAD-mosaic-from-Iowa.ipynb


Slowly, like a sloth, I'm using cartopy more and more.

Monday, May 20, 2019

Add an ArcGIS Map Service Image to a Cartopy axes.

I am a very slow Cartopy adopter. I learned Basemap years ago and am very comfortable with it and continue to hold tight to what I know. However, as I dabble more with Cartopy, I can see how it is superior.

For the last two years, one of my many reasons for sticking with Basemap despite almsot everyone telling me to stop, that the ease of adding arcGIS images as a map background. In Basemap, it is a simple m.arcgisimage(service='World_Shaded_Relief'). It took me a long time and many attempts to figure out how to do this in Cartopy. It does take a few extra steps, but it is possible, and even without the ease of use, I am now believe Cartopy does this better than Basemap.

In Basemap, you are limited to adding arcGIS images to cylindrical projection, but in Cartopy you can add these images to any projection. Incredible!

See the full notebook here, https://github.com/blaylockbk/pyBKB_v3/blob/master/BB_maps/cartopy_arcgisimage.ipynb.


Additional Resources:

NOTE: When you set the extent with ax.set_extent, the images are fine until you cross the prime meridian, then the images are distorted as if there aren't enough tiles. The only way I've been able to prevent this is to keep the image in the same projection as the tiles, a.k.a. the Mercator projection (see the second stack overflow answer linked above).

import cartopy.io.img_tiles as cimgt

fig=plt.figure(figsize=(10,5))
ax = fig.add_subplot(1,1,1, projection=ccrs.PlateCarree())
ax.coastlines()

url = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Shaded_Relief/MapServer/tile/{z}/{y}/{x}.jpg'

image = cimgt.GoogleTiles(url=url)

ax.add_image(image, 1)




Wednesday, April 17, 2019

GOES-16 and GOES-17 Overlay

I have successfully plotted GOES-16 and GOES-17 true color images together on the same plot. GOES16 was plotted first, then the GOES17 layer was added on top.

Details on GitHub:
https://github.com/blaylockbk/pyBKB_v3/blob/master/BB_GOES/Both_GOES16_and_GOES17.ipynb


Monday, April 23, 2018

Tips for Publication-worthy Matplotlib Figures

Matplotlib is the graphical plotting package for Python and is heavily used to create. Here are a few tips to create a publication-quality image in Python. There is nothing worse than seeing a poorly made, blurry, or crowded figure when you read a journal article.

The American Meteorological Society has a document that explains best practices to make your figures. These are good rules of thumb to follow.

  1. Create each figure in its own Jupyter Notebook. I recently learned why Jupyter Notebooks are so amazing...it's a great way to document and cleanly see each step. You can easily share notebooks and upload them to GitHub. They also give you the power to make minor changes and do figure 'touch-ups'. These touch-ups are what will make your figures look amazing. 
  2. Pay attention to the dpi: dpi is the 'dots per inch'. A high dpi will make your images look crisp. Generally, anything less than 150 is ideal for viewing on a web, but for print, you want at least 300 dpi. If you figure is just black and white, use at least 1000 dpi. If it includes shades of grey or colors it is acceptable to bump the dpi down to 600. 
  3. Do not manually resize your picture after it has been created. For example, do not make a large picture, load it into PowerPoint, and then shrink the image. This will distort the image, change the dpi, make lines too thin, and cause all sorts of asthetic problems. It is best to make the image the size you want it to be from the beginning.
  4. Pay careful consideration to label, titles, and legends. You want these to be readable, so pay attention to their font size. In fact, most of the time you can get away with not using a figure title--that is the purpose of the figure caption.
  5. Do not use the `Jet` color map. Jet is just horrible. If you need some convincing, then watch this. You can also read a million other articles why Jet is horrible.
  6. Use colors that make sense. If you are showing two diverging values, like negative-ness and positive-ness of a value, use a diverging color map. If you are illustrating hot temperatures, use red.
  7. Consider the right marker shape.
In general, make your figures look good. It is worth spending extra time to make your figures look good. After all, the pretty pictures in your article are the most-looked-at part of your paper.

Tuesday, November 7, 2017

More Python Color maps: cmocean

Additional, built-in colormaps can be found by importing cmocean
http://matplotlib.org/cmocean/

import cmocean
plt.plot(data, cmpa=cmocean.cm.temperature)

Options:
  • temperature
  • salinity
  • par
  • gray
  • oxygen
  • bathymetry
  • density
  • chlorophyll
  • CDOM
  • turbidity
  • speed
  • waveheight
  • waveperiod
  • phase
  • freesurface
  • velocity
  • vorticity
Don't use the Jet colormap!
http://www.research.ibm.com/people/l/lloydt/color/color.HTM