Altair: Can't get independent 'y' scales with faceted heatmap

蓝咒 提交于 2021-01-05 06:15:17

问题


I'm working in a jupyter notebook trying to display a sort of adjacency-matrix-like data set in a grouped layout like this one using Altair's Chart.facet() method. This guy's answer gets me halfway there, but when I try to facet with rows and resolve the y scale as 'independent', the chart comes up as a tiny box containing no elements.

df = (pd.util.testing.makeDataFrame()
      .reset_index(drop=True)  # drop string index
      .reset_index()  # add an index column
      .melt(id_vars=['index'], var_name="column")
     )

# To include all the indices and not create NaNs, I add -1 and max(indices) + 1 to the desired bins.
hbins= [-1, 3, 9, 15, 27, 30]
df['hbins'] = pd.cut(df['index'], hbins, labels=range(len(hbins) - 1))
# This was done for the index, but a similar approach could be taken for the columns as well.

df['vbins'] = df.column.replace({'A':'Grp1', 'B':'Grp1', 'C':'Grp2', 'D':'Grp2'})

alt.Chart(df).mark_rect().encode(
    x=alt.X('index:O', title=None),
    y=alt.Y('column:O', title=None),
    color="value:Q",
    column=alt.Column("hbins:O", 
                      title=None, 
                      header=alt.Header(labelFontSize=0)
                     ),
    row=alt.Row("vbins:O", 
                      title=None, 
                      header=alt.Header(labelFontSize=0)
                     )
).resolve_scale(
    x="independent",
    y="independent"
).configure_facet(
    spacing=5
)

What might be causing this?

来源:https://stackoverflow.com/questions/58122427/altair-cant-get-independent-y-scales-with-faceted-heatmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!