How to create a nested Grouped Bar Chart using Altair? - Added sample data

点点圈 提交于 2019-12-02 13:44:28

The only way to do side-by-side bars is to use a column encoding alongside an x encoding. You can follow the grouped bar chart example from Altair's example gallery here: https://altair-viz.github.io/gallery/grouped_bar_chart.html

The result for your data would be something like this:

import altair as alt
import pandas as pd
import numpy as np

q13a = pd.DataFrame({
    'year': np.random.randint(2000, 2018, 100),
    'Number_of_Incidents': np.random.randint(0, 20, 100),
    'percent_arrest': np.random.rand(100),
    'primary_type': np.random.choice(['a', 'b', 'c', 'd', 'e'], 100)
})

alt.Chart(q13a).mark_bar().encode(
    x=alt.X('primary_type', scale=alt.Scale(rangeStep=8), title=None),
    y=alt.Y('sum(Number_of_Incidents)', title='sum(Number_of_Incidents)'),
    color='primary_type',
    column='year',
    tooltip=['year']
).configure_view(
    stroke='transparent'
)

@jakevdp has already answered, but for the sake of answering your last question (even though you should ask several questions if you have several points), you can sort the bars with alt.EncodingSortField.

import altair as alt

alt.Chart(q13a).mark_bar().encode(
    x=alt.X('primary_type:N', 
            scale=alt.Scale(rangeStep=8), 
            axis=None,
            sort=alt.EncodingSortField(field='Number_of_Incident', 
                                       op='sum', 
                                       order='ascending')
           ),
    y=alt.Y('sum(Number_of_Incidents):Q', title='sum(Number_of_Incidents)'),
    color='primary_type:N',
    tooltip=['primary_type:N', 
             'year:O', 
             alt.Tooltip('sum(Number_of_Incidents):Q',
                         title='Number of incidents')
            ]
).facet(
    column=alt.Column('year:O', 
                      header=alt.Header(title=None)),
).resolve_scale(
    x='independent'
).configure_view(
    stroke='transparent'
)


Data

import pandas as pd
from io import StringIO

q13a = pd.read_table(StringIO("""year   primary_type    Number_of_Incidents number_of_arrests   percent_arrest  rank
2018    THEFT   57330   5503    9.6     1
2018    BATTERY     44667   8886    19.89   2
2018    CRIMINAL DAMAGE     24889   1498    6.02    3
2018    ASSAULT     18229   2931    16.08   4
2018    DECEPTIVE PRACTICE  15879   713     4.49    5
2017    THEFT   64334   6459    10.04   1
2017    BATTERY     49213   10060   20.44   2
2017    CRIMINAL DAMAGE     29040   1747    6.02    3
2017    ASSAULT     19298   3455    17.9    4
2017    DECEPTIVE PRACTICE  18816   805     4.28    5
2016    THEFT   61600   6518    10.58   1
2016    BATTERY     50292   10328   20.54   2
2016    CRIMINAL DAMAGE     31018   1668    5.38    3
2016    ASSAULT     18738   3490    18.63   4
2016    DECEPTIVE PRACTICE  18733   815     4.35    5
2015    THEFT   57335   6771    11.81   1
2015    BATTERY     48918   11558   23.63   2
2015    CRIMINAL DAMAGE     28675   1835    6.4     3
2015    NARCOTICS   23883   23875   99.97   4
2015    OTHER OFFENSE   17552   4795    27.32   5
2014    THEFT   61561   7415    12.04   1
2014    BATTERY     49447   12517   25.31   2
2014    NARCOTICS   29116   29000   99.6    3
2014    CRIMINAL DAMAGE     27798   2095    7.54    4
2014    OTHER OFFENSE   16979   4159    24.49   5
2013    THEFT   71530   7727    10.8    1
2013    BATTERY     54002   12927   23.94   2
2013    NARCOTICS   34127   33819   99.1    3
2013    CRIMINAL DAMAGE     30853   2107    6.83    4
2013    OTHER OFFENSE   17993   3400    18.9    5"""))

Created on 2018-11-28 by the reprexpy package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!