python - 如何使用 altair 在 hconcat 图表中显示两个不同的图例

标签 python jupyter-lab altair

我需要并排显示两个单独的图表,包括它们在 Jupyterlab 中的图例,而我设法做到这一点的唯一方法是使用 hconcat。
我已经走了这么远:

enter image description here

然而,即使是 .resolve_legend(color='independent')我从顶部的两个图例中都显示了两个图表中的条目 - 这非常令人困惑。

结果应如下所示:

enter image description here

如何删除不需要的图例条目?
或者,如果有人知道如何在单个 jupyterlab 单元中并排绘制图表的好方法,我很乐意采用不同的方法。

我的代码如下所示:

import altair as alt
import pandas as pd
from altair.expr import datum
df_test=pd.read_csv("test_df.csv")

chart_m1=alt.Chart(df_test).mark_bar().encode(
            x=alt.X('counts:Q', stack="normalize",axis=None),
            y=alt.Y('category:N',sort=['A','B','C'],title=None),
            color=alt.Color('grade:N',
                sort = alt.EncodingSortField( 'sort:Q', order = 'ascending' ),
                scale = alt.Scale(domain=['good <10', 'average 10-20', 'bad >20'], range=['#0cce6b', '#ffa400', '#ff4e42']),
                legend = alt.Legend(title="Metric1",orient='top')),
            order='sort:Q',
            tooltip=['category:N','grade:N','counts:Q']
            ).transform_filter(datum.metric=='metric1'
            ).properties(height=50,width=150)

chart_m2=alt.Chart(df_test).mark_bar().encode(
            x=alt.X('counts:Q', stack="normalize",axis=None),
            y=alt.Y('category:N',sort=['A','B','C'],title=None),
            color=alt.Color('grade:N',
                sort = alt.EncodingSortField( 'sort:Q', order = 'ascending' ),
                scale = alt.Scale(domain=['good <100', 'average 100-350', 'bad >350'], range=['#0cce6b', '#ffa400', '#ff4e42']),
                legend = alt.Legend(title="Metric2",orient='top')),
            order='sort:Q',
            tooltip=['category:N','grade:N','counts:Q']
            ).transform_filter(datum.metric=='metric2'
            ).properties(height=50,width=150)

alt.hconcat(chart_m1,chart_m2).resolve_legend(color='independent').configure_view(stroke=None)
test_df.csv我用的是这个:
category,metric,sort,grade,counts
A,metric1,1,good <10,345
B,metric1,1,good <10,123
C,metric1,1,good <10,567
A,metric1,2,average 10-20,567
B,metric1,2,average 10-20,678
C,metric1,2,average 10-20,789
A,metric1,3,bad >20,900
B,metric1,3,bad >20,1011
C,metric1,3,bad >20,1122
A,metric2,1,good <100,1122
B,metric2,1,good <100,1011
C,metric2,1,good <100,900
A,metric2,2,average 100-350,789
B,metric2,2,average 100-350,678
C,metric2,2,average 100-350,567
A,metric2,3,bad >350,567
B,metric2,3,bad >350,345
C,metric2,3,bad >350,123

最佳答案

使用 resolve_scale(color='independent')

alt.hconcat(
    chart_m1, chart_m2
).resolve_scale(
    color='independent'
).configure_view(
    stroke=None
)

enter image description here

更多信息请访问 https://altair-viz.github.io/user_guide/scale_resolve.html

关于python - 如何使用 altair 在 hconcat 图表中显示两个不同的图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60328943/

相关文章:

python - Windows下Python如何限制内存和CPU使用?

anaconda - 在 jupyterlab 中使用 plot.ly - 图形不显示

python - Altair 无法访问文件系统以在 Google Colab 上渲染大型数据集

python - 如何 pickle 一个写在函数内部的类的实例?

python - cursor.execute/cursor.callproc 不返回错误但不执行任何操作

query-performance - 如何使用 SQLAlchemy 和 PostgreSQL 对大表中的所有行进行有效排序?

pandas - 对堆积条形图进行排序

jupyter-notebook - 如何在谷歌计算引擎(深度学习 VM)的 conda 环境中运行 jupyter 实验室?

jupyter-notebook - 通过 JupyterLab 的链接在当前工作区中打开笔记本

python - Altair 的累积计数