1

我正在尝试在 altair 中创建一个重复的图表。

这是我的代码:

x = alt.Chart(data).mark_bar().encode(
    alt.X(alt.repeat("row"), type='quantitative'),
    alt.Y(alt.repeat("column"), type='quantitative')
).repeat(
row= ['country'],
column=['alcohol_use', 'drug_use', 'high_meat', 'low_exercise', 'smoking'])

当我运行它时,它只显示带有轴的图形,没有别的。有什么问题?

4

2 回答 2

2

如果您在重复图表中得到空图,通常意味着以下两种情况之一:

  1. 前端无法访问您的数据。例如,如果您将数据作为 URL 传递并且 URL 有拼写错误,则可能会发生这种情况。
  2. 行/列中使用的列名输入错误。

作为第二个问题的示例,请考虑Altair 文档中的此图表:

import altair as alt
from vega_datasets import data
iris = data.iris.url

alt.Chart(iris).mark_point().encode(
    alt.X(alt.repeat("column"), type='quantitative'),
    alt.Y(alt.repeat("row"), type='quantitative'),
    color='species:N'
).properties(
    width=200,
    height=200
).repeat(
    row=['petalLength', 'petalWidth'],
    column=['sepalLength', 'sepalwidth']
).interactive()

在此处输入图像描述

现在让我们看看如果我们拼错列名会发生什么(这里我们的“拼写错误”涉及将大写字符更改为小写):

alt.Chart(iris).mark_point().encode(
    alt.X(alt.repeat("column"), type='quantitative'),
    alt.Y(alt.repeat("row"), type='quantitative'),
    color='species:N'
).properties(
    width=200,
    height=200
).repeat(
    row=['petallength', 'petalwidth'],
    column=['sepallength', 'sepalwidth']
).interactive()

在此处输入图像描述

确保您没有拼错列名:常见问题是大写与小写、特殊字符以及列名字符串开头或结尾处的空格。

于 2018-10-12T15:37:16.237 回答
0

原来我需要切换alt.X(alt.repeat("row"), type='quantitative'),到, alt.X(alt.repeat("row"), type='ordinal'),因为行数据是国家名称。

于 2018-10-14T02:23:21.197 回答