Search code examples
pythonaltairvega-litevega

Inverse logarithmic scale [0,0.9,0.99,0.999]


I'm trying to plot the equivalent of a hdrhistogram, to analyse some latency data, however it seems non-trivial to do so since it requires what is essentially the inverse of a logarithmic scale.

I.e what I am trying to get is a scale that has ticks along the lines of: [0,0.9, 0.99, 0.999, 0.9999]

I'm coding this all via the Altair library for python, if that helps in any way.


Solution

  • There is no easy way to do this in Altair, because it's not supported in Vega (see the two-year-old feature request here: https://github.com/vega/vega/issues/1277)

    But you can hack around it by transforming your data, using a standard log scale, and then computing new tick labels to reflect your underlying data. It might look something like this:

    import altair as alt
    import pandas as pd
    
    df = pd.DataFrame({
        'x': range(5),
        'y': [0.0001, 0.9, 0.99, 0.999, 0.9999],
    })
    
    alt.Chart(df).transform_calculate(
      z = 1 - alt.datum.y  
    ).mark_line().encode(
      x='x:Q',
      y=alt.Y(
        'z:Q',
        scale=alt.Scale(type='log', reverse=True)),
        axis=alt.Axis(
          values=[1, 0.1, 0.01, 0.001, 0.0001, 0.00001],
          labelExpr="1 - datum.value"),
    )
    

    enter image description here