I am trying to create a heatmap where the color scale is discrete, not continuous. I would like to use 5 colors, so I don't think a conditional statement will work. I found this example on Google Groups, but it doesn't seem to be working completely. How can I fix it to get the actual heatmap to appear as expected?
import altair as alt
import pandas as pd
import numpy as np
np.random.seed(0)
df = pd.DataFrame({
'x': np.random.randint(0, 5, 100),
'y': np.random.randint(0, 5, 100),
'z': np.random.rand(100)
})
alt.Chart(df).mark_rect().encode(
x='x:O',
y='y:O',
color=alt.Color('mean(z)', bin=alt.Bin(maxbins=5))
)
Expected Result:
Actual Result:
Using a transform_aggregate
function might help.
alt.Chart(df).mark_rect().encode(
x='x:O',
y='y:O',
color=alt.Color('mean_z', bin=alt.Bin(maxbins=5)),
).transform_aggregate(
mean_z='mean(z)',
groupby=["x","y"]
)