【问题标题】:Labelling Layered Charts in Altair (Python)在 Altair (Python) 中标记分层图表
【发布时间】:2020-05-12 06:56:02
【问题描述】:

我正在尝试在 Altair 中创建两个分层直方图(每个都有一个垂直平均标尺)。我想要一个图例来标记这四个中的每一个。

我正在使用 here 中的第一个“出生体重 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

最终的分层图表如下所示:

我只想要一个说明哪个直方图/平均值属于什么的图例。

如果我也可以给它们上色,或者以这种方式添加一个图例,那也很好,但我不确定该怎么做。

感谢您的任何见解!

【问题讨论】:

    标签: python altair


    【解决方案1】:

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

    例如:

    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
    

    从那里您可以使用标准方法来处理每个标记所使用的Customize the color schemes

    【讨论】:

      【解决方案2】:

      @jakevdp 刚刚打败了我!我想说同样的话。这是一个完整的示例供您使用。

      import pandas as pd
      import altair as alt
      
      # Link to data source
      URL =  'https://www.stat.berkeley.edu/users/statlabs/data/babiesI.data'
      # Read data into a pandas dataframe
      df = pd.read_table(URL, sep='\s+')
      
      hist = alt.Chart(df).mark_area(
          opacity=0.7,
          interpolate='step'
      ).encode(
          alt.X("bwt:Q", axis=alt.Axis(title='Birth Weight (Ounces)'), bin=True),
          alt.Y('count()', axis=alt.Axis(title='Count'), stack=None), 
          alt.Color('smoke:N')
      ).properties(
          width=400,
          height=400
      ).transform_filter(alt.datum.smoke != 9)
      
      rule = alt.Chart(df).mark_rule(color='red').encode(
          alt.Detail('smoke:N'),
          alt.Color('smoke:N'),
          alt.X('mean(bwt):Q'),
          size=alt.value(4), 
      ).transform_filter(alt.datum.smoke != 9)
      
      hist + rule
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-18
        • 2021-04-20
        • 2020-07-23
        • 2017-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多