Skip to content

Commit f96a591

Browse files
authored
Merge pull request #45 from kathrynrooney/kr-climkern
refactorized plots in climkern notebook
2 parents 5365960 + c0f9fd4 commit f96a591

File tree

1 file changed

+105
-143
lines changed

1 file changed

+105
-143
lines changed

notebooks/foundations/climkern-calc.ipynb

Lines changed: 105 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,11 @@
254254
"source": [
255255
"# Our control simulation\n",
256256
"ctrl = ds_dict[\"CMIP.NCAR.CESM2.piControl.Amon.gn\"].isel(\n",
257-
" time=slice(-600,None)).squeeze().chunk({\"plev\":1,\"time\":-1})\n",
257+
" time=slice(-600,None)).squeeze().compute()\n",
258258
"\n",
259259
"# The increase CO2 aka \"perturbed\" simulation\n",
260260
"pert = ds_dict[\"CMIP.NCAR.CESM2.abrupt-4xCO2.Amon.gn\"].isel(\n",
261-
" time=slice(-360,None)).squeeze().chunk({\"plev\":1,\"time\":-1})"
261+
" time=slice(-360,None)).squeeze().compute()"
262262
]
263263
},
264264
{
@@ -366,7 +366,7 @@
366366
"cell_type": "markdown",
367367
"metadata": {},
368368
"source": [
369-
"Finally, let's plot the time average surface albedo feedback on a map, first as just the perturbation."
369+
"This function allows for the quick creation of a pre-configured map, eliminating the need to repeat setup code by setting up the figure, axes, and Cartopy Robinson projection."
370370
]
371371
},
372372
{
@@ -375,23 +375,69 @@
375375
"metadata": {},
376376
"outputs": [],
377377
"source": [
378-
"# Set a few variables\n",
379-
"proj = ccrs.Robinson()\n",
378+
"def base_map():\n",
379+
" proj= ccrs.Robinson() #select Robinson projection\n",
380380
"\n",
381-
"# Create figure and axis\n",
382-
"fig, ax = plt.subplots(subplot_kw=dict(projection=proj),\n",
383-
" layout=\"constrained\")\n",
381+
" fig, ax = plt.subplots(subplot_kw=dict(projection=proj)) #Create the figure and axis \n",
382+
" \n",
383+
" ax.coastlines() # Add coastlines\n",
384384
"\n",
385-
"# Plot surface albedo feedback & add coastlines\n",
386-
"alb.mean(dim=\"time\").plot(ax=ax,transform=ccrs.PlateCarree(),\n",
387-
" cbar_kwargs={\"orientation\": \"horizontal\", \"shrink\": 0.7,\n",
388-
" \"label\":\"W/m$^2$\"})\n",
389-
"ax.coastlines()\n",
385+
" return fig, ax\n",
386+
"\n"
387+
]
388+
},
389+
{
390+
"cell_type": "markdown",
391+
"metadata": {},
392+
"source": [
393+
"Similar to the function above, these dictionaries encapsulate the repetitive code setup and provide a set of reusable arguments to simplify the plotting of data onto this map. "
394+
]
395+
},
396+
{
397+
"cell_type": "code",
398+
"execution_count": null,
399+
"metadata": {},
400+
"outputs": [],
401+
"source": [
402+
"plotargs = {'transform': ccrs.PlateCarree(),\n",
403+
" 'cbar_kwargs': {\"orientation\": \"horizontal\", \"shrink\": 0.7, \n",
404+
" \"label\":\"W/m$^2$\"}\n",
405+
" }"
406+
]
407+
},
408+
{
409+
"cell_type": "code",
410+
"execution_count": null,
411+
"metadata": {},
412+
"outputs": [],
413+
"source": [
414+
"plotargs_2 = {'transform': ccrs.PlateCarree(),\n",
415+
" 'cbar_kwargs': {\"orientation\": \"horizontal\", \"shrink\": 0.7,\n",
416+
" \"label\":\"W/m$^2$/K\"}\n",
417+
" }"
418+
]
419+
},
420+
{
421+
"cell_type": "markdown",
422+
"metadata": {},
423+
"source": [
424+
"Finally, let's plot the time average surface albedo feedback on a map, first as just the perturbation."
425+
]
426+
},
427+
{
428+
"cell_type": "code",
429+
"execution_count": null,
430+
"metadata": {},
431+
"outputs": [],
432+
"source": [
433+
"# Create the base map\n",
434+
"fig, ax = base_map()\n",
390435
"\n",
391-
"# Add a title\n",
392-
"ax.set_title(\"Time-averaged Surface Albedo Feedback\")\n",
436+
"# Plot the surface albedo feedback \n",
437+
"alb.mean(dim=\"time\").plot(ax=ax, **plotargs)\n",
393438
"\n",
394-
"plt.show()"
439+
"# Add a title\n",
440+
"ax.set_title(\"Time-averaged Surface Albedo Feedback\");"
395441
]
396442
},
397443
{
@@ -407,23 +453,14 @@
407453
"metadata": {},
408454
"outputs": [],
409455
"source": [
410-
"# Set a few variables\n",
411-
"proj = ccrs.Robinson()\n",
412-
"\n",
413-
"# Create figure and axis\n",
414-
"fig, ax = plt.subplots(subplot_kw=dict(projection=proj),\n",
415-
" layout=\"constrained\")\n",
456+
"# Create the base map\n",
457+
"fig, ax = base_map()\n",
416458
"\n",
417459
"# Plot surface albedo feedback & add coastlines\n",
418-
"(alb.mean(dim=\"time\")/Δts.mean(dim=\"time\")).plot(ax=ax,transform=ccrs.PlateCarree(),\n",
419-
" cbar_kwargs={\"orientation\": \"horizontal\", \"shrink\": 0.7,\n",
420-
" \"label\":\"W/m$^2$/K\"})\n",
421-
"ax.coastlines()\n",
460+
"(alb.mean(dim=\"time\")/Δts.mean(dim=\"time\")).plot(ax=ax, **plotargs_2)\n",
422461
"\n",
423462
"# Add a title\n",
424-
"ax.set_title(\"Time-averaged Surface Albedo Feedback\")\n",
425-
"\n",
426-
"plt.show()"
463+
"ax.set_title(\"Time-averaged Surface Albedo Feedback\");"
427464
]
428465
},
429466
{
@@ -498,23 +535,14 @@
498535
"metadata": {},
499536
"outputs": [],
500537
"source": [
501-
"# Set a few variables\n",
502-
"proj = ccrs.Robinson()\n",
503-
"\n",
504-
"# Create figure and axis\n",
505-
"fig, ax = plt.subplots(subplot_kw=dict(projection=proj),\n",
506-
" layout=\"constrained\")\n",
538+
"# Create the base map\n",
539+
"fig, ax = base_map()\n",
507540
"\n",
508-
"# Plot surface albedo feedback & add coastlines\n",
509-
"lr.mean(dim=\"time\").plot(ax=ax,transform=ccrs.PlateCarree(),\n",
510-
" cbar_kwargs={\"orientation\": \"horizontal\", \"shrink\": 0.7,\n",
511-
" \"label\":\"W/m$^2$\"})\n",
512-
"ax.coastlines()\n",
541+
"# Plot the surface albedo feedback \n",
542+
"lr.mean(dim=\"time\").plot(ax=ax, **plotargs)\n",
513543
"\n",
514544
"# Add a title\n",
515-
"ax.set_title(\"Time-averaged Lapse Rate Feedback\")\n",
516-
"\n",
517-
"plt.show()"
545+
"ax.set_title(\"Time-averaged Lapse Rate Feedback\");"
518546
]
519547
},
520548
{
@@ -523,23 +551,14 @@
523551
"metadata": {},
524552
"outputs": [],
525553
"source": [
526-
"# Set a few variables\n",
527-
"proj = ccrs.Robinson()\n",
554+
"# Create the base map\n",
555+
"fig, ax = base_map()\n",
528556
"\n",
529-
"# Create figure and axis\n",
530-
"fig, ax = plt.subplots(subplot_kw=dict(projection=proj),\n",
531-
" layout=\"constrained\")\n",
532-
"\n",
533-
"# Plot surface albedo feedback & add coastlines\n",
534-
"(lr.mean(dim=\"time\")/Δts.mean(dim=\"time\")).plot(ax=ax,transform=ccrs.PlateCarree(),\n",
535-
" cbar_kwargs={\"orientation\": \"horizontal\", \"shrink\": 0.7,\n",
536-
" \"label\":\"W/m$^2$/K\"})\n",
537-
"ax.coastlines()\n",
557+
"# Plot surface albedo feedback\n",
558+
"(lr.mean(dim=\"time\")/Δts.mean(dim=\"time\")).plot(ax=ax, **plotargs_2)\n",
538559
"\n",
539560
"# Add a title\n",
540-
"ax.set_title(\"Time-averaged Lapse Rate Feedback\")\n",
541-
"\n",
542-
"plt.show()"
561+
"ax.set_title(\"Time-averaged Lapse Rate Feedback\");"
543562
]
544563
},
545564
{
@@ -548,23 +567,14 @@
548567
"metadata": {},
549568
"outputs": [],
550569
"source": [
551-
"# Set a few variables\n",
552-
"proj = ccrs.Robinson()\n",
570+
"# Create the base map\n",
571+
"fig, ax = base_map()\n",
553572
"\n",
554-
"# Create figure and axis\n",
555-
"fig, ax = plt.subplots(subplot_kw=dict(projection=proj),\n",
556-
" layout=\"constrained\")\n",
557-
"\n",
558-
"# Plot surface albedo feedback & add coastlines\n",
559-
"pl.mean(dim=\"time\").plot(ax=ax,transform=ccrs.PlateCarree(),cmap=\"Blues_r\",\n",
560-
" cbar_kwargs={\"orientation\": \"horizontal\", \"shrink\": 0.7,\n",
561-
" \"label\":\"W/m$^2$\"})\n",
562-
"ax.coastlines()\n",
573+
"# Plot surface albedo feedback\n",
574+
"pl.mean(dim=\"time\").plot(ax=ax, cmap=\"Blues_r\", **plotargs)\n",
563575
"\n",
564576
"# Add a title\n",
565-
"ax.set_title(\"Time-averaged Planck Feedback\")\n",
566-
"\n",
567-
"plt.show()"
577+
"ax.set_title(\"Time-averaged Planck Feedback\");"
568578
]
569579
},
570580
{
@@ -573,24 +583,14 @@
573583
"metadata": {},
574584
"outputs": [],
575585
"source": [
576-
"# Set a few variables\n",
577-
"proj = ccrs.Robinson()\n",
586+
"# Create the base map\n",
587+
"fig, ax = base_map()\n",
578588
"\n",
579-
"# Create figure and axis\n",
580-
"fig, ax = plt.subplots(subplot_kw=dict(projection=proj),\n",
581-
" layout=\"constrained\")\n",
582-
"\n",
583-
"# Plot surface albedo feedback & add coastlines\n",
584-
"(pl.mean(dim=\"time\")/Δts.mean(dim=\"time\")).plot(\n",
585-
" ax=ax,transform=ccrs.PlateCarree(),cmap=\"Blues_r\",\n",
586-
" cbar_kwargs={\"orientation\": \"horizontal\", \"shrink\": 0.7,\"label\":\"W/m$^2$\"}\n",
587-
")\n",
588-
"ax.coastlines()\n",
589+
"# Plot surface albedo feedback\n",
590+
"(pl.mean(dim=\"time\")/Δts.mean(dim=\"time\")).plot(ax=ax,cmap=\"Blues_r\", **plotargs_2)\n",
589591
"\n",
590592
"# Add a title\n",
591-
"ax.set_title(\"Time-averaged Planck Feedback\")\n",
592-
"\n",
593-
"plt.show()"
593+
"ax.set_title(\"Time-averaged Planck Feedback\");"
594594
]
595595
},
596596
{
@@ -670,23 +670,14 @@
670670
"metadata": {},
671671
"outputs": [],
672672
"source": [
673-
"# Set a few variables\n",
674-
"proj = ccrs.Robinson()\n",
673+
"# Create the base map\n",
674+
"fig, ax = base_map()\n",
675675
"\n",
676-
"# Create figure and axis\n",
677-
"fig, ax = plt.subplots(subplot_kw=dict(projection=proj),\n",
678-
" layout=\"constrained\")\n",
679-
"\n",
680-
"# Plot the longwave water vapor feedback and add coastlines\n",
681-
"qlw.mean(dim=\"time\").plot(ax=ax,transform=ccrs.PlateCarree(),cmap=\"Reds\",\n",
682-
" cbar_kwargs={\"orientation\": \"horizontal\", \"shrink\": 0.7,\n",
683-
" \"label\":\"W/m$^2$\"})\n",
684-
"ax.coastlines()\n",
676+
"# Plot the longwave water vapor feedback\n",
677+
"qlw.mean(dim=\"time\").plot(ax=ax,cmap=\"Reds\", **plotargs)\n",
685678
"\n",
686679
"# Add a title\n",
687-
"ax.set_title(\"Time-averaged LW Water Vapor Feedback\")\n",
688-
"\n",
689-
"plt.show()"
680+
"ax.set_title(\"Time-averaged LW Water Vapor Feedback\");"
690681
]
691682
},
692683
{
@@ -695,25 +686,15 @@
695686
"metadata": {},
696687
"outputs": [],
697688
"source": [
698-
"# Set a few variables\n",
699-
"proj = ccrs.Robinson()\n",
700-
"\n",
701-
"# Create figure and axis\n",
702-
"fig, ax = plt.subplots(subplot_kw=dict(projection=proj),\n",
703-
" layout=\"constrained\")\n",
689+
"# Create the base map\n",
690+
"fig, ax = base_map()\n",
704691
"\n",
705-
"# Plot the longwave water vapor feedback and add coastlines\n",
706-
"(qlw.mean(dim=\"time\")/Δts.mean(dim=\"time\")).plot(\n",
707-
" ax=ax,transform=ccrs.PlateCarree(),cmap=\"Reds\",\n",
708-
" cbar_kwargs={\"orientation\": \"horizontal\", \"shrink\": 0.7,\n",
709-
" \"label\":\"W/m$^2$/K\"}\n",
710-
")\n",
692+
"# Plot the longwave water vapor feedback \n",
693+
"(qlw.mean(dim=\"time\")/Δts.mean(dim=\"time\")).plot(ax=ax,cmap=\"Reds\", **plotargs_2)\n",
711694
"ax.coastlines()\n",
712695
"\n",
713696
"# Add a title\n",
714-
"ax.set_title(\"Time-averaged LW Water Vapor Feedback\")\n",
715-
"\n",
716-
"plt.show()"
697+
"ax.set_title(\"Time-averaged LW Water Vapor Feedback\");"
717698
]
718699
},
719700
{
@@ -729,23 +710,15 @@
729710
"metadata": {},
730711
"outputs": [],
731712
"source": [
732-
"# Set a few variables\n",
733-
"proj = ccrs.Robinson()\n",
734-
"\n",
735-
"# Create figure and axis\n",
736-
"fig, ax = plt.subplots(subplot_kw=dict(projection=proj),\n",
737-
" layout=\"constrained\")\n",
713+
"# Create the base map\n",
714+
"fig, ax = base_map()\n",
738715
"\n",
739716
"# Plot the longwave water vapor feedback and add coastlines\n",
740-
"qsw.mean(dim=\"time\").plot(ax=ax,transform=ccrs.PlateCarree(),cmap=\"Reds\",\n",
741-
" cbar_kwargs={\"orientation\": \"horizontal\", \"shrink\": 0.7,\n",
742-
" \"label\":\"W/m$^2$\"})\n",
743-
"ax.coastlines()\n",
717+
"qsw.mean(dim=\"time\").plot(ax=ax,cmap=\"Reds\", **plotargs)\n",
744718
"\n",
745-
"# Add a title\n",
746-
"ax.set_title(\"Time-averaged SW Water Vapor Feedback\")\n",
747719
"\n",
748-
"plt.show()"
720+
"# Add a title\n",
721+
"ax.set_title(\"Time-averaged SW Water Vapor Feedback\");"
749722
]
750723
},
751724
{
@@ -754,25 +727,14 @@
754727
"metadata": {},
755728
"outputs": [],
756729
"source": [
757-
"# Set a few variables\n",
758-
"proj = ccrs.Robinson()\n",
759-
"\n",
760-
"# Create figure and axis\n",
761-
"fig, ax = plt.subplots(subplot_kw=dict(projection=proj),\n",
762-
" layout=\"constrained\")\n",
730+
"# Create the base map\n",
731+
"fig, ax = base_map()\n",
763732
"\n",
764733
"# Plot the longwave water vapor feedback and add coastlines\n",
765-
"(qsw.mean(dim=\"time\")/Δts.mean(dim=\"time\")).plot(\n",
766-
" ax=ax,transform=ccrs.PlateCarree(),cmap=\"Reds\",\n",
767-
" cbar_kwargs={\"orientation\": \"horizontal\", \"shrink\": 0.7,\n",
768-
" \"label\":\"W/m$^2$/K\"}\n",
769-
")\n",
770-
"ax.coastlines()\n",
734+
"(qsw.mean(dim=\"time\")/Δts.mean(dim=\"time\")).plot(ax=ax,cmap=\"Reds\", **plotargs_2)\n",
771735
"\n",
772736
"# Add a title\n",
773-
"ax.set_title(\"Time-averaged SW Water Vapor Feedback\")\n",
774-
"\n",
775-
"plt.show()"
737+
"ax.set_title(\"Time-averaged SW Water Vapor Feedback\");"
776738
]
777739
},
778740
{

0 commit comments

Comments
 (0)