微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Altair Ridgeline 不会创建带有名义组的图

如何解决Altair Ridgeline 不会创建带有名义组的图

我尝试在 altair 中创建 Ridgeline 图。假设我的数据框由 str 和 float 列组成:

a     object
b    float64
dtype: object

具有类似的值

    a       b
0  25  2303.0
1  29  2676.0
2  18  2983.0
3  16  1489.0
4  21  3468.0

我使用 altair 库中的代码来创建我的图表:https://altair-viz.github.io/gallery/ridgeline_plot.html。我更改了数据和列名的代码

import pandas as np
import numpy as np

source = pd.DataFrame(columns=list('ab'))
source['a'] = np.random.randint(0,17,size=500)
source['a'] = source['a'].astype('str')
source['b'] = np.random.randint(1000,5000,size=500).astype('float')

import altair as alt

step = 20
overlap = 1

alt.Chart(source,height=step).transform_joinaggregate(
    mean_temp='mean(b)',groupby=['a']
).transform_bin(
    ['bin_max','bin_min'],'b'
).transform_aggregate(
    value='count()',groupby=['a','b','bin_min','bin_max']
).transform_impute(
    impute='value','b'],key='bin_min',value=0
).mark_area(
    interpolate='monotone',fillOpacity=0.8,stroke='lightgray',strokeWidth=0.5
).encode(
    alt.X('bin_min:Q',bin='binned',title=''),alt.Y(
        'value:Q',scale=alt.Scale(range=[step,-step * overlap]),axis=None
    ),alt.Fill(
        'b:Q',legend=None,)
).facet(
    row=alt.Row(
        'a:T',title=None,header=alt.Header(labelAngle=0,labelAlign='right',format='%B')
    )
).properties(
    title='',bounds='flush'
).configure_facet(
    spacing=0
).configure_view(
    stroke=None
).configure_title(
    anchor='end'
)

当我使用 row=alt.Row('a:T'...) 时,它认为我的数据是临时月份,但工作正常:

enter image description here

但是当我将那里的类型更改为名义 'a:N' 时,结果为空。如何解决

解决方法

对于像这样的图表渲染错误,浏览器的开发者控制台中通常会有线索。在这种情况下,会报告以下错误:

vega@5?noext:1 ERROR Error: invalid format: %B

我认为 "%B" 不是名义数据的有效格式代码。如果您从标题中删除 format='%B',名义行编码将起作用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。