Altair:用连续比例着色的散点图的回归
Altair: Regression over a scatter plot coloured with a continuous scale
我正在尝试使用 Altair 显示散点图,其中标记颜色由非分类特征(连续)给出,并在其上添加回归线。不过,我对 Altair 还很陌生。
第一,here's a sample of the data I'm inputting into Altair.
So far I can create either the colouring without a regression:
问题是回归线没有显示出来。这是通过以下代码实现的:
chart = alt.Chart(df).mark_circle(size=60).encode(
x='Lambda',
y='ACR',
color='Consensus',
tooltip=["Tolerance Prevalence", "Consensus", "ACR", "Lambda"]
)
chart.interactive() + chart.transform_regression(
'Lambda', 'ACR', method="quad"
).mark_line(color="red")
Or a regression without the desired colour scale.
我可以通过简单地删除第一条指令中的“color='consensus'”行来做到这一点。
我试过改变回归方法,甚至不同的特征组合都无济于事。
我可以使用任何参数或 Altair 函数轻松解决此问题吗?
提前致谢!
编辑 1
完整代码:
import altair as alt
import pandas as pd
import numpy as np
df = read_csv("processed.txt")
chart = alt.Chart(df).mark_circle(size=60).encode(
x='Lambda',
y='ACR',
color='Consensus',
tooltip=["Tolerance Prevalence", "Consensus", "ACR", "Lambda"]
)
chart.interactive() + chart.transform_regression(
'Lambda', 'ACR', method="quad").mark_line(color="red")
chart
您可以定义一个没有着色的基本图表,然后从中构建散点图和回归线:
import altair as alt
import pandas as pd
import numpy as np
df = pd.read_csv("Downloads/processed.txt", sep=' ')
base = alt.Chart(df).mark_circle(size=60).encode(
x='Lambda',
y='ACR',
)
scatter = base.encode(
color='Consensus',
tooltip=["Tolerance Prevalence", "Consensus", "ACR", "Lambda"]
)
line = base.mark_line(color="red").transform_regression(
'Lambda', 'ACR', method="quad")
scatter + line
我正在尝试使用 Altair 显示散点图,其中标记颜色由非分类特征(连续)给出,并在其上添加回归线。不过,我对 Altair 还很陌生。
第一,here's a sample of the data I'm inputting into Altair.
So far I can create either the colouring without a regression:
问题是回归线没有显示出来。这是通过以下代码实现的:
chart = alt.Chart(df).mark_circle(size=60).encode(
x='Lambda',
y='ACR',
color='Consensus',
tooltip=["Tolerance Prevalence", "Consensus", "ACR", "Lambda"]
)
chart.interactive() + chart.transform_regression(
'Lambda', 'ACR', method="quad"
).mark_line(color="red")
Or a regression without the desired colour scale. 我可以通过简单地删除第一条指令中的“color='consensus'”行来做到这一点。
我试过改变回归方法,甚至不同的特征组合都无济于事。 我可以使用任何参数或 Altair 函数轻松解决此问题吗?
提前致谢!
编辑 1
完整代码:
import altair as alt
import pandas as pd
import numpy as np
df = read_csv("processed.txt")
chart = alt.Chart(df).mark_circle(size=60).encode(
x='Lambda',
y='ACR',
color='Consensus',
tooltip=["Tolerance Prevalence", "Consensus", "ACR", "Lambda"]
)
chart.interactive() + chart.transform_regression(
'Lambda', 'ACR', method="quad").mark_line(color="red")
chart
您可以定义一个没有着色的基本图表,然后从中构建散点图和回归线:
import altair as alt
import pandas as pd
import numpy as np
df = pd.read_csv("Downloads/processed.txt", sep=' ')
base = alt.Chart(df).mark_circle(size=60).encode(
x='Lambda',
y='ACR',
)
scatter = base.encode(
color='Consensus',
tooltip=["Tolerance Prevalence", "Consensus", "ACR", "Lambda"]
)
line = base.mark_line(color="red").transform_regression(
'Lambda', 'ACR', method="quad")
scatter + line