Search code examples
pythonpandasplotlystreamlit

Y-axis ticks not displayed correctly in Plotly bar chart in Streamlit app


I have a function free_vs_paid(df_clean) that generates a Plotly bar chart comparing the number of free and paid apps across different categories. In Jupyter Notebook, the chart displays the y-axis ticks correctly (1, 10, 100,1000) it shows in below. I know that problem cames from yaxis=dict(type='log')). How can I set my yaxis more professionally.

enter image description here

However. when I integrate the same function into my Streamlit app, the y-axis ticks are displayed as intermediate numbers which are shown in the image.

enter image description here

Here's the relevant part of my code:

import pandas as pd
import plotly.express as px

def free_vs_paid(df_clean):
    df_free_vs_paid = df_clean.groupby(["Category","Type"], as_index=False).agg({"App": pd.Series.count})
    df_free_vs_paid.sort_values("App", ascending=False, inplace=True)

    freevsPaid_bar = px.bar(df_free_vs_paid,
                   x="Category",
                   y="App",
                   color="Type",
                   title="Free vs Paid Apps by Category",
                   barmode="group")

    freevsPaid_bar .update_layout(xaxis_title='Category',
                        yaxis_title='Number of Apps',
                        xaxis={'categoryorder':'total descending'},
                        yaxis=dict(type='log'))

    return freevsPaid_bar 

How can I ensure that the y-axis ticks display correctly in my Streamlit app, as they do in Jupyter Notebook?

Updated

I have solved that problem with change yaxis.

   yaxis={'type': 'log',
         'tickvals': [1, 10, 100, 1000],
         ticktext': ['1', '10', '100', '1000']})

However is there any better solution for that?

Thank you in advance for your help!


Solution

  • You can do that by setting the axis dtick accordingly.

    dtick - Sets the step in-between ticks on this axis. Use with tick0. Must be a positive number, or special strings available to "log" and "date" axes. If the axis type is "log", then ticks are set every 10^(n"dtick) where n is the tick number. For example, to set a tick mark at 1, 10, 100, 1000, ... set dtick to 1.

    fig.update_yaxes(dtick=1)