Search code examples
graphplotlyplotly-python

log in plotly graph object is not working with range


I'm trying to plot top 10 rated workers but the ratings are so similar and I want to make a visible difference but when I try to use log with range it's doesn't log within that range

import plotly.graph_objects as go

x = [f'Worker {i}' for i in range(1, 11)]
y = top_10['rating'].values #[27, 18, 12, 6, 27, 18, 12, 6, 10, 10]
p = y

fig = go.Figure(
        go.Bar(
            x=[5]*10,
            y=x,
            text=p,
            textposition="inside",
            textfont=dict(color="white"),
            orientation="h",
            marker_color="grey",
        ),
        layout_xaxis_range=[1,5]
)

fig.add_trace(
    go.Bar(
        x=y,
        y=x,
        orientation="h",
    )
)
fig.update_layout(
    title='Top 10 Workers', 
    barmode="overlay", 
    showlegend=False, 
    template="presentation",
    # xaxis_type="log",
    )
fig.update_yaxes(
    tickmode="array",
    categoryorder="total ascending",
    tickvals=x,
    ticktext=x,
    ticklabelposition="inside",
    tickfont=dict(color="white"),
)
fig.update_xaxes(
    range=[0, 5], 
    # visible=False, 
    type='log')
# fig.update_xaxes(type="log")
fig.show()

I want an x-axis looking like this: enter image description here

but this is what I get: enter image description here


Solution

  • The values on the x axis are in log, so your range has to be in log as well. Currently you are setting the range [10⁰, 10⁵] -> [1,10000], so your range should be something like range=[np.log(0.1),np.log(5)].

    Note : log(0) evaluates to -inf

    Take a look at the official documentation on using log axis with graph objects