【问题标题】:Add background solid fill to Altair graph向 Altair 图形添加背景实心填充
【发布时间】:2021-06-23 21:32:57
【问题描述】:

我非常喜欢 Altair 用 Python 制作图表。作为致敬,我想在"Mistakes, we’ve drawn a few" 中重新生成经济学人图表: 这是第一枪的代码:

import numpy as np
import pandas as pd
import altair as alt

 df = pd.read_csv('http://infographics.economist.com/databank/Economist_corbyn.csv').dropna()

 bars = alt.Chart(df, title="Average number of likes per Facebook post").mark_bar().
    encode(
        y=alt.Y('Page:O', axis=alt.Axis(title=''),
        sort=alt.EncodingSortField(
           field="Average number of likes per Facebook post 2016:Q",  # The field to use for the sort
           op="sum",  # The operation to run on the field prior to sorting
           order="ascending"  # The order to sort in
       )),
       color=alt.value("#116EA1"),
       x=alt.X("Average number of likes per Facebook post 2016:Q", 
       axis=alt.Axis(title='Average number of likes per Facebook post')),
)


text = bars.mark_text(
    align='left',
    baseline='middle',
    dx=3,  # Nudges text to right so it doesn't appear on top of the bar,
).encode(
    text='Average number of likes per Facebook post 2016:Q'
)

(bars+text).configure_title(fontSize=14)

四个问题:

  1. 如何用纯色##D9E9F0 填充背景?
  2. 如何使第一行弹出(通过在粗体标签上添加框 “杰里米·科尔宾”)?

  3. 我可以在左下角添加浅灰色的版权声明吗 版本 3 中新的 Altair 文本层可能性的角落? 如果是这样,该怎么做?

  4. 如何将 xticks 放在图表顶部?

感谢您的帮助!

更新来自jakevdpaberna 的 (1) 和 (4) 答案:

df['x'] = df['Average number of likes per Facebook post 2016']/1000.0
bars = alt.Chart(
    df, title="Average number of likes per Facebook post ('000)"
).mark_bar().encode(
    y=alt.Y('Page:O', axis=alt.Axis(title=''),
           sort=alt.EncodingSortField(
            field="Average number of likes per Facebook post 2016:Q",  # The field to use for the sort
            op="sum",  # The operation to run on the field prior to sorting
            order="ascending"  # The order to sort in
        )),
    color=alt.value("#116EA1"),
    x=alt.X("x:Q", 
            axis=alt.Axis(title='', orient="top"),
            scale=alt.Scale(round=True, domain=[0,5])),
)

bars.configure_title(fontSize=14).configure(background='#D9E9F0')

【问题讨论】:

    标签: python data-visualization altair


    【解决方案1】:

    回答您的一个问题:您可以使用configure(background="colorname") 添加背景。例如:

    (bars+text).configure_title(fontSize=14).configure(background='#DDEEFF')
    

    【讨论】:

    【解决方案2】:

    回答您的问题 4,您需要在 alt.Axis 中使用 orient="top" 参数。

    axis=alt.Axis(title='Average number of likes per Facebook post',orient="top"))
    

    【讨论】:

    • 非常感谢。我们快到了。
    【解决方案3】:

    有点回答问题 2。不是突出显示栏,而是通过条件字体样式突出显示标签。

    label_style = {
        "condition": [{"test" : "datum.value == 'Jeremy Corbyn'", "value": "bold"}],
        "value": "italic"
    }
    
    ...
    
    y=alt.Y("Page:N", axis=alt.Axis(title="", labelFontStyle=label_style))
    

    【讨论】:

    • 是的,谢谢您的回答。更简洁:axis=alt.Axis(labelFontSize=14, labelFontStyle=alt.condition('datum.value == "Jeremy Corbyn"', alt.value('bold'), alt.value('italic'))
    • 好点!我从我自己的代码中复制了较长的版本,其中我有多个不同样式的亮点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    相关资源
    最近更新 更多