Skip to content

Commit

Permalink
Merge pull request #478 from NCAR/clean-up
Browse files Browse the repository at this point in the history
Clean up plotting bugs
  • Loading branch information
Heather Craker committed Jul 18, 2022
2 parents 3bf613a + 60acc7c commit 93c47a8
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 50 deletions.
9 changes: 7 additions & 2 deletions Gallery/Contours/NCL_ce_3_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,19 @@
# Contourf-plot data
heatmap = t.plot.contourf(ax=ax,
transform=projection,
levels=40,
levels=np.arange(0, 32, .5),
vmin=0,
vmax=32,
cmap=newcmp,
add_colorbar=False)

# Add colorbar
cbar = plt.colorbar(heatmap, ticks=np.arange(0, 32, 2))
cbar = plt.colorbar(heatmap,
shrink=0.8,
drawedges=True,
ticks=np.arange(0, 32, 2),
extendrect=True,
extendfrac='auto')
cbar.ax.set_yticklabels([str(i) for i in np.arange(0, 32, 2)])

# Usa geocat.viz.util convenience function to set axes parameters without calling several matplotlib functions
Expand Down
9 changes: 7 additions & 2 deletions Gallery/Contours/NCL_ce_3_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,19 @@
# Contourf-plot data
heatmap = t.plot.contourf(ax=ax,
transform=projection,
levels=40,
levels=np.arange(0, 32, .5),
vmin=0,
vmax=32,
cmap=newcmp,
add_colorbar=False)

# Add colorbar
cbar = plt.colorbar(heatmap, ticks=np.arange(0, 32, 2))
cbar = plt.colorbar(heatmap,
shrink=0.8,
drawedges=True,
ticks=np.arange(0, 32, 2),
extendrect=True,
extendfrac='auto')
cbar.ax.set_yticklabels([str(i) for i in np.arange(0, 32, 2)])

# Adjust tick label size
Expand Down
81 changes: 72 additions & 9 deletions Gallery/Contours/NCL_color_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
===============
This script illustrates the following concepts:
- Recreating a default NCL colormap
- Using Geocat-Viz Contour
See following URLs to see the reproduced NCL plot & script:
- Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/color_1.ncl
Expand All @@ -19,8 +18,10 @@
# Import packages:

import matplotlib.pyplot as plt
import xarray as xr
import cartopy.crs as ccrs
import cmaps
import numpy as np
import xarray as xr

import geocat.viz as gv
import geocat.datafiles as gdf
Expand All @@ -30,16 +31,78 @@

# Open a netCDF data file using xarray default engine and load the data into xarray
ds = xr.open_dataset(gdf.get("netcdf_files/uv300.nc")).isel(time=1)
U = ds.U

# Use geocat-viz utility function to handle the no-shown-data
# artifact of 0 and 360-degree longitudes
U = gv.xr_add_cyclic_longitudes(U, 'lon')

###############################################################################
# Plot with geocat.viz
# Generate figure (set its size (width, height) in inches)
plt.figure(figsize=(12, 8))

# Generate axes, using Cartopy
projection = ccrs.PlateCarree()
ax = plt.axes(projection=projection)

# Use global map and draw coastlines
ax.set_global()
ax.coastlines()

# Import the default NCL colormap
newcmp = cmaps.ncl_default

# Contourf-plot data (for filled contours)
# Note, min-max contour levels are hard-coded. contourf's automatic contour value selector produces fractional values.
p = U.plot.contourf(ax=ax,
vmin=-16.0,
vmax=44,
levels=16,
cmap=newcmp,
add_colorbar=False,
transform=projection,
extend='neither')

# Contour-plot data (for contour lines)
# Note, min-max contour levels are hard-coded. contourf's automatic contour value selector produces fractional values.
U.plot.contour(ax=ax,
vmin=-16.0,
vmax=44,
levels=16,
colors='black',
linewidths=0.5,
transform=projection)

# Add horizontal colorbar
cbar = plt.colorbar(p,
orientation='horizontal',
shrink=0.75,
drawedges=True,
aspect=16,
pad=0.075)
cbar.ax.tick_params(labelsize=14)
cbar.set_ticks(np.linspace(-12, 40, 14))

# Use geocat.viz.util convenience function to set axes tick values
gv.set_axes_limits_and_ticks(ax,
xticks=np.linspace(-180, 180, 13),
yticks=np.linspace(-90, 90, 7))

