[英]Altair Bar Chart - how to show missing categories
I have this chart below, which looks great.我有下面这张图表,看起来很棒。 Category '2' on the X-axis has however been omitted because there is no data available in the dataframe.
然而,X 轴上的类别“2”已被省略,因为数据框中没有可用数据。
What is the best way to go about this in Altair?在 Altair 中解决此问题的最佳方法是什么? I would like the '2' leadtime to show as an empty space/column (same width as a column).
我希望“2”提前期显示为空白空间/列(与列的宽度相同)。
import altair as alt
alt.Chart(data).mark_bar().encode(
x='leadtime:O',
y='value',
color='category'
)
Data数据
leadtime category value
1 cat1 1
1 cat2 1
1 cat3 1
1 cat4 2
1 cat5 2
1 cat6 0.5
1 cat7 3
3 cat1 2
3 cat2 3
3 cat3 2
3 cat4 4
3 cat5 5
3 cat6 0.5
3 cat7 6
4 cat1 2
4 cat2 4
4 cat3 2
4 cat4 7
4 cat5 8
4 cat6 8
4 cat7 3
You can do this in two ways, either using an impute transform in y:您可以通过两种方式执行此操作,或者使用 y 中的 插补变换:
alt.Chart(data).mark_bar().encode(
x='leadtime:O',
color='category',
y=alt.Y('value',
impute=alt.ImputeParams(
value=0,
keyvals=[1, 2, 3, 4]
)
),
)
or setting an explicit scale domain in x:或在 x 中设置显式比例域:
alt.Chart(data).mark_bar().encode(
x=alt.X('leadtime:O', scale=alt.Scale(domain=[1, 2, 3, 4])),
y='value',
color='category',
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.