簡體   English   中英

在 Altair 中更改 y 軸刻度數

[英]Changing Number of y-axis Ticks in Altair

我使用對稱對數 y 軸生成了這張圖片,比較了 2010 年至 2020 年十年期間比特幣與 REIT 的收益率:圖像

plot 是從此 DataFrame 生成的:

    btc_yields  Year    reit_yields
0   3619143193  2010    38791
1   45790476    2011    31261
2   37433124    2012    30274
3   2739779     2013    4051
4   808407      2014    282
5   1711472     2015    650
6   685726      2016    863
7   75958       2017    1324
8   49428       2018    976
9   34330       2019    -285
10  27005       2020    631

使用此代碼:

alt.Chart(merged_btc_reit_low_inf).transform_fold(
    ['btc_yields', 'reit_yields'],
).mark_line(strokeWidth = 5).encode(
    x=alt.X('Year:N', title = 'Year Investment Made'),
    y=alt.Y('value:Q', title = 'Returns in USD', 
            scale=alt.Scale(type='symlog'),
            axis=alt.Axis(tickCount=merged_btc_reit_low_inf.shape[0])),
    color='key:N',
    tooltip = 'value:Q'
).properties(
    width = 600,
    height = 400,
    title = 'Return on $10,000 Seed Investment in Bitcoin'
).configure_axis(
    labelFontSize=14,
    titleFontSize=16,
    labelAngle = 0
).configure_title(
    fontSize = 21
).configure_legend(
    labelFontSize = 14
)

我不確定為什么,即使使用axis=alt.Axis(tickCount=merged_btc_reit_low_inf.shape[0])也只有 3 個 y 軸刻度。 我想要一只手將 y 軸刻度的數量增加到足夠的 plot 提供更多信息,特別是考慮到對數轉換軸。

您可以顯式設置軸values ,而不是嘗試調整刻度數:

from itertools import product

...

axis=alt.Axis(values=[0] + [10**x * y for (x, y) in product(range(2, 9, 2), (1, -1))]))
# There might be a nicer way of composing this list...

在此處輸入圖像描述


我認為tickCount不適合您的原因是文檔中的這一部分:

結果數字可能不同,因此值是“不錯的”(2、5、10 的倍數)並且位於基礎比例的范圍內。

我不確定它們為什么不對稱,這個參數可能不適用於symlog ,所以你可能想在 VegaLite 存儲庫中打開一個功能請求。

暫無
暫無

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

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