简体   繁体   中英

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.

For binned axes, I believe you need to set the extent of the bins to control the axis labels. In this case, setting alt.Bin(maxbins=50, extent=(0.48, 0.54))) gives you:

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM