New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating Marimekko chart #1632
Comments
Hmm... I don't think this is well supported by the grammar, but you can do something like this by transforming the data. Not perfect, but a rough idea: import altair as alt
import pandas as pd
df = pd.DataFrame({
'year': [2017, 2018, 2019],
'year_weight': [340, 170, 520],
'food': [12, 16, 17],
'medicine': [15, 22, 17],
'rent': [14, 35, 10],
'other': [13, 15, 23]
})
alt.Chart(df).transform_stack(
stack='year_weight',
as_=['x1', 'x2'],
offset='normalize',
groupby=[],
).transform_fold(
['food', 'medicine', 'rent', 'other'],
as_=['category', 'category_weight'],
).transform_stack(
stack='category_weight',
groupby=['year'],
offset='normalize',
as_=['y1', 'y2'],
).mark_rect().encode(
x='x1:Q', x2='x2:Q',
y='y1:Q', y2='y2:Q',
color='category:N',
tooltip=['year:N', 'category:N']
) |
Thanks for reply, @jakevdp ! import altair as alt
import pandas as pd
df = pd.DataFrame({
'year': [2017, 2018, 2019],
'year_weight': [340, 170, 520],
'food': [12, 16, 17],
'medicine': [15, 22, 17],
'rent': [14, 35, 10],
'other': [13, 15, 23]
})
alt.Chart(df).transform_stack(
stack='year_weight',
as_=['x1', 'x2'],
groupby=[],
).transform_fold(
['food', 'medicine', 'rent', 'other'],
as_=['category', 'category_weight'],
).transform_stack(
stack='category_weight',
groupby=['year'],
offset='normalize',
as_=['y1', 'y2'],
).mark_rect(strokeWidth=0.3).encode(
x=alt.X('x1:Q', title='Yearly expenses'),
x2='x2:Q',
y=alt.Y('y1:Q',title='Expense item share'),
y2='y2:Q',
fill='category:N',
tooltip=['year:N', 'category:N'],
stroke=alt.value('black'),
).configure_axis( grid=False ) Biggest challenge now would be adding nominal X labels (year in our case) without tooltips or legend. Big fan of Altair, almost completely switched to it for doing work stuff! |
I am trying to create a Marimekko chart - Altair's grammar seems capable enough to generate them.
That is my best attempt yet:
The problem seems to be that X ticks may only be spaced evenly, which is not desirable when creating Marimekko.
Is there any way to fix this?
The text was updated successfully, but these errors were encountered: