![](/img/trans.png)
[英]Altair/Vega-Lite bar chart: filter top K bars from aggregated field
[英]Add data labels to vega-lite bar chart
我有一個相當簡單的條形圖,使用 Python altair 庫創建,基於 Pandas DataFrame。
生成圖表的代碼是:
Chart(df).configure(background='white').mark_bar().encode(
X('user_id', bin=Bin(maxbins=10), title='Subscriber count'),
Y('count(*)', title='Number of publications')
)
這轉化為以下 vega-lite 語法:
{
"encoding":
{
"x":
{
"bin":
{
"maxbins": 10
}, "field": "user_id",
"title": "Subscriber count",
"type": "quantitative"
},
"y":
{
"aggregate": "count",
"field": "*",
"title": "Number of publications"
}
},
"mark": "bar"
}
我唯一想添加的是每個條形中(或上)的實際值,最好逆時針旋轉 90°。
到目前為止,我只能找到mark_text
功能,如果我使用分層,這可能是一個選項,但我找不到如何旋轉文本。 當然,如果有更好的方法(或者根本不可能),請告訴! 謝謝!
您可以使用 文本配置按指定角度旋轉。 本質上,您可能想要執行以下操作: mark_text(angle=-90, dx=10)
。
import altair as alt
from vega_datasets import data
source = data.wheat()
bars = alt.Chart(source).mark_bar().encode(
x='wheat:Q',
y="year:O"
)
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='wheat:Q'
)
(bars + text).properties(height=900)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.