Search code examples
pythonhistogramlayeraltairvega-lite

Python Altair How Do I Bin Histogram Data without Changing the Axis Ticks


In altair, how do I make a histogram without altering the axis ticks? When I try to bin the data, the axis ticks no longer extend past the data in the histogram. I would prefer the bins and axis ticks be calculated separately.

The below code is a simple example attempting to show limits of an in-tolerance region for a measurement to see how many measurements pass vs fail.

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



nominal = 0.500
tolerance = 0.011
limits = pd.DataFrame({'x_limits' : [nominal - tolerance, nominal + tolerance]})

data = pd.DataFrame({'x_data' : np.random.normal(0.520, 0.002, 100)})



layers = []

layers.append(alt.Chart(data)
         .mark_bar()
         .encode(x = alt.X('x_data', bin = alt.Bin(nice = False)), y = 'count()'))

layers.append(alt.Chart(limits)
         .mark_rule()
         .encode(x = 'x_limits'))

chart = alt.layer(*layers)

The output shows ticks on the X axis only under the histogram bins and they stop well before the rules at the tolerance limits.


Solution

  • I was able to solve this by plotting the histogram using mark_rect instead of mark_bar. Using transform_bin directly lets you get the start and end of each bin separately (as x and x2 in this case). Then setting zero = False in the scale makes the bounds fit the data as before.

    layers.append(alt.Chart(data)
    .mark_rect()
    .transform_bin(as_ = ['x', 'x2'], field = 'x_data')
    .encode(x = alt.X('x:Q', scale = {'zero':False}), x2 = 'x2:Q', y = 'count()'))