0

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?

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

Set the x range but treat each category as an integer starting at 0.

import plotly.express as px

df = px.data.iris()
fig = px.bar(df,
    x = 'species',
    y = 'sepal_length',
    range_x = [-0.5, 1.5],
    barmode = 'overlay',
                    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.