许可优化
许可优化
产品
产品
解决方案
解决方案
服务支持
服务支持
关于
关于
软件库
当前位置:服务支持 >  软件文章 >  Altair案例:在concat图表中显示不同图例的方法

Altair案例:在concat图表中显示不同图例的方法

阅读数 3
点赞 0
article_banner

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

import altair as altimport pandas as pdfrom altair.expr import datumdf_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)

但是图例同一颜色的图例重复了,造成混淆。使用.resolve_legend(color='independent')没有用。

目标是每个图只显示跟它相关的legend,如下:

解决 方法:.resolve_scale(color='independent')

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

来源:python - How to display two different legends in hconcat chart using altair - Stack Overflowhttps://stackoverflow.com/questions/60328943/how-to-display-two-different-legends-in-hconcat-chart-using-altair


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删


相关文章
QR Code
微信扫一扫,欢迎咨询~
customer

online

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 board-phone 155-2731-8020
close1
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

姓名不为空

姓名不为空
手机不正确

手机不正确

手机不正确
公司不为空

公司不为空

公司不为空