使用Altair的layer()方法将两个图层组合起来
示例代码:
import altair as alt
from vega_datasets import data
# 导入数据并创建第一个图层
source = data.cars()
bar_chart = alt.Chart(source).mark_bar().encode(
x='count()',
y='Origin',
color='Origin'
)
# 创建第二个图层
line_chart = alt.Chart(source).mark_line(color='red').encode(
x='Horsepower',
y='Miles_per_Gallon'
)
# 使用layer()方法将两个图层组合
combined_chart = alt.layer(bar_chart, line_chart)
# 显示图表
combined_chart.show()