Skip to content

Climate Vulnerability and CO2

import pandas as pd
import plotnine as gg

from ninejs import interactive, save, css, show


data_url = "https://raw.githubusercontent.com/holtzy/the-python-graph-gallery/master/static/data/data-CO2.csv"
data = pd.read_csv(data_url).rename(
    columns={
        " ": "vulnerability",
        " .1": "readiness",
        "Name": "country",
        "CO2 per Capita": "co2_per_capita",
        "Color": "color",
    }
)

data["tooltip"] = [
    f"""
    <div class="tooltip-card">
        <div class="tooltip-header">
            <span class="tooltip-country">{country}</span>
        </div>

        <div class="tooltip-grid">
            <div class="tooltip-label">Vulnerability</div>
            <div class="tooltip-value">{vulnerability:.2f}</div>

            <div class="tooltip-label">Readiness</div>
            <div class="tooltip-value">{readiness:.2f}</div>

            <div class="tooltip-label">CO₂ / capita</div>
            <div class="tooltip-value">{co2:.1f} t</div>
        </div>
    </div>
    """
    for country, vulnerability, readiness, co2 in zip(
        data["country"],
        data["vulnerability"],
        data["readiness"],
        data["co2_per_capita"],
    )
]

text_color = "#252525"

plot = (
    gg.ggplot(data, gg.aes(x="vulnerability", y="readiness", tooltip="tooltip"))
    + gg.geom_point(
        gg.aes(size="co2_per_capita", color="color"),
        shape="s",
        alpha=0.4,
        show_legend=False,
    )
    + gg.scale_color_identity()
    + gg.scale_size_area(max_size=17)
    + gg.scale_x_continuous(limits=(0.22, 0.70), expand=(0, 0))
    + gg.scale_y_continuous(limits=(0.04, 1.02), expand=(0, 0))
    + gg.coord_cartesian(expand=False)
    + gg.theme_void(base_size=8)
    + gg.theme(figure_size=(6, 6), plot_margin=0.03)
)

fig = plot.draw()
ax = fig.axes[0]

ax.vlines(0.43,0.12,0.84,color="#a8a8a8",linestyle="--",linewidth=1.0,alpha=0.65,zorder=1)
ax.hlines(0.41,0.24,0.68,color="#a8a8a8",linestyle="--",linewidth=1.0,alpha=0.65,zorder=1)

ax.text(
    0.245,
    0.98,
    "The countries with the highest vulnerability to climate change\nhave the lowest CO2 emissions",
    fontsize=10,
    fontweight="bold",
    ha="left",
    va="top",
    color=text_color,
)
ax.text(
    0.245,
    0.91,
    "All countries sorted by their vulnerability and readiness to climate change. The size\nshows the CO2 emission per person in that country.",
    fontsize=8,
    ha="left",
    va="top",
    color=text_color,
)
ax.text(
    0.245,
    0.065,
    (
        "Data: Notre Dame Global Adaptation Initiative and Global Carbon Project "
        "| Original: Datawrapper"
    ),
    fontsize=7.3,
    ha="left",
    va="bottom",
    color=text_color,
)

fig.text(0.11, 0.45, "High readiness", color="silver", size=6)
fig.text(0.11, 0.31, "Low readiness", color="silver", size=6)

fig.text(0.41, 0.13, "Low vulnerability", color="silver", size=6, ha="right")
fig.text(0.47, 0.13, "High vulnerability", color="silver", size=6, ha="left")

arrowprops = dict(arrowstyle="->", color="silver", lw=0.4)

ax.annotate("", xy=(0.25, 0.32), xytext=(0.25, 0.37), arrowprops=arrowprops)
ax.annotate("", xy=(0.25, 0.5), xytext=(0.25, 0.45), arrowprops=arrowprops)

ax.annotate("", xy=(0.38, 0.12), xytext=(0.41, 0.12), arrowprops=arrowprops)
ax.annotate("", xy=(0.48, 0.12), xytext=(0.45, 0.12), arrowprops=arrowprops)

(
    interactive(plot, hover_nearest=True)
    + css(from_file="docs/examples/climate-vulnerability-co2.css")
    + save("docs/iframes/climate-vulnerability-co2.html")
)
.hovered {
    stroke: #111;
    stroke-width: 1.5px;
    fill-opacity: 1;
    stroke-opacity: 1;
    transition: all 120ms ease;
}

.tooltip {
    background: rgba(20, 20, 24, 0.96);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 14px;
    box-shadow: 0 12px 35px rgba(0, 0, 0, 0.35);
    padding: 0;
    overflow: hidden;
    color: white;
    font-family: Inter;
}

.tooltip-card {
    padding: 14px 16px;
    min-width: 220px;
}

.tooltip-header {
    margin-bottom: 10px;
    padding-bottom: 8px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.tooltip-country {
    font-size: 15px;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.tooltip-grid {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 6px 18px;
    align-items: center;
}

.tooltip-label {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.65);
}

.tooltip-value {
    font-size: 12px;
    font-weight: 600;
    color: white;
    text-align: right;
}