I try to create a Ridgeline plot in altair. Let’s assume my dataframe consists of str and float columns:
JavaScript
x
4
1
a object
2
b float64
3
dtype: object
4
with values like
JavaScript
1
7
1
a b
2
0 25 2303.0
3
1 29 2676.0
4
2 18 2983.0
5
3 16 1489.0
6
4 21 3468.0
7
I used code from Altair gallery to create my chart: https://altair-viz.github.io/gallery/ridgeline_plot.html. My code with changed data and column names:
JavaScript
1
54
54
1
import pandas as np
2
import numpy as np
3
4
source = pd.DataFrame(columns=list('ab'))
5
source['a'] = np.random.randint(0,17,size=500)
6
source['a'] = source['a'].astype('str')
7
source['b'] = np.random.randint(1000,5000,size=500).astype('float')
8
9
import altair as alt
10
11
step = 20
12
overlap = 1
13
14
alt.Chart(source, height=step).transform_joinaggregate(
15
mean_temp='mean(b)', groupby=['a']
16
).transform_bin(
17
['bin_max', 'bin_min'], 'b'
18
).transform_aggregate(
19
value='count()', groupby=['a', 'b', 'bin_min', 'bin_max']
20
).transform_impute(
21
impute='value', groupby=['a', 'b'], key='bin_min', value=0
22
).mark_area(
23
interpolate='monotone',
24
fillOpacity=0.8,
25
stroke='lightgray',
26
strokeWidth=0.5
27
).encode(
28
alt.X('bin_min:Q', bin='binned', title=''),
29
alt.Y(
30
'value:Q',
31
scale=alt.Scale(range=[step, -step * overlap]),
32
axis=None
33
),
34
alt.Fill(
35
'b:Q',
36
legend=None,
37
)
38
).facet(
39
row=alt.Row(
40
'a:T',
41
title=None,
42
header=alt.Header(labelAngle=0, labelAlign='right', format='%B')
43
)
44
).properties(
45
title='',
46
bounds='flush'
47
).configure_facet(
48
spacing=0
49
).configure_view(
50
stroke=None
51
).configure_title(
52
anchor='end'
53
)
54
When I use row=alt.Row('a:T'...)
it thinks my data is temporal month, but works fine:
But when I change type there to nominal 'a:N'
, result is empty. How to fix it?
Advertisement
Answer
For chart rendering errors like this, often there are clues in the browser’s developer console. In this case the following error is reported:
JavaScript
1
2
1
vega@5?noext:1 ERROR Error: invalid format: %B
2
I believe that "%B"
is not a valid format code for nominal data. If you remove format='%B'
from the header, the nominal row encoding will work.