簡體   English   中英

在 Altair 中更改圖例的大小

[英]Changing Size of Legend in Altair

我很喜歡 Altair 創建等值分布圖! 但是,我最大的問題是我無法弄清楚如何更改圖例的大小。 我已經通讀了文檔並嘗試了幾件事無濟於事。

這是一個使用 Altair 文檔中按縣划分的失業地圖的示例。 我添加了一個“配置”層來更改地圖和圖例上標題的字體大小。 請注意“config”中代碼的 .configure_legend() 部分。

counties = alt.topo_feature(data.us_10m.url, 'counties')
source = data.unemployment.url

foreground = alt.Chart(counties).mark_geoshape(
    ).encode(
    color=alt.Color('rate:Q', sort="descending",  scale=alt.Scale(scheme='plasma'), legend=alt.Legend(title="Unemp Rate", tickCount=6))
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(source, 'id', ['rate'])
).project(
    type='albersUsa'
).properties(
    title="Unemployment Rate by County",
    width=500,
    height=300
)

config = alt.layer(foreground).configure_title(fontSize=20, anchor="middle").configure_legend(titleColor='black', titleFontSize=14) 

config

圖像應該是這樣的:

在此處輸入圖片說明

如果我像這樣更改地圖的大小:

counties = alt.topo_feature(data.us_10m.url, 'counties')
source = data.unemployment.url

foreground = alt.Chart(counties).mark_geoshape(
    ).encode(
    color=alt.Color('rate:Q', sort="descending",  scale=alt.Scale(scheme='plasma'), legend=alt.Legend(title="Unemp Rate", tickCount=6))
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(source, 'id', ['rate'])
).project(
    type='albersUsa'
).properties(
    title="Unemployment Rate by County",
    width=900,
    height=540
)

config = alt.layer(foreground).configure_title(fontSize=20, anchor="middle").configure_legend(titleColor='black', titleFontSize=14) 

config

圖例保持相同的大小,因此與地圖相比,它現在看起來很小:

在此處輸入圖片說明

或者,如果我使地圖尺寸變小,則圖例會很大!

在此處輸入圖片說明

我嘗試了十幾種不同的方法都無濟於事。

任何人都有解決方案?

如您所見,圖例具有以像素為單位的默認大小,無論圖表大小如何,該大小都是恆定的。 如果你想調整它,你可以使用configure_legend()圖表方法。

在 Altair 3.0 或更高版本中,以下參數是調整圖例漸變大小的相關參數:

chart.configure_legend(
    gradientLength=400,
    gradientThickness=30
) 

第一個答案非常接近,但缺少更改圖例中字體大小的最重要部分。 使用下面的代碼片段調整圖例中文本的字體大小。

 .configure_legend( titleFontSize=18, labelFontSize=15 )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM