【问题标题】:Adding footnotes to layered chart in Altair在 Altair 中为分层图表添加脚注
【发布时间】:2022-08-18 18:06:16
【问题描述】:

我正在使用劳工统计局的数据制作分层图表,并且由于我要发布图表,因此我需要引用数据源。我需要在图表底部添加一条线,说明“来源:劳工统计局。截至 2022 年 7 月的数据。”我可以添加标题和副标题,但似乎没有脚注/源代码行的选项。有什么解决方法吗?

import pandas as pd
import pandas_datareader.data as pdr
import datetime
import altair as alt

start = datetime.datetime (2020, 1, 1)
end = datetime.datetime (2022, 7, 10)

df = pdr.DataReader(\'UNRATE\', \'fred\', start, end)
df = df.rename(columns={\'UNRATE\':\'Unemployment Rate\'})
df[\"Date\"] = df.index
df[\'Prepandemic Rate\'] = 3.5

source = df

line = (
    alt.Chart(source)
    .mark_line(point=False, strokeWidth=2, color=\'blue\')
    .encode(x=\"Date\", y=\"Unemployment Rate\")
)

line2 = (
    alt.Chart(source)
    .mark_line(point=False, strokeWidth=2, color=\'red\')
    .encode(x=\"Date\", y=\"Prepandemic Rate\")
)

alt.layer(line, line2).properties(
    width=300, height=300, title={
    \"text\":\'Unemployment Rate\',
    \"subtitle\":[\'Seasonally adjusted\']
    },
).configure_title(
  anchor= \'start\'
)

注意:我看到了这个问题 (How to add a Text Footer to an Altair graph?),但我似乎无法让 concat 函数在我的分层图表上工作。

    标签: altair footnotes


    【解决方案1】:

    您可以在任何您喜欢的地方添加任何文本。例如。

    import altair as alt
    import pandas as pd
    
    source = pd.DataFrame({
        'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
        'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
    })
    
    data = alt.Data(values=[{'x': 'A'}])
    
    text1 = (
        alt.Chart(data)
        .mark_text(text='Footnote', x='width', y='height', dx = 10, dy=40)
      
    )
    
    bar1 = alt.Chart(source).mark_bar().encode(
        x='a',
        y='b',
        
    )
    alt.layer(text1, bar1)
    

    【讨论】:

    • 谢谢,但是如何将脚注文本添加到分面和分层的图表中?例如:stackoverflow.com/questions/43797379/…
    • 您可以添加为 TitleParams 的页脚 - 在下面发布了答案,它不适合评论
    【解决方案2】:

    多面图表的页脚,您可以将其作为 TitleParams 添加到最终图表中。您仍然需要使用字体大小并根据自己的喜好平衡图表)

    df = pd.DataFrame([['Action', 5, 'F'], 
                       ['Crime', 10, 'F'], 
                       ['Action', 3, 'M'], 
                       ['Crime', 9, 'M']], 
                      columns=['Genre', 'Rating', 'Gender'])
    
    chart = alt.Chart(df).mark_bar().encode(
       column=alt.Column(
           'Genre', 
           header=alt.Header(orient='bottom')
        ),
       x=alt.X('Gender', axis=alt.Axis(ticks=False, labels=False, title='')),
       y=alt.Y('Rating', axis=alt.Axis(grid=False)),
       color='Gender'
    ).configure_view(
        stroke=None,
    ).properties(width=100)
    
    chart.properties(
        title=alt.TitleParams(
            ['This is a footer.'],
            baseline='bottom',
            orient='bottom',
            anchor='end',
            fontWeight='normal',
            fontSize=10,
            dy=5 
        ))
    

    【讨论】:

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