import altair as alt
from altair_recipes.display_pweave import show
import pandas as pd
import numpy as np
Sample data (must sum to 100):
energy = pd.DataFrame(
{
"source": ["natural gas", "nuclear", "coal", "hydro"],
"percentage": [50, 30, 10, 10],
"renewable": [False, False, False, True],
}
)
Define viz:
def mybarchart(data, source):
return (
alt.Chart(data=data)
.mark_bar()
.encode(x=source, y="percentage", color="renewable")
)
Charts related by color:
show(mybarchart(energy, source="source"))
.
show(
mybarchart(
energy.groupby(by="renewable").agg({"percentage": "sum", "renewable": "first"}),
source="renewable",
)
)