Skip to content

Commit

Permalink
ADD: Colorbar Manipulation Example (#1300)
Browse files Browse the repository at this point in the history
* ADD: Modify Colorbar Example

* ENH: print statement removal, PEP8 fixes

* ENH: Made Requested Changes

* ENH: Removal of comment lines

* ENH: typo fix
  • Loading branch information
jrobrien91 authored Nov 16, 2022
1 parent 6572100 commit d2aea4a
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions examples/plotting/plot_modify_colorbar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"""
=================================
Modify a Colorbar for your Plot
=================================
This is an example of how to modify a colobar
within a Py-ART display object.
"""
print(__doc__)

# Author: Joe O'Brien ([email protected])
# License: BSD 3 clause

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs

import pyart
from pyart.testing import get_test_data

#########################################
# ** Initial PPI Map Display
#
# Let's plot a PPI Map Display
# and take a look at the colorbar
#
# Notice: the colorbar is not perfect
# and slightly overlaps the PPI display

# Define figure
fig = plt.figure()

# Create a subplot with correct cartopy projection
axs = plt.subplot(111, projection=ccrs.PlateCarree())

# Define and Read in the test data
radar_file = get_test_data('swx_20120520_0641.nc')
radar = pyart.io.read(radar_file)

# Create the Radar Map Display (defines x,y as lat/lons)
display = pyart.graph.RadarMapDisplay(radar)

# Display the horizontal equivalent reflectivity factor
# Note: embellish = False will remove initial lat/lon coords
display.plot_ppi_map('reflectivity_horizontal',
2,
ax=axs,
vmin=-30,
vmax=60,
embellish=False,
norm=None,
cmap='pyart_HomeyerRainbow')

# Add gridlines
gl = axs.gridlines(crs=ccrs.PlateCarree(),
draw_labels=True,
linewidth=1,
color='gray',
alpha=0.3,
linestyle='--')

plt.gca().xaxis.set_major_locator(plt.NullLocator())

# Make sure labels are only plotted on the left and bottom
gl.top_labels = False
gl.right_labels = False

gl.xlabel_style = {'size': 14}
gl.ylabel_style = {'size': 14}

# delete the display object
del display

############################################
# ** Colorbar Position / Title Manipulation

# Now, let's update the colorbar position
# to match the display

# Define figure
fig = plt.figure()

# Create a subplot with correct cartopy projection
axsB = plt.subplot(111, projection=ccrs.PlateCarree())

# Create the Radar Map Display (defines x,y as lat/lons)
display = pyart.graph.RadarMapDisplay(radar)

# Create the display again
ppi_map = display.plot_ppi_map('reflectivity_horizontal',
2,
ax=axsB,
vmin=-30,
vmax=60,
embellish=False,
norm=None,
cmap='pyart_HomeyerRainbow')

# Add gridlines
gl = axsB.gridlines(crs=ccrs.PlateCarree(),
draw_labels=True,
linewidth=1,
color='gray',
alpha=0.3,
linestyle='--')

plt.gca().xaxis.set_major_locator(plt.NullLocator())

# Make sure labels are only plotted on the left and bottom
gl.top_labels = False
gl.right_labels = False

# Define the size of the grid labels
gl.xlabel_style = {'size': 12}
gl.ylabel_style = {'size': 12}

# Define the colorbar from the RadarMapDisplay object
cbar = display.cbs[0]
# Modify the colorbar label and size
cbar.set_label(label='Horizontal Reflectivity Factor ($Z_{H}$) (dBZ)', fontsize=12)
# Modify the number of colorbar ticks
cbar.set_ticks([-20, 0, 20, 40, 60])

0 comments on commit d2aea4a

Please sign in to comment.