问题描述
我正在使用Altair创建包含多条线的图表,每条线都有多个波段(代表不同的CI)&我正在努力了解如何添加图例.例如,在这个简单得多的示例中:
I'm using Altair to create a chart with multiple lines each of which has multiple bands (representing different CI) & I'm struggling to understand how I can add a legend. For example, in this considerably simpler example:
import altair as alt
import pandas as pd
df = pd.DataFrame(data={'col1': [1, 2,4,5,6], 'col2': [3, 4,7,4,4], 'col3': [1.5, 2.6,4.6,5.6,6.6], 'col4': [3.6, 4.6,7.6,4.6,4.4],'col5': [1.9, 2.9,4.9,5.9,6.9], 'col4': [3.9, 4.9,7.9,4.9,4.9]})
line = alt.Chart(df).mark_line(color='purple').encode(
x=alt.X('col1', title='Day'),
y=alt.Y('col2', title='Column 2')
)
band_90 = alt.Chart(df).mark_area(opacity=0.3, color='purple').encode(
x=alt.X('col1', title='Day'),
y='col3',
y2='col4',
)
band_50 = alt.Chart(df).mark_area(opacity=0.2, color='purple').encode(
x=alt.X('col1', title='Day'),
y='col4',
y2='col5',
)
alt.layer(
line+band_90+band_50
).save('chart.html')
如何为&行添加图例乐队?我了解通常的做法是通过尺寸&颜色,但数据源&如果可能的话,我正在使用的数据会让我想这样做吗?
How can I add a legend for the line & the bands? I understand that the normal way to do it is via size & color but the data source & data I'm using makes me wanna do it this way if possible?
(请注意-乐队看上去很愚蠢,完全是伪造的数据)
(Please note - the bands admittedly look v. silly, it's completely fake data)
推荐答案
将图例添加到Altair/Vega-Lite中的图表的唯一方法是添加将由图例表示的编码.因此,您不能直接回答我如何不使用尺寸/颜色来添加图例"的问题.
The only way to add a legend to a chart in Altair/Vega-Lite is to add an encoding that will be represented by the legend. So the direct answer to your question, "How can I add a legend without using size/color" is that you can't.
目前最好的方法是使用变换.像这样的东西:
The current best way to do this is to use a transform; something like this:
import altair as alt
import pandas as pd
df = pd.DataFrame(data={'col1': [1, 2,4,5,6], 'col2': [3, 4,7,4,4], 'col3': [1.5, 2.6,4.6,5.6,6.6], 'col4': [3.6, 4.6,7.6,4.6,4.4],'col5': [1.9, 2.9,4.9,5.9,6.9], 'col4': [3.9, 4.9,7.9,4.9,4.9]})
base = alt.Chart(df).transform_calculate(
line="'line'",
shade1="'shade1'",
shade2="'shade2'",
)
scale = alt.Scale(domain=["line", "shade1", "shade2"], range=['red', 'lightblue', 'darkblue'])
line = base.mark_line(color='purple').encode(
x=alt.X('col1', title='Day'),
y=alt.Y('col2', title='Column 2'),
color=alt.Color('line:N', scale=scale, title=''),
)
band_90 = base.mark_area(opacity=0.3, color='purple').encode(
x=alt.X('col1', title='Day'),
y='col3',
y2='col4',
color=alt.Color('shade1:N', scale=scale, title='')
)
band_50 = base.mark_area(opacity=0.2, color='purple').encode(
x=alt.X('col1', title='Day'),
y='col4',
y2='col5',
color=alt.Color('shade2:N', scale=scale, title='')
)
alt.layer(
line+band_90+band_50
)
将来,当Altair支持Vega-Lite的数据定义,这种方法将不再那么冗长.
In the future, when Altair supports Vega-Lite's datum definition, this approach will be a bit less verbose.
这篇关于将图例添加到行&不使用尺寸/颜色的Altair图表的柱线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!