0

Im trying to tidy up my x-axes label from this :

Before

to this :

After

is there any parameter or atributte in px express could make it like that ?

Thank you

  • Does this answer your question? [Matplotlib showing x-tick labels overlapping](https://stackoverflow.com/questions/26700598/matplotlib-showing-x-tick-labels-overlapping) – Abhyuday Vaish Apr 11 '22 at 07:35
  • This might help as well : https://stackoverflow.com/questions/50033189/matplotlib-x-axis-overlap – Abhyuday Vaish Apr 11 '22 at 07:36
  • 1
    I believe that the graphed code and data will increase the chances of getting answers. As it stands now, we have to prepare the data, write the code, and if it is slightly different from what we intended, we have to exchange comments and rewrite the code. – r-beginners Apr 11 '22 at 10:00

1 Answers1

1
  • have simulated your data
  • taken approach that every second label is shown. Then to have all labels, need two x-axes. Clearly if two axes are used, two traces are required
import numpy as np
import pandas as pd
import plotly.express as px

regions = [
    "North Sumatra",
    "West Sumatra",
    "South Sumatra",
    "West Java, Central Java",
    "Kalimantan",
    "East Java",
    "Sulawesi",
    "Maluku",
]

df = pd.DataFrame({"region": regions, "value": np.random.randint(1, 20, len(regions))})

fig = px.bar(df, x="region", y="value")
# simulate a second trace for second xaxis
fig.add_traces(
    px.bar(df, x="region", y=np.zeros(len(regions))).update_traces(xaxis="x2").data
)
# configure labels against two axis
fig.update_layout(
    xaxis={
        "tickangle": 0,
        "anchor": "free",
        "position": 0.1,
        "tickmode": "array",
        "tickvals": df["region"].tolist()[::2],
    },
    xaxis2={
        "tickangle": 0,
        "overlaying": "x",
        "anchor": "free",
        "position": 0.05,
        "tickmode": "array",
        "tickvals": df["region"].tolist()[1::2],
    },
    yaxis={"domain": [0.1, 1]},
)

enter image description here

Rob Raymond
  • 23,623
  • 2
  • 11
  • 24