{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Modify a Colorbar for your Plot\n\nThis is an example of how to modify a colobar\nwithin a Py-ART display object.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(__doc__)\n\n# Author: Joe O'Brien (obrienj@anl.gov)\n# License: BSD 3 clause\n\nimport cartopy.crs as ccrs\nimport matplotlib.pyplot as plt\n\nimport pyart\nfrom pyart.testing import get_test_data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initial PPI Map Display\n\nLet's plot a PPI Map Display\nand take a look at the colorbar\n\nNotice: the colorbar is not perfect\nand slightly overlaps the PPI display\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Define figure\nfig = plt.figure()\n\n# Create a subplot with correct cartopy projection\naxs = plt.subplot(111, projection=ccrs.PlateCarree())\n\n# Define and Read in the test data\nradar_file = get_test_data(\"swx_20120520_0641.nc\")\nradar = pyart.io.read(radar_file)\n\n# Create the Radar Map Display (defines x,y as lat/lons)\ndisplay = pyart.graph.RadarMapDisplay(radar)\n\n# Display the horizontal equivalent reflectivity factor\n# Note: embellish = False will remove initial lat/lon coords\ndisplay.plot_ppi_map(\n \"reflectivity_horizontal\",\n 2,\n ax=axs,\n vmin=-30,\n vmax=60,\n embellish=False,\n norm=None,\n cmap=\"HomeyerRainbow\",\n)\n\n# Add gridlines\ngl = axs.gridlines(\n crs=ccrs.PlateCarree(),\n draw_labels=True,\n linewidth=1,\n color=\"gray\",\n alpha=0.3,\n linestyle=\"--\",\n)\n\nplt.gca().xaxis.set_major_locator(plt.NullLocator())\n\n# Make sure labels are only plotted on the left and bottom\ngl.top_labels = False\ngl.right_labels = False\n\ngl.xlabel_style = {\"size\": 14}\ngl.ylabel_style = {\"size\": 14}\n\n# delete the display object\ndel display" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Colorbar Position / Title Manipulation\n\nNow, let's update the colorbar position\nto match the display\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Define figure\nfig = plt.figure()\n\n# Create a subplot with correct cartopy projection\naxsB = plt.subplot(111, projection=ccrs.PlateCarree())\n\n# Create the Radar Map Display (defines x,y as lat/lons)\ndisplay = pyart.graph.RadarMapDisplay(radar)\n\n# Create the display again\nppi_map = display.plot_ppi_map(\n \"reflectivity_horizontal\",\n 2,\n ax=axsB,\n vmin=-30,\n vmax=60,\n embellish=False,\n norm=None,\n cmap=\"HomeyerRainbow\",\n)\n\n# Add gridlines\ngl = axsB.gridlines(\n crs=ccrs.PlateCarree(),\n draw_labels=True,\n linewidth=1,\n color=\"gray\",\n alpha=0.3,\n linestyle=\"--\",\n)\n\nplt.gca().xaxis.set_major_locator(plt.NullLocator())\n\n# Make sure labels are only plotted on the left and bottom\ngl.top_labels = False\ngl.right_labels = False\n\n# Define the size of the grid labels\ngl.xlabel_style = {\"size\": 12}\ngl.ylabel_style = {\"size\": 12}\n\n# Define the colorbar from the RadarMapDisplay object\ncbar = display.cbs[0]\n# Modify the colorbar label and size\ncbar.set_label(label=\"Horizontal Reflectivity Factor ($Z_{H}$) (dBZ)\", fontsize=12)\n# Modify the number of colorbar ticks\ncbar.set_ticks([-20, 0, 20, 40, 60])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.3" } }, "nbformat": 4, "nbformat_minor": 0 }