如何在Altair中向多面或类似双轴图表添加特定于系列的线y = x? 问题:图表不是双轴的并且不包括line_x_equals_y问题:JavaScript错误问题:JavaScript错误获取数据创建Y = X行数据合并线性数据构建图表手动分面图合并图表

如何解决如何在Altair中向多面或类似双轴图表添加特定于系列的线y = x? 问题:图表不是双轴的并且不包括line_x_equals_y问题:JavaScript错误问题:JavaScript错误获取数据创建Y = X行数据合并线性数据构建图表手动分面图合并图表

关于如何制作双轴图表,然后使用Altair在y=x的每个图表上添加一条线的任何建议?挑战在于,y=x行必须与特定于每个多面图表中显示的数据的系列比例相匹配。

链接:

  1. altair github issue thread on facets
  2. altair github issue thread on axis display

下面是重现该问题的代码。

import altair as alt
from vega_datasets import data

source = data.anscombe().copy()
source['line-label'] = 'x=y'
source = pd.concat([source,source.groupby('Series').agg(x_diff=('X','diff'),y_diff=('Y','diff'))],axis=1)
source['rate'] = source.y_diff/source.x_diff
source['rate-label'] = 'rate-of-change'


base = alt.Chart().encode(
    x='X:O',)

scatter = base.mark_circle(size=60,opacity=0.30).encode(
    y='Y:Q',color=alt.Color('Series:O',scale=alt.Scale(scheme='category10')),tooltip=['Series','X','Y']
)

line_x_equals_y = alt.Chart().mark_line(color= 'black',strokeDash=[3,8]).encode(
    x=alt.X('max(X)',axis=None),y=alt.Y('max(X)',# note: it's intentional to set max(X) here so that X and Y are equal.
    color = alt.Color('line-label') # note: the intent here is for the line label to show up in the legend
    )

rate = base.mark_line(strokeDash=[5,3]).encode(
    y=alt.Y('rate:Q'),color = alt.Color('rate-label',),tooltip=['rate','Y']
)

scatter_rate = alt.layer(scatter,rate,data=source)

尝试的解决方案

问题:图表不是双轴的(并且不包括line_x_equals_y

scatter_rate.facet('Series',columns=2).resolve_axis(
        x='independent',y='independent',)

enter image description here

问题:JavaScript错误

alt.layer(scatter_rate,line_x_equals_y,data=source).facet('Series',)

问题:JavaScript错误

chart_generator =  (alt.layer(line_x_equals_y,scatter_rate,data = source,title=f"Series {val}").transform_filter(alt.datum.Series == val).resolve_scale(y='independent',x='independent') \
             for val in source.Series.unique()) 

alt.concat(*(
    chart_generator
),columns=2)

目标

  1. scatter_rate是一个多面(按系列)双轴图表,带有适合值范围的单独刻度。
  2. 每个多面图表均包含一行y=x,其范围从单个图表的(0,0)到y=max(X)值。

解决方法

您可以通过正常创建图层并在图层表上调用facet()方法来做到这一点。唯一的要求是所有层共享相同的源数据。在当前版本的Altair中,无需手动构造构面,也不需要后期的数据绑定:

import altair as alt
from vega_datasets import data
import pandas as pd

source = data.anscombe().copy()
source['line-label'] = 'x=y'
source = pd.concat([source,source.groupby('Series').agg(x_diff=('X','diff'),y_diff=('Y','diff'))],axis=1)
source['rate'] = source.y_diff/source.x_diff
source['rate-label'] = 'line y=x'

source_linear = source.groupby(by=['Series']).agg(x_linear=('X','max'),y_linear=('X','max')).reset_index().sort_values(by=['Series'])

source_origin = source_linear.copy()
source_origin['y_linear'] = 0
source_origin['x_linear'] = 0

source_linear = pd.concat([source_origin,source_linear]).sort_values(by=['Series'])

source = source.merge(source_linear,on='Series').drop_duplicates()

scatter = alt.Chart(source).mark_circle(size=60,opacity=0.60).encode(
    x='X:Q',y='Y:Q',color='Series:N',tooltip=['X','Y','rate']
)

rate = alt.Chart(source).mark_line(strokeDash=[5,3]).encode(
    x='X:Q',y='rate:Q',color = 'rate-label:N'
)

line_plot = alt.Chart(source).mark_line(color= 'black',strokeDash=[3,8]).encode(
    x=alt.X('x_linear',title = ''),y=alt.Y('y_linear',shape = alt.Shape('rate-label',title = 'Break Even'),color = alt.value('black')
)

alt.layer(scatter,rate,line_plot).facet(
    'Series:N'
).properties(
    columns=2
).resolve_scale(
    x='independent',y='independent'
)

enter image description here

,

此解决方案在y=x处根据每个图表上的数据构建所需的线;但是,点在合并步骤中是重复的,我不确定如何添加双轴速率。

获取数据

source = data.anscombe().copy()
source['line-label'] = 'x=y'
source = pd.concat([source,axis=1)
source['rate'] = source.y_diff/source.x_diff
source['rate-label'] = 'line y=x'

创建Y = X行数据

source_linear = source.groupby(by=['Series']).agg(x_linear=('X',source_linear]).sort_values(by=['Series'])

合并线性数据

source = source.merge(source_linear,on='Series').drop_duplicates()

构建图表

scatter = alt.Chart().mark_circle(size=60,opacity=0.60).encode(
    x=alt.X('X',title='X'),y=alt.Y('Y',title='Y'),#color='year:N','rate']
)

line_plot = alt.Chart().mark_line(color= 'black',color = alt.value('black')
)

手动分面图

chart_generator =  (alt.layer(scatter,line_plot,data = source,title=f"{val}: Duplicated Points w/ Line at Y=X").transform_filter(alt.datum.Series == val) \
             for val in source.Series.unique())

合并图表

chart = alt.concat(*(
    chart_generator
),columns=3)

chart.display()

enter image description here

,

此解决方案包括费率,但不是双轴w / Y和另一轴rate

import altair as alt
from vega_datasets import data
import pandas as pd

source = data.anscombe().copy()
source['line-label'] = 'x=y'
source = pd.concat([source,axis=1)
source['rate'] = source.y_diff/source.x_diff
source['rate-label'] = 'rate of change'
source['line-label'] = 'line y=x'

source_linear = source.groupby(by=['Series']).agg(x_linear=('X','rate']
)

line_plot = alt.Chart(source).mark_line(color= 'black',shape = alt.Shape('line-label',color = alt.value('black')
)

rate =  alt.Chart(source).mark_line(strokeDash=[5,3]).encode(
    x=alt.X('X',axis=None,title = 'X'),y=alt.Y('rate:Q'),color = alt.Color('rate-label',),tooltip=['rate','X','Y']
)

alt.layer(scatter,rate).facet(
    'Series:N'
).properties(
    columns=2
).resolve_scale(
    x='independent',y='independent'
).display()

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-