0

在 python3 中,我有一个关于熊猫的数据框:

import pandas as pd
import altair as alt

nomes_dep.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 120 entries, 0 to 119
Data columns (total 3 columns):
Deputado       120 non-null object
Valor          120 non-null object
porcentagem    120 non-null float64
dtypes: float64(1), object(2)
memory usage: 2.9+ KB

    Deputado                Valor           porcentagem
0   ITAMAR BORGES           1,225,322.51    1.60
1   CARLAO PIGNATARI        1,205,708.42    1.57
2   CAMPOS MACHADO          1,155,406.50    1.51
3   ALENCAR SANTANA BRAGA   1,149,436.08    1.50
4   FELICIANO FILHO         1,134,941.68    1.48

“Deputado”列有人名。“porcentagem”列包含这些人的支出占机构总数的百分比

使用 altair,我尝试制作一个水平条形图,X 轴为“porcentagem”列的值,Y 轴为“Deputado”列的名称:

alt.Chart(nomes_dep.reset_index().head(10), title="Deputados com maiores totais de despesas em %").mark_bar().encode(
    x=alt.X("porcentagem:Q", axis=alt.Axis(title="Porcentagem", format="%s")),
    y=alt.Y(
        "Deputado:N",
        sort=alt.SortField(field="Porcentagem", op="sum", order="descending"),
        axis=alt.Axis(title="Deputados")
    )
)

但在 jupyter 实验室中,我收到了以下信息:

Javascript Error: t is undefined. This usually means there's a typo in your chart specification. See the JavaScript console for the full traceback.

请问,有谁知道我怎样才能避免这个错误?

4

1 回答 1

2

"%s"不是有效的数据格式化程序,因此在渲染绘图时会导致错误。也许你的意思是".0%"

有关 Altair 支持的格式字符串的更多信息,请参阅d3-format

于 2018-07-17T19:28:21.017 回答