# Use geocat.viz.util convenience function to make plots look like NCL plots by using latitude, longitude tick labels
gv.add_lat_lon_ticklabels(ax)

fig = plt.figure(figsize=(12, 8))
# Use geocat.viz.util convenience function to add minor and major tick lines
gv.add_major_minor_ticks(ax, labelsize=14)

gv.Contour(ds.U,
main_title="Default Color",
fig=fig,
other=fig,
cmap=cmaps.ncl_default)
# Use geocat.viz.util convenience function to add titles to left and right of the plot axis.
gv.set_titles_and_labels(ax,
maintitle="NCL Default Colors",
lefttitle=U.long_name,
lefttitlefontsize=16,
righttitle=U.units,
righttitlefontsize=16,
xlabel="",
ylabel="")

# Show the plot
plt.show()
9 changes: 8 additions & 1 deletion Gallery/Contours/NCL_conLev_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@
add_labels=False)

# Add horizontal colorbar
cbar = plt.colorbar(p, orientation='horizontal', shrink=0.5)
cbar = plt.colorbar(p,
orientation='horizontal',
shrink=0.6,
extendrect=True,
extendfrac='auto',
pad=0.075,
aspect=15,
drawedges=True)
cbar.ax.tick_params(labelsize=11)
cbar.set_ticks([-12, -10, -8, -6, -4, -2, -1, 1, 2, 4, 6, 8, 10, 12])

Expand Down
2 changes: 1 addition & 1 deletion Gallery/Contours/NCL_conwomap_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
add_labels=False)

# Add horizontal colorbar
cbar = plt.colorbar(p, orientation='horizontal', shrink=0.5)
cbar = plt.colorbar(p, orientation='horizontal', shrink=0.5, drawedges=True)
cbar.ax.tick_params(labelsize=16)
cbar.set_ticks(np.linspace(0, 9, 10))

Expand Down
4 changes: 3 additions & 1 deletion Gallery/Contours/NCL_eof_1_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ def make_contour_plot(ax, dataset):
orientation='horizontal',
shrink=0.9,
pad=0.05,
fraction=.02)
fraction=.02,
extendrect=True,
extendfrac='auto')
cbar.ax.tick_params(labelsize=8)

# Set a common title
Expand Down
23 changes: 15 additions & 8 deletions Gallery/Contours/NCL_lb_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
# Plot:

# Generate figure (set its size (width, height) in inches)
fig = plt.figure(figsize=(10, 10))
fig = plt.figure(figsize=(12, 6))

# Generate axes using Cartopy and draw coastlines
ax = plt.axes(projection=ccrs.PlateCarree())
Expand All @@ -48,19 +48,26 @@
newcmp = cmaps.wgne15

# Contourf-plot data (for filled contours)
a = wrap_v.plot.contourf(levels=14,
a = wrap_v.plot.contourf(levels=np.arange(-24, 25, 4),
cmap=newcmp,
add_colorbar=False,
add_labels=False)
# Contour-plot data (for borderlines)
wrap_v.plot.contour(levels=14, linewidths=0.5, cmap='black', add_labels=False)
wrap_v.plot.contour(levels=np.arange(-24, 25, 4),
linewidths=0.5,
cmap='black',
add_labels=False)

# Add vertical colorbar
clabels = [
"-70", "-50", "-30", "-10", "10", "30", "50", "70", "90", "110", "130",
"150"
]
cbar = fig.colorbar(a, label='', ticks=np.linspace(-24, 24, 12), shrink=0.4)
cbar = plt.colorbar(a,
ticks=np.arange(-20, 25, 4),
shrink=0.8,
aspect=10,
extendrect=True,
extendfrac='auto')

# Change the colorbar tick labels
clabels = np.arange(-70, 151, 20)
cbar.ax.set_yticklabels(clabels)

# Use geocat.viz.util convenience function to set axes limits & tick values without calling several matplotlib functions
Expand Down
24 changes: 14 additions & 10 deletions Gallery/Contours/NCL_lb_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
===========
This script illustrates the following concepts:
- Drawing a horizontal colorbar
- Changing the labelbar labels
- Adding a title to a labelbar
- Changing the font of the labelbar's labels
- Making the labelbar label fonts smaller
- Centering the labels inside each box in a labelbar
- Adding a vertical title to a labelbar
- Changing the colorbar labels
- Adding a title to a colorbar
- Changing the font of the colorbar's labels
- Making the colorbar label fonts smaller
- Centering the labels inside each box in a colorbar
- Adding a vertical title to a colorbar
See following URLs to see the reproduced NCL plot & script:
- Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/lb_4.ncl
Expand Down Expand Up @@ -116,18 +116,22 @@
cbar.ax.get_xaxis().set_ticks([])

# Draw text in the center of each box
offset = 1 / 13 / 2
# The origin of the colorbar xaxis starts at right edge of the leftmost box
# The offset moves the test left to the center of each box
# The i/11 comes from the 11 main boxes on the colorbar since the boxes on each
# end are the extensions for values that fall outside the colorbar rand
offset = 3 / 22
for i in range(1, 14):
cbar.ax.text(i / 13 - offset,
cbar.ax.text(i / 11 - offset,
0.45,
i,
horizontalalignment='center',
verticalalignment='center',
fontweight='bold',
transform=cbar.ax.transAxes)

# Draw the colorbar title
cbar.ax.text(1.1,
# Draw the colorbar units
cbar.ax.text(1.15,
0.5,
V.units,
horizontalalignment='center',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
shrink=0.8,
pad=0.05,
extendrect=True,
ticks=cbar_ticks)
ticks=cbar_ticks,
drawedges=True)

cbar.ax.tick_params(labelsize=10)

Expand Down
2 changes: 1 addition & 1 deletion Gallery/Overlays/NCL_overlay_11a.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
ticks=clevs[1:-1],
drawedges=True,
orientation='horizontal')
cbar.ax.tick_params(labelsize=12)
cbar.ax.tick_params(labelsize=12, rotation=45)

# Add the land mask feature on top of the contour plot (higher zorder)
ax.add_feature(land_mask, zorder=2)
Expand Down
5 changes: 2 additions & 3 deletions Gallery/Overlays/NCL_overlay_12.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,8 @@
# Manually set color bar tick length and tick labels padding.
clb2.ax.yaxis.set_tick_params(length=0, pad=18, labelsize=14)

# Center align colorbar tick labels
ticklabs = clb2.ax.get_yticklabels()
clb2.ax.set_yticklabels(ticklabs, ha='center')
# Add tick labels and center align them
clb2.ax.set_yticklabels(levels, ha='center')

#
# Set axes features (tick formats and main title)
Expand Down
6 changes: 2 additions & 4 deletions Gallery/Panels/NCL_panel_18.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,9 @@
drawedges=True,
format='%.1f')

# Right align colorbar tick labels
# Remove colorbar tick marks and adjust label spacing
for cab in [cab1, cab2]:
ticklabs = cab.ax.get_yticklabels()
cab.ax.set_yticklabels(ticklabs, ha='right')
cab.ax.yaxis.set_tick_params(pad=30, length=0)
cab.ax.yaxis.set_tick_params(pad=10, length=0)

# Generate plot
plt.show()
17 changes: 10 additions & 7 deletions Gallery/Panels/NCL_panel_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def plot_labelled_filled_contours(data, ax=None):
contours, and the contour labels."""

# Import an NCL colormap, truncating it by using geocat.viz.util convenience function
newcmp = gv.truncate_colormap(cmaps.gui_default, minval=0.03, maxval=0.9)
newcmp = gv.truncate_colormap(cmaps.gui_default, minval=0.03, maxval=0.8)

handles = dict()
handles["filled"] = data.plot.contourf(
Expand Down Expand Up @@ -124,12 +124,15 @@ def plot_labelled_filled_contours(data, ax=None):
plot_labelled_filled_contours(ds.V, ax=ax[1])

# Add horizontal colorbar
cbar = plt.colorbar(handles["filled"],
ax=ax,
orientation="horizontal",
ticks=levels[:-1],
drawedges=True,
aspect=30)
cbar = plt.colorbar(
handles["filled"],
ax=ax,
orientation="horizontal",
ticks=levels[:-1], # keep the last colorbar tick from showing
drawedges=True,
extendrect=True,
extendfrac='auto',
aspect=30)
cbar.ax.tick_params(labelsize=10)

# Show the plot
Expand Down

0 comments on commit 93c47a8

Please sign in to comment.