【问题标题】:Altair Ridgeline doesn't create a plot with nominal groupsAltair Ridgeline 不会创建带有名义组的图
【发布时间】:2021-04-26 10:50:04
【问题描述】:

我尝试在 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 库中的代码创建了我的图表: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'...) 时,它认为我的数据是临时月份,但工作正常:

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

【问题讨论】:

    标签: python altair vega-lite vega ridgeline-plot


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 2023-01-18
      • 2020-07-05
      相关资源
      最近更新 更多