Skip to content
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

Closed
Sunamur opened this issue Jul 31, 2019 · 2 comments
Closed

Creating Marimekko chart #1632

Sunamur opened this issue Jul 31, 2019 · 2 comments
Labels

Comments

@Sunamur
Copy link

Sunamur commented Jul 31, 2019

I am trying to create a Marimekko chart - Altair's grammar seems capable enough to generate them.

That is my best attempt yet:

data = {
    'year':[2017,2017,2017,2017,2018,2018,2018,2018,2019,2019,2019,2019],
    'yearly_weight':[340,340,340,340,170,170,170,170,520,520,520,520],
    'category':['food','medicine', 'rent','other','food','medicine', 'rent','other','food','medicine', 'rent','other',],
    'category_weight':[12,15,14,13,16,22,35,15,17,17,10,23]
}

df = pd.DataFrame(data)
alt.Chart(df).mark_bar().encode(
    x='year:O',
    y=alt.Y('category_weight:Q',stack="normalize" ),
    color='category:N',
    size='yearly_weight:Q'
).properties(width=100,height=100)

Marimekko attempt

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?

@jakevdp
Copy link
Collaborator

jakevdp commented Jul 31, 2019

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']
)

visualization (40)

@Sunamur
Copy link
Author

Sunamur commented Aug 1, 2019

Thanks for reply, @jakevdp !
I would remove first normalize (so that the X axis is more informative) and add borders to divide the blocks.

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 )

image_2019-08-01_15-58-11

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants