Plotting results from SQLite files
A standard plotting routine is provided to generate figures from data generated by gfdlvitals that is stored in the SQLite data files.
Making plots within a Jupyter notebook
The code below illustrates how to use the plotting function on the
sample data provided with the gfdlvitals package. The plotting
function can take multiple gfdlvitals.VitalsDataFrame objects
if they are passed into the function as a list. There are additional
options to smooth the data, overlay a trend line, and align offset
time axes.
In [1]: import gfdlvitals
In [2]: df_ctrl = gfdlvitals.open_db(gfdlvitals.sample.picontrol);
In [3]: df_hist = gfdlvitals.open_db(gfdlvitals.sample.historical);
In [4]: fig = gfdlvitals.plot_timeseries([df_ctrl,df_hist],\
...: align_times=True,\
...: trend=True,\
...: smooth=20,\
...: var="t_ref",\
...: labels="Preindustrial Control,Historical");
...:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[4], line 1
----> 1 fig = gfdlvitals.plot_timeseries([df_ctrl,df_hist],\
2 align_times=True,\
3 trend=True,\
4 smooth=20,\
5 var="t_ref",\
6 labels="Preindustrial Control,Historical");
File ~/checkouts/readthedocs.org/user_builds/gfdlvitals/envs/latest/lib/python3.11/site-packages/gfdlvitals/plot.py:222, in plot_timeseries(dsets, var, trend, align_times, plottype, smooth, nyears, labels, legend, means, title)
217 _varname = var if (title in ["varname", "both"]) else ""
218 axes_dict[label]["topline_label"] = _ax.text(
219 0.01, 1.08, _varname, ha="left", transform=ax1.transAxes, fontsize=22
220 )
--> 222 _long_name = dset[var].attrs["long_name"]
223 _long_name = f"Sum of {_long_name}" if plottype == "sum" else _long_name
224 _long_name = _long_name if (title in ["longname", "both"]) else ""
KeyError: 'long_name'
Using the command-line interface
The same plotting function can be called from the command line and an X-window Matplotlib figure viewer will appear with the plot. The inputs to the script are the SQLite (*.db) files and the various options can specified with flags.
plotdb [-h] [-a] [-t] [-s SMOOTH] [-l LABELS] [-n NYEARS] DB FILES [DB FILES ...]
DB FILES: Path to input database files
-a, align: Align different time axes
-t, trend: Add trend lines to plots
-s, smooth: Apply a n-years smoother to all plots
-l, labels: Comma-separated list of dataset labels
-n, nyears: Limit the plotting to a set number of n years
Hint
Use the left and right arrows keys on the keyboard to cycle through different variables