Search code examples
pythonrplotlybokeh

How to make a section of the axis log scale and other section to be linear scale to better present the data


I would like to represent my data where values go from 0.00001 to 1 where I am interested in plotting data from 0.00001 to 0.1 in a log scale and from 0.1 to 0.95 in linear scale and then again 0.95 to 1 in log scale. Most of the variations happen in the extreme and the data between 0.1 to 0.9 is almost stable but i need to take a look at it though with the rest of the data.

The x axis should be like the one shown below.

enter image description here


Solution

  • This is possible by splitting the graphing in three parts and assigning each to its own x-axis. These x-axes can be placed next to each other by specifying the domain (relative coverage of space). This is in Python, but Plotly should have the same functionality in R.

    import plotly.graph_objects as go
    
    x1 = [0.001,0.01,0.1]
    x2 = [10,100,1000]
    x3 = [1000,10000,100000]
    
    y1 = [1,2,3]
    y2 = [4,5,6]
    y3 = [7,8,9]
    
    fig = go.Figure()
    
    fig.add_trace(go.Scatter(x=x1, y=y1, xaxis='x1'))
    fig.add_trace(go.Scatter(x=x2, y=y2, xaxis='x2'))
    fig.add_trace(go.Scatter(x=x3, y=y3, xaxis='x3'))
    
    fig.update_layout(
        xaxis1=dict(
            range=[-3,0],
            side='bottom',
            overlaying='x',
            domain=[0,0.35],
            type='log'
            ),
        xaxis2=dict(
            range=[1,1000],
            side='bottom',
            overlaying='x',
            domain=[.35,.65],
            ),
        xaxis3=dict(
            range=[3,6],
            side='bottom',
            overlaying='x',
            domain=[.65,1],
            type='log'
            ),
        )
    

    x axes with different scales