[英]Dual Axis on Altair - filtering source
Altair 圖表在 Python 中非常出色,因為它很容易添加懸停數據注釋(與 Seaborn 不同)並且添加額外的“標記”(線條、條形等)比 Plotly 更清晰,但我正在努力解決的一件事是雙軸。 這通常工作正常(類似於文檔https://altair-viz.github.io/gallery/layered_chart_with_dual_axis.html ):
import altair as alt
from vega_datasets import data
source = data.seattle_weather()
chart_base = alt.Chart(source.query("weather != 'snow'")).encode(
alt.X('date:T',
axis=alt.Axis(),
scale=alt.Scale(zero=False)
)
).properties(width=800, height=400)
line_prec = chart_base.mark_line(color='blue', size=1).encode(y='mean(precipitation)')
line_temp = chart_base.mark_line(color='red', size=1).encode(y='mean(temp_max)')
alt.layer(line_prec, line_temp).resolve_scale(y='independent')
...所以我需要做的就是過濾單個標記,類似於 source.query 但說我們只想在“weather == 'rain'”和 temp_max where “weather != 'rain'”(是的,沒有分析洞察力,只是為了說明雙過濾器的工作示例)。
解決方案可能是Altair 組合了多個數據集,但 DRYer 代碼不是雙軸並且更有希望,但多層方法似乎不適用於雙軸,因為我們在嘗試添加(相當拼命).resolve_scale( y='獨立'):
chart_precip = alt.Chart(source.query("weather == 'rain'")).mark_line(color='blue', size=1).encode(
y='mean(precipitation)',
x=('date:T')
).properties(width=500, height=300) #.resolve_scale(y='independent') # can't add resolve_scale
chart_temp = alt.Chart(source.query("weather != 'rain'")).mark_line(color='red', size=1).encode(
y='mean(temp_max)',
x=('date:T')
).properties(width=500, height=300) #.resolve_scale(y='independent') # can't add resolve_scale
chart_precip + chart_temp
它有效但沒有標記雙軸,這是不好的:
那么......有沒有辦法簡單地單獨過濾雙軸標記?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.