Altair: Is binning with log scale allowed?

Created on 9 Aug 2018  Â·  4Comments  Â·  Source: altair-viz/altair

I would like to bin values along one or both axes while also plotting on log scale. Is this possible? It seems like when I request both binning and log scale axis, the log scale is disabled:

import altair as alt
import numpy as np

data = pd.DataFrame( {col: np.random.random_sample(50) for col in 'xy'})
data += 1e-4

chart = alt.Chart( data ).mark_circle().encode(y='y')

naturalBin = chart.encode(
     x = alt.X('x', bin=True, axis=alt.Axis(title = 'Binning, natural scale')))

log = chart.encode(
    x = alt.X('x', bin = False, scale = alt.Scale(type='log'), axis=alt.Axis(title = 'No Binning, log scale')))

logBin = chart.encode(
    x = alt.X('x', bin = True, scale = alt.Scale(type='log'), axis=alt.Axis(title = 'Binned, log scale')))

naturalBin & log &  logBin

visualization 1

question

Most helpful comment

This would be nice to have - any updates on the altair side? Looks like it's not yet supported on the vega side.

All 4 comments

Hi @breadbaron – I don't think log-spaced bins are supported in Vega-Lite. In particular, it's not clear to me what the right binning would even be...

One way you could proceed is to precompute the log and bin that:

alt.Chart(data).transform_calculate(
    logx = 'log(datum.x)/log(10)'
).mark_circle().encode(
    alt.X('logx:Q', bin=True, title='log_10(x)'),
    y='y:Q'
)

visualization 35

This is actually doable with some extra work.

See vega/vega-lite#4795 https://vega.github.io/vega-lite/examples/histogram_log.html for an example in Vega-Lite.
We may also improve this in the future -- see https://github.com/vega/vega-lite/issues/4792.

This would be nice to have - any updates on the altair side? Looks like it's not yet supported on the vega side.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HalukaMB picture HalukaMB  Â·  3Comments

nielsmde picture nielsmde  Â·  4Comments

bmcfee picture bmcfee  Â·  3Comments

floringogianu picture floringogianu  Â·  3Comments

firasm picture firasm  Â·  3Comments