PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, June 27, 2022

[FIXED] How to Tidy Up x-axes label in px express?

 June 27, 2022     dashboard, graph, hyphen, plotly, python     No comments   

Issue

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


Solution

  • 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



Answered By - Rob Raymond
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing