问题描述
我尝试在 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'...)
时,它认为我的数据是临时月份,但工作正常:
但是当我将那里的类型更改为名义 'a:N'
时,结果为空。如何解决?
解决方法
对于像这样的图表渲染错误,浏览器的开发者控制台中通常会有线索。在这种情况下,会报告以下错误:
vega@5?noext:1 ERROR Error: invalid format: %B
我认为 "%B"
不是名义数据的有效格式代码。如果您从标题中删除 format='%B'
,名义行编码将起作用。