Search code examples
pythongraphplotlylinegraphxticks

Adjust x ticks for a hour column of a dataframe in plotly


Plotted using Autoplotter in jupyter where hours are in countsplot of Timestamp Vs Temp

I have a csv file, that contains a column called hours. While plotting that in x axis for line graph in plotly , it is getting represented in the form of counts like

0K, 50K,………..250K. The frequency seems to be 1

Is there any way to adjust the ticks according to timestamp?

Thanks in advance..


Solution

    • you describe hours in range unto 250k, so have simulated the same
    • you can simply convert this to number of nanoseconds since epoch to use a timestamp axis
    • have plotted both on same figure with two x-axis to demonstrate
    import plotly.graph_objects as go
    import pandas as pd
    import numpy as np
    import plotly.express as px
    
    df = pd.DataFrame(
        {
            "hours": range(1, 250 * 10 ** 3 + 1, 100),
            "value": np.linspace(1, 10, 2500) * np.random.uniform(0.9, 1.1, 2500),
        }
    )
    px.line(df, x="hours", y="value").update_traces(name="raw", showlegend=True).add_trace(
        go.Scattergl(
            x=(df["hours"] * 60 * 60 * 10 ** 9).astype("datetime64[ns]"),
            y=df["value"] + 2,
            name="epoch",
            xaxis="x2",
        )
    ).update_layout(xaxis2={"anchor": "y", "domain": [0.0, 1.0], "side": "top"})
    
    

    enter image description here