簡體   English   中英

反轉軸方向 Altair

[英]Invert axis direction Altair

出於某種原因,使用 altair 繪圖時的 Y 軸似乎是倒置的(期望值從圖的較低(底部)到較高(頂部)為 go)。 另外,我希望能夠更改滴答頻率。 對於舊版本,我可以使用ticks=n_ticks但現在看來這個參數只能使用 boolean。

import altair as alt
alt.renderers.enable('notebook')

eff_metals =  pd.read_excel(filename, sheet_name='summary_eff_metals')
points = alt.Chart(eff_metals, height=250, width=400).mark_circle().encode(
    x=alt.X('Temperature:Q',axis=alt.Axis(title='Temperature (°C)'),
            scale=alt.Scale(zero=False, padding=50)),
    y=alt.Y('Efficiency:N',axis=alt.Axis(title='Efficiency (%)'),
            scale=alt.Scale(zero=False, padding=1)),
    color=alt.Color('Element:N'),
)
text = points.mark_text(align='right', dx=0, dy=-5).encode(
    text='Element:N'
)
chart = alt.layer(points, text, data=eff_metals, 
                  width=600, height=300)
chart

和圖: 在此處輸入圖像描述

雖然可以手動反轉domain ,但這需要對邊界進行硬編碼。

相反,我們可以將Scale(reverse=True)傳遞給軸編碼,例如:

from vega_datasets import data

alt.Chart(data.wheat().head()).mark_bar().encode(
    x='wheat:Q',
    y=alt.Y('year:O', scale=alt.Scale(reverse=True)),
)

這里它被傳遞給alt.Y ,所以年份是倒置的(左)與默認的y='year:O' (右):

反轉與默認 y 軸

我沒有您的數據,因此很難編寫工作代碼。

但是,這是一個帶有附加刻度的倒置比例的示例,它擴展了簡單的散點圖示例。 這里它在維加編輯器。

import altair as alt
from vega_datasets import data

iris = data.iris()

alt.Chart(iris).mark_point().encode(
    x='petalWidth',
    y=alt.Y('petalLength', scale=alt.Scale(domain=[7,0]), axis=alt.Axis(tickCount=100)),
    color='species'
).interactive()

這可能適用於您的數據:

eff_metals =  pd.read_excel(filename, sheet_name='summary_eff_metals')
points = alt.Chart(eff_metals, height=250, width=400).mark_circle().encode(
    x=alt.X('Temperature:Q',axis=alt.Axis(title='Temperature (°C)'),
            scale=alt.Scale(zero=False, padding=50)),
    y=alt.Y('Efficiency:N',axis=alt.Axis(title='Efficiency (%)'),
            scale=alt.Scale(zero=False, padding=1, domain=[17,1])),
    color=alt.Color('Element:N'),
)
text = points.mark_text(align='right', dx=0, dy=-5).encode(
    text='Element:N'
)
chart = alt.layer(points, text, data=eff_metals, 
                  width=600, height=300)
chart

但是,我認為效率變量的類型可能有誤。 您可以嘗試將'Efficiency:N'替換為'Efficiency:N' 'Efficiency:Q',那可以做到嗎?

暫無
暫無

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

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