在 Altair 中将图例制作成多列
Making a legend into multiple columns in Altair
抱歉无法提供大量代码,一切都是相互关联的,现在不可能。
我的问题是我创建了一个点图作为 "interactive legend"。
legend = alt.Chart(source).mark_point().encode(
y=alt.Y('STATE', axis=alt.Axis(orient='right')),
).add_selection(
select_state
)
问题是列出了 50 个州。因此,图表变得很长,无法在一个屏幕上显示所有内容。
有什么方法可以以某种方式包装此图表,使其显示在多列中吗?鉴于图例是单柱点图,我认为这是不可能的。
有没有什么方法可以在 Altair 中将其转换成某种结构,从而可以包装成多个不是图表的列?
或者,有没有办法重新定位我的滑块?它出现在底部 :( 如果它出现在顶部,我认为它可以与其他所有内容出现在同一屏幕上,因此图例图表不会有太大问题。
slider = alt.binding_range(min=1992, max=2016, step=1)
# 1st selection filter
select_year = alt.selection_single(name="YEAR", fields=['YEAR'],
bind=slider, init={'YEAR': 1992})
您可以指定编码的 legend.columns
属性 来控制图例中的列数。例如,使用汽车数据集:
import altair as alt
from vega_datasets import data
alt.Chart(data.cars.url).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color=alt.Color('Name:N', legend=alt.Legend(columns=8))
).properties(
# Adjust chart width and height to match size of legend
width=600,
height=600
)
虽然有这么多属性,但传说在实践中变得不是很有用。您可能会考虑使用 tooltip
编码,而不是像这样显示详细信息。
看看这个例子:
import altair as alt
from vega_datasets import data
source = data.unemployment_across_industries.url
selection = alt.selection_multi(fields=['series'], bind='legend')
alt.Chart(source).mark_area().encode(
alt.X('yearmonth(date):T', axis=alt.Axis(domain=False, format='%Y', tickSize=0)),
alt.Y('sum(count):Q', stack='center', axis=None),
alt.Color('series:N', scale=alt.Scale(scheme='category20b')),
opacity=alt.condition(selection, alt.value(1), alt.value(0.2))
).add_selection(
selection
)
参考:https://altair-viz.github.io/gallery/interactive_legend.html
抱歉无法提供大量代码,一切都是相互关联的,现在不可能。
我的问题是我创建了一个点图作为 "interactive legend"。
legend = alt.Chart(source).mark_point().encode(
y=alt.Y('STATE', axis=alt.Axis(orient='right')),
).add_selection(
select_state
)
问题是列出了 50 个州。因此,图表变得很长,无法在一个屏幕上显示所有内容。
有什么方法可以以某种方式包装此图表,使其显示在多列中吗?鉴于图例是单柱点图,我认为这是不可能的。
有没有什么方法可以在 Altair 中将其转换成某种结构,从而可以包装成多个不是图表的列?
或者,有没有办法重新定位我的滑块?它出现在底部 :( 如果它出现在顶部,我认为它可以与其他所有内容出现在同一屏幕上,因此图例图表不会有太大问题。
slider = alt.binding_range(min=1992, max=2016, step=1)
# 1st selection filter
select_year = alt.selection_single(name="YEAR", fields=['YEAR'],
bind=slider, init={'YEAR': 1992})
您可以指定编码的 legend.columns
属性 来控制图例中的列数。例如,使用汽车数据集:
import altair as alt
from vega_datasets import data
alt.Chart(data.cars.url).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color=alt.Color('Name:N', legend=alt.Legend(columns=8))
).properties(
# Adjust chart width and height to match size of legend
width=600,
height=600
)
虽然有这么多属性,但传说在实践中变得不是很有用。您可能会考虑使用 tooltip
编码,而不是像这样显示详细信息。
看看这个例子:
import altair as alt
from vega_datasets import data
source = data.unemployment_across_industries.url
selection = alt.selection_multi(fields=['series'], bind='legend')
alt.Chart(source).mark_area().encode(
alt.X('yearmonth(date):T', axis=alt.Axis(domain=False, format='%Y', tickSize=0)),
alt.Y('sum(count):Q', stack='center', axis=None),
alt.Color('series:N', scale=alt.Scale(scheme='category20b')),
opacity=alt.condition(selection, alt.value(1), alt.value(0.2))
).add_selection(
selection
)
参考:https://altair-viz.github.io/gallery/interactive_legend.html