要绘制一个带有标签的Altair流图,您可以按照以下步骤进行操作:
import altair as alt
import pandas as pd
data = pd.DataFrame({
'x': ['A', 'B', 'C', 'D', 'E'],
'y': [5, 3, 2, 4, 1],
'label': ['Label A', 'Label B', 'Label C', 'Label D', 'Label E']
})
chart = alt.Chart(data).mark_bar().encode(
x='x',
y='y'
)
text = chart.mark_text(
align='center',
baseline='bottom',
dy=-10 # 调整标签的垂直位置
).encode(
text='label'
)
chart = chart + text
chart.show()
完整的代码示例:
import altair as alt
import pandas as pd
data = pd.DataFrame({
'x': ['A', 'B', 'C', 'D', 'E'],
'y': [5, 3, 2, 4, 1],
'label': ['Label A', 'Label B', 'Label C', 'Label D', 'Label E']
})
chart = alt.Chart(data).mark_bar().encode(
x='x',
y='y'
)
text = chart.mark_text(
align='center',
baseline='bottom',
dy=-10
).encode(
text='label'
)
chart = chart + text
chart.show()
这将绘制一个带有标签的Altair流图,其中x轴表示类别,y轴表示值,并在每个条形图上方显示标签。