我可以在Altair中的Legend类中执行此自定义图例吗?

时间:2019-10-30 12:11:51

标签: vega altair vega-lite

https://image.prntscr.com/image/22S0nz5YR3CaT-x4tBH8ig.png

我需要做一个这样的传奇。另外: 1.另外,我也想解决x轴问题(显示.991和.100,而不是1950 1991 2020 2100) 2.创建字幕仍然是不可能的吗?我应该在第二层添加标题吗?

使用Altair插件编写(并且最感兴趣的是它是否可以在没有其他层的情况下完成(仅使用Legend类)。

from altair import datum
import pandas as pd
url = "https://gist.githubusercontent.com/turiy/005f2ce11637fefcde8e9d6efdb0c2e6/raw/19e67bb3a6d63e7fd9f49a596e5d24404469bd63/population_prospects.csv"
df = pd.read_csv(url)

base = alt.Chart(df).transform_calculate(population=datum.population/1000).properties(title='Ukraine Population Prospecrs')
x_axis = alt.Axis(values=[1950,1991,2020,2100], domain=False, gridDash=[1,1])
x_scale = alt.Scale(domain=[1950, 2100])
x=alt.X('year:T', scale=x_scale, axis=x_axis)
y_axis = alt.Axis(domain=False, gridDash=[1,1])
y_scale = alt.Scale(domain=[15, 55])
y=alt.Y('population:Q', scale=y_scale, axis=y_axis) 
base = base.encode(x=x, y=y)


layer1 = alt.layer(base.mark_line(color="#3288bd").transform_filter(alt.FieldRangePredicate(field="year", range=[1950, 2020])))
layer2 = alt.layer(base.mark_line(strokeDash=[6, 2]).transform_filter(alt.FieldRangePredicate(field="year", range=[2020, 2100])))
layer2 = layer2.encode(color=alt.Color("type:N", legend=None, scale=alt.Scale(range=["#d53e4f", "#d53e4f"])))
#.transform_calculate(population=datum.population/1000).properties(title='Ukraine Population Prospecrs')

我现在得到的是 https://image.prntscr.com/image/J8Qmb27RQR2UKMpZpSJcFg.png

0 个答案:

没有答案