GISS Global/Hemispheric Temperatures

Set Up

Imports

Python

from functools import partial
from pathlib import Path
import os

PyPi

from bokeh.layouts import column
from bokeh.palettes import Set1
from bokeh.models import (
    BoxZoomTool,
    HoverTool,
    Legend,
    PanTool,
    ResetTool,
    SaveTool,
    Span,
    WheelZoomTool,
)
from bokeh.models.widgets import Panel, Tabs
from bokeh.plotting import (figure, 
                            ColumnDataSource,
)
from dotenv import load_dotenv
import holoviews
import pandas

This Project

from bartleby_the_penguin.tangles.embed_bokeh import EmbedBokeh

Setup the Embed

files_path = Path("../../files/posts/giss/giss-globalhemispheric-temperatures/")
Embed = partial(
    EmbedBokeh,
    folder_path=files_path)

Set Up Bokeh

I originally createde EmbedBokeh to use HoloViews to do the rendering so you have to set bokeh to be the backend or it will try to use matplotlib instead.

holoviews.extension("bokeh")

Load the Data

load_dotenv(".env")
path = Path(os.environ.get("ZONES")).expanduser()
assert path.is_file()
with path.open() as reader:
    giss = pandas.read_csv(path)
giss.loc[:, "Year"] = giss.Year.astype("int32")
print(giss.describe())
              Year        Glob        NHem        SHem     24N-90N  \
count   139.000000  139.000000  139.000000  139.000000  139.000000   
mean   1949.000000    0.032302    0.056043    0.008561    0.077698   
std      40.269923    0.336896    0.393435    0.301848    0.464606   
min    1880.000000   -0.490000   -0.540000   -0.490000   -0.580000   
25%    1914.500000   -0.200000   -0.220000   -0.235000   -0.280000   
50%    1949.000000   -0.070000   -0.010000   -0.080000    0.020000   
75%    1983.500000    0.215000    0.210000    0.265000    0.255000   
max    2018.000000    0.980000    1.260000    0.710000    1.500000   

          24S-24N     90S-24S     64N-90N     44N-64N     24N-44N     EQU-24N  \
count  139.000000  139.000000  139.000000  139.000000  139.000000  139.000000   
mean     0.036115   -0.018561    0.111079    0.117770    0.027698    0.027626   
std      0.331384    0.295695    0.917715    0.516729    0.356416    0.326111   
min     -0.650000   -0.470000   -1.640000   -0.710000   -0.590000   -0.720000   
25%     -0.215000   -0.250000   -0.545000   -0.270000   -0.200000   -0.230000   
50%     -0.030000   -0.100000    0.020000    0.000000   -0.070000    0.000000   
75%      0.255000    0.230000    0.660000    0.360000    0.135000    0.240000   
max      0.970000    0.700000    3.050000    1.440000    1.060000    0.930000   

          24S-EQU     44S-24S     64S-44S     90S-64S  
count  139.000000  139.000000  139.000000  139.000000  
mean     0.045683    0.020432   -0.069353   -0.078129  
std      0.343385    0.312688    0.269380    0.732359  
min     -0.580000   -0.430000   -0.540000   -2.570000  
25%     -0.210000   -0.220000   -0.265000   -0.490000  
50%     -0.030000   -0.080000   -0.090000    0.050000  
75%      0.290000    0.260000    0.180000    0.410000  
max      1.020000    0.780000    0.450000    1.270000  
print(giss.iloc[0])
print()
print(giss.iloc[-1])
Year       1880.00
Glob         -0.18
NHem         -0.31
SHem         -0.06
24N-90N      -0.38
24S-24N      -0.17
90S-24S      -0.01
64N-90N      -0.97
44N-64N      -0.47
24N-44N      -0.25
EQU-24N      -0.21
24S-EQU      -0.13
44S-24S      -0.04
64S-44S       0.05
90S-64S       0.67
Name: 0, dtype: float64

Year       2018.00
Glob          0.82
NHem          0.99
SHem          0.66
24N-90N       1.19
24S-24N       0.64
90S-24S       0.70
64N-90N       1.87
44N-64N       1.09
24N-44N       1.03
EQU-24N       0.69
24S-EQU       0.59
44S-24S       0.78
64S-44S       0.37
90S-64S       1.07
Name: 138, dtype: float64
print(giss.columns)
giss = giss.rename(columns=dict(
    Glob="Global", 
    NHem="Northern Hemisphere", 
    SHem="Southern Hemisphere"))
print(giss.columns)
Index(['Year', 'Glob', 'NHem', 'SHem', '24N-90N', '24S-24N', '90S-24S',
       '64N-90N', '44N-64N', '24N-44N', 'EQU-24N', '24S-EQU', '44S-24S',
       '64S-44S', '90S-64S'],
      dtype='object')
Index(['Year', 'Global', 'Northern Hemisphere', 'Southern Hemisphere',
       '24N-90N', '24S-24N', '90S-24S', '64N-90N', '44N-64N', '24N-44N',
       'EQU-24N', '24S-EQU', '44S-24S', '64S-44S', '90S-64S'],
      dtype='object')

Plot

Global/Hemispheric

class Plot:
    width = 1000
    height = 800
    line_width = 4
    alpha = 0.8
    light_alpha = 0.2
    title_font_size = "14pt"
hover = HoverTool(
    tooltips = [
        ("Year", "@year"),
        ("Difference From Normal", "@anomaly")
    ]
)

tools = [
    hover,
    PanTool(),
    WheelZoomTool(),
    BoxZoomTool(),
    ResetTool(),
    SaveTool(),
]

plot = figure(plot_width=Plot.width, plot_height=Plot.height, 
              x_range=(giss.Year.min(), giss.Year.max()),
              x_axis_label="Year",
              y_axis_label="Difference (Celsius)",
              tools=tools)

plot.title.text = "Yearly Temperature Difference from Mean 1931-1980 Temperature by Hemisphere"
plot.title.text_font_size = Plot.title_font_size

horizontal = Span(location=0, dimension="width", line_color="darkgray",
                  line_width=Plot.line_width, 
                  line_cap="round",
                  line_dash="dashed")
plot.renderers.extend([horizontal])
locations = ["Global", "Northern Hemisphere", "Southern Hemisphere"]
for location, color in zip(locations, Set1[3]):
    columns = ColumnDataSource(
        data=dict(
            year=giss.Year,
            anomaly=giss[location],
            smoothed=giss.rolling(5, on="Year", min_periods=1)[location].mean(),
        )
    )
    line = plot.circle("year", "anomaly", source=columns, 
                       color=color, 
                       line_width=Plot.line_width, 
                       alpha=Plot.light_alpha,
                       legend=location)
    line = plot.line("year", "smoothed", source=columns,
                     color=color,
                     line_width=Plot.line_width, alpha=Plot.alpha,
                     legend="{} Rolling 5 Year Mean".format(location))
plot.legend.click_policy = "hide"
plot.legend.location = "top_left"

Embed the Plot

I need to fix the EmbedBokeh class.

embed = Embed(plot, "global_temperature_anomalies")
embed._figure = plot
embed()