This script illustrates the following concepts:
  • Labeling both Y axes

  • Changing padding for axis labels

  • Setting line color and width

See following URLs to see the reproduced NCL plot & script:

Import packages

import numpy as np
import xarray as xr
import matplotlib.pyplot as plt

import geocat.datafiles as gdf
import geocat.viz as gv

Read in data:

# Open a netCDF data file using xarray default engine and load data into xarray
data = xr.open_dataset(gdf.get("netcdf_files/"))

# Select data to be plotted
U = data.U[0, :, 0]


# Generate figure (set its size (width, height) in inches) and axes
plt.figure(figsize=(5.5, 5))
ax = plt.gca()

# Create second y axis
ax1 = ax.twinx()

# Plot the specific slice of the data with the correct color and linewidth
U.plot(x="lat", color="gray", linewidth=1.1)

# Turn off automatic title

# Use geocat.viz.util convenience function to add minor and major tick lines

# Use geocat.viz.util convenience function to set axes parameters without calling several matplotlib functions
# Set axes limits, tick values, and tick labels to show latitude & longitude (i.e. North (N) - South (S))
    xlim=(-90, 90),
    ylim=(-10, 40),
    xticks=np.linspace(-90, 90, 7),
    xticklabels=['90S', '60S', '30S', '0', '30N', '60N', '90N'])
gv.set_axes_limits_and_ticks(ax1, ylim=(-10, 40), yticklabels=[])

# Use geocat.viz.util convenience function to set titles and labels
gv.set_titles_and_labels(ax, ylabel="Left Y axis string")

# Set label on second y axis
ax1.set_ylabel("Right Y axis string", labelpad=18, fontsize=16)

# Show the plot
NCL text 11

Total running time of the script: ( 0 minutes 0.278 seconds)

Gallery generated by Sphinx-Gallery