python - Altair Ridgeline 不会创建标称组图

标签 python altair vega-lite vega ridgeline-plot

我尝试在 altair 中创建山脊线图。假设我的数据框由 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 gallery 中的代码创建了我的图表: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', groupby=['a', '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" 不是标称数据的有效格式代码。如果您从 header 中删除 format='%B',标称行编码将起作用。

关于python - Altair Ridgeline 不会创建标称组图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65837215/

相关文章:

javascript - Python:如何使用 python 单击网页中的按钮?

python - 在 python altair 中保存数字

python - 在 Altair fiddle 图上绘制中位数线和四分位数线

vega-lite - 使用 Vega-Lite 在指定日期范围内用 0 填充缺失数据

python - Altair:从偏移量向下指向的条形图

python - 导入Flask-SocketIO实例

python - 应用引擎 : map form data to url

python - 如何为用户建立配置文件 View ,该 View 还允许更新其信息

pyqt - 如何在 PyQt 小部件中渲染 Altair/Vega

python - 有没有办法用分箱数据反转 Altair 等值区域图的配色方案?