Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Altair(Python)中标记分层图表_Python_Altair - Fatal编程技术网

在Altair(Python)中标记分层图表

在Altair(Python)中标记分层图表,python,altair,Python,Altair,我试图在Altair中创建两层柱状图,并为每个柱状图创建一个垂直平均标尺。我想要一个图例来标记这四个 我正在使用可以找到的第一个“出生体重I”数据 我的代码很长,道歉看起来像这样: from altair import datum # This histogram for baby weights of mothers who dont smoke dont = alt.Chart(babyData).mark_bar().encode( alt.X("bwt-oz:Q", axis=

我试图在Altair中创建两层柱状图,并为每个柱状图创建一个垂直平均标尺。我想要一个图例来标记这四个

我正在使用可以找到的第一个“出生体重I”数据

我的代码很长,道歉看起来像这样:

from altair import datum

# This histogram for baby weights of mothers who dont smoke
dont = alt.Chart(babyData).mark_bar().encode(
    alt.X("bwt-oz:Q", axis=alt.Axis(title='Birth Weight (Ounces)'), bin=True),
    alt.Y('count()', axis=alt.Axis(title='Count'), scale=alt.Scale(domain=[0, 350]))
).properties(
    width=400,
    height=400
).transform_filter(
    datum.smoke == 0,
)

mean = alt.Chart(babyData).mark_rule(color='red').encode(
    x='mean(bwt-oz):Q',
    size=alt.value(4)
).transform_filter(
    datum.smoke == 0
)

dontSmokeChart = dont + mean

# This histogram for baby weights of mothers who smoke
do = alt.Chart(babyData).mark_bar().encode(
    alt.X("bwt-oz:Q", axis=alt.Axis(title='Birth Weight (Ounces)'), bin=True),
    alt.Y('count()', axis=alt.Axis(title='Count'), scale=alt.Scale(domain=[0, 350]))
).transform_filter(
    datum.smoke == 1
).properties(
    width=400,
    height=400
)

mean2 = alt.Chart(babyData).mark_rule(color='red').encode(
    x='mean(bwt-oz):Q',
    size=alt.value(4)
).transform_filter(
    datum.smoke == 1
)

doSmokeChart = do + mean2

# This layers, and puts them all together

layer = alt.layer(
    dont,
    mean,
    do,
    mean2
).properties(
    title="Layered Histogram of Baby Weights of Mothers Who smoke Vs. Who Don't",
).configure_mark(
    opacity=0.5,
    color='blue',
)
layer
最终的分层图表如下所示:

我只想要一个图例,说明哪个直方图/平均值属于哪个

如果我也能给它们涂上颜色,或许可以用这种方式添加一个传奇,那也不错,但我不知道怎么做


谢谢你的洞察力

您不应使用过滤数据手动创建图层,而应在完整数据集上使用颜色编码:然后将自动生成图例

例如:

import altair as alt
import pandas as pd

babyData = pd.read_csv('https://www.stat.berkeley.edu/users/statlabs/data/babiesI.data', delim_whitespace=True)

base = alt.Chart(babyData).transform_filter(
    'datum.smoke != 9'
)

hist = base.mark_bar(opacity=0.5).encode(
    alt.X("bwt:Q",title='Birth Weight (Ounces)', bin=True),
    alt.Y('count()', title='Count'),
    color='smoke:N'
).properties(
    width=400,
    height=400
)

mean = base.mark_rule().encode(
    x='mean(bwt):Q',
    size=alt.value(4),
    color='smoke:N'
)

hist + mean

从那里,您可以使用标准方法来为每个标记创建图层。

而不是使用过滤数据手动创建图层,您应该在完整的数据集上使用颜色编码:然后将自动生成图例

例如:

import altair as alt
import pandas as pd

babyData = pd.read_csv('https://www.stat.berkeley.edu/users/statlabs/data/babiesI.data', delim_whitespace=True)

base = alt.Chart(babyData).transform_filter(
    'datum.smoke != 9'
)

hist = base.mark_bar(opacity=0.5).encode(
    alt.X("bwt:Q",title='Birth Weight (Ounces)', bin=True),
    alt.Y('count()', title='Count'),
    color='smoke:N'
).properties(
    width=400,
    height=400
)

mean = base.mark_rule().encode(
    x='mean(bwt):Q',
    size=alt.value(4),
    color='smoke:N'
)

hist + mean

从那里你可以使用标准的方法来处理每个标记。

@jakevdp刚刚击败了我!我也要说同样的话。这里有一个完整的示例供您使用

作为pd进口熊猫 将牵牛星导入为alt 链接到数据源 URL='1〕https://www.stat.berkeley.edu/users/statlabs/data/babiesI.data' 将数据读入数据帧 df=pd.read_tableURL,sep='\s+' hist=alt.Chartdf.mark\U区域 不透明度=0.7, 内插class='step' 编码 alt.Xbwt:Q,axis=alt.Axistitle='出生体重盎司',bin=True, alt.Y'count',axis=alt.Axistitle='count',stack=None, alt.Color“烟雾:N” .物业 宽度=400, 高度=400 .transform_filterat.datum.smoke!=9 rule=alt.Chartdf.mark\u rulecolor='red'。编码 备选详图“烟雾:N”, alt.Color“烟雾:N”, alt.X‘平均值:Q’, 大小=alt.value4, .transform_filterat.datum.smoke!=9 历史+规则
@杰克,VDP刚刚打败了我!我也要说同样的话。这里有一个完整的示例供您使用

作为pd进口熊猫 将牵牛星导入为alt 链接到数据源 URL='1〕https://www.stat.berkeley.edu/users/statlabs/data/babiesI.data' 将数据读入数据帧 df=pd.read_tableURL,sep='\s+' hist=alt.Chartdf.mark\U区域 不透明度=0.7, 内插class='step' 编码 alt.Xbwt:Q,axis=alt.Axistitle='出生体重盎司',bin=True, alt.Y'count',axis=alt.Axistitle='count',stack=None, alt.Color“烟雾:N” .物业 宽度=400, 高度=400 .transform_filterat.datum.smoke!=9 rule=alt.Chartdf.mark\u rulecolor='red'。编码 备选详图“烟雾:N”, alt.Color“烟雾:N”, alt.X‘平均值:Q’, 大小=alt.value4, .transform_filterat.datum.smoke!=9 历史+规则