Python altair.Axis() Examples

The following are 3 code examples of altair.Axis(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module altair , or try the search function .
Example #1
Source File: core.py    From starborn with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def violinplot(x=None, y=None, data=None, orient=None):
    # TODO: automatically infer orientation

    if orient is None or orient == 'v':
        kwargs = dict(
                    x=alt.X('count(*):Q',
                            axis=alt.Axis(grid=False, labels=False),
                            stack='center',
                            title=''),
                    y=alt.Y('{y}:Q'.format(y=y), bin=alt.Bin(maxbins=100)),
                    column='{x}:N'.format(x=x),
                    color='{x}:N'.format(x=x)
        )
    else:
        kwargs = dict(
                    y=alt.Y('count(*):Q',
                            axis=alt.Axis(grid=False, labels=False),
                            stack='center',
                            title=''),
                    x=alt.X('{x}:Q'.format(x=x), bin=alt.Bin(maxbins=100)),
                    row='{y}:N'.format(y=y),
                    color='{y}:N'.format(y=y)
        )
    chart = alt.Chart(data).mark_area().encode(**kwargs)
    return chart 
Example #2
Source File: core.py    From starborn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def jointplot(x, y, data, kind='scatter', hue=None, xlim=None, ylim=None):
    if xlim is None:
        xlim = get_limit_tuple(data[x])
    if ylim is None:
        ylim = get_limit_tuple(data[y])
    xscale = alt.Scale(domain=xlim)
    yscale = alt.Scale(domain=ylim)
 
    points = scatterplot(x, y, data, hue=hue, xlim=xlim, ylim=ylim)

    area_args = {'opacity': .3, 'interpolate': 'step'}

    blank_axis = alt.Axis(title='')

    top_hist = alt.Chart(data).mark_area(**area_args).encode(
        alt.X('{x}:Q'.format(x=x),
              # when using bins, the axis scale is set through
              # the bin extent, so we do not specify the scale here
              # (which would be ignored anyway)
              bin=alt.Bin(maxbins=20, extent=xscale.domain),
              stack=None,
              axis=blank_axis,
             ),
        alt.Y('count()', stack=None, axis=blank_axis),
        alt.Color('{hue}:N'.format(hue=hue)),
    ).properties(height=60)

    right_hist = alt.Chart(data).mark_area(**area_args).encode(
        alt.Y('{y}:Q'.format(y=y),
              bin=alt.Bin(maxbins=20, extent=yscale.domain),
              stack=None,
              axis=blank_axis,
             ),
        alt.X('count()', stack=None, axis=blank_axis),
        alt.Color('{hue}:N'.format(hue=hue)),
    ).properties(width=60)

    return top_hist & (points | right_hist) 
Example #3
Source File: core.py    From starborn with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def boxplot_vertical(x=None, y=None, hue=None, data=None, order=None):

    # orientation_mapper = {'v': {'x': 'x', 'y': 'y'},
    #                       'h': {'x': 'y', 'y': 'x'}}

    # Define aggregate fields
    lower_box = 'q1({value}):Q'.format(value=y)
    lower_whisker = 'min({value}):Q'.format(value=y)
    upper_box = 'q3({value}):Q'.format(value=y)
    upper_whisker = 'max({value}):Q'.format(value=y)
    
    kwargs = {'x': '{x}:O'.format(x=x)}

    if hue is not None:
        kwargs['color'] = '{hue}:N'.format(hue=hue)
        # Swap x for column
        column, kwargs['x'] = kwargs['x'], '{hue}:N'.format(hue=hue)

    base = alt.Chart().encode(
        **kwargs
    )

    # Compose each layer individually
    lower_whisker = base.mark_rule().encode(
        y=alt.Y(lower_whisker, axis=alt.Axis(title=y)),
        y2=lower_box,
    )
    
    middle_bar_kwargs = dict(
        y=lower_box,
        y2=upper_box,
    )
    if hue is None:
        middle_bar_kwargs['color'] = 'year:O'

    middle_bar = base.mark_bar(size=10.0).encode(**middle_bar_kwargs)

    upper_whisker = base.mark_rule().encode(
        y=upper_whisker,
        y2=upper_box,
    )
    
    middle_tick = base.mark_tick(
        color='white',
        size=10.0
    ).encode(
        y='median({value}):Q'.format(value=y),
    )
    
    chart = (lower_whisker + upper_whisker + middle_bar + middle_tick)

    if hue is None:
        chart.data = data
        return chart
    else:
        return chart.facet(column=column, data=data)