'How to create initial zoom on Plotly express bar chart? (categorical)

I have a categorical axis in plotly express, it contains around 100 different categories. What I want to do is that when I create it, it will be zoomed only on the first 10 categories and then the user will be able to zoom out using the plotly controls. how can I do that?



Solution 1:[1]

A possible solution is using rangeslider and range on you xaxis

Have generated a sample data set where x is categorical to demonstrate this.

import plotly.express as px
import numpy as np
import pandas as pd

df = pd.DataFrame(
    {
        "category": [
            chr(r % 26 + 65) if r // 26 == 0 else chr(r // 26 + 64) + chr(r % 26 + 65)
            for r in range(100)
        ],
        "value": np.sin(np.linspace(-2 * np.pi, 2 * np.pi, 100)),
    }
)


px.bar(df, x="category", y="value").update_layout(
    xaxis_rangeslider_visible=True, xaxis_range=[0, 10]
)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rob Raymond