簡體   English   中英

如何在Altair中設置標簽文字大小?

[英]How do I set label text size in Altair?

我正在使用Altair可視化一些數據,並希望在圖表上放置標簽。 我以https://altair-viz.github.io/gallery/scatter_with_labels.html為例。

我要調整點的大小,但不調整標簽文本的大小。

從上面的鏈接:

import altair as alt
import pandas as pd

source = pd.DataFrame({
    'x': [1, 3, 5, 7, 9],
    'y': [1, 3, 5, 7, 9],
    'label': ['A', 'B', 'C', 'D', 'E']
})

points = alt.Chart(source).mark_point().encode(
    x='x:Q',
    y='y:Q'
)

text = points.mark_text(
    align='left',
    baseline='middle',
    dx=7
).encode(
    text='label'
)

points + text

這按預期工作。

但是,如果我想改變點的大小以顯示其他信息,請在points塊中進行調整,使其現在顯示為:

x='x:Q',
y='y:Q'
size='x'
)

不幸的是,我們現在的文字大小也隨x增加。 哎呀!

text塊內設置size命令不會像我期望的那樣覆蓋文本大小。 如何在Altair中設置Axis FontSize的軸配置建議 還沒有解決我的問題。 如果我使用https://altair-viz.github.io/user_guide/generated/toplevel/altair.Chart.html#altair.Chart.configure_text中的 configure_text ,我將得到: ValueError: Objects with "config" attribute cannot be used within LayerChart. Consider defining the config attribute in the LayerChart object instead. ValueError: Objects with "config" attribute cannot be used within LayerChart. Consider defining the config attribute in the LayerChart object instead. 而且不知道下一步要去哪里。

標記大小更改時,如何使文本保持相同大小?

當你做text = points.mark_text()text layr繼承您在定義的所有points層:在這里,包含size編碼。

如果您不希望text圖層具有大小編碼,則只能為points圖層指定它。 例如,您可以執行以下操作:

points = alt.Chart(source).mark_point().encode(
    x='x:Q',
    y='y:Q',
    size='x'
)

text = alt.Chart(source).mark_text(
    align='left',
    baseline='middle',
    dx=7
).encode(
    x='x:Q',
    y='y:Q',
    text='label'
)

points + text

mark_text()塊中的設置size之所以不能覆蓋,是因為編碼總是會取代標記屬性。 有關更多信息,請參見https://altair-viz.github.io/user_guide/customization.html#global-config-vs-local-config-vs-encoding

暫無
暫無

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

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