Forum Discussion

niyati_61's avatar
niyati_61
Helper III
4 years ago

Scatter Plot: Axis not sorted correctly issue

Hello all,

 

Currently have an issue in the Python Visual: Scatter Plot Graph,

 

The data values are sorted according to the y-axis instead of the x-axis.

Basically, for our Time column, we have future time intervals before our past time intervals.

As you can see in the graph below, 15:49:00 time is after the 16:00:00 time interval. How do we fix this issue?

 

Thank You!

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi niyati_61 ,

     

    Maybe this post helps you.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    # create test data
    np.random.seed(365)
    data = {'freq': np.random.randint(20, size=(20,)), 'RN': np.random.randint(800, 900, size=(20,))}
    df = pd.DataFrame(data)
    
    # convert RN to a str type
    df.RN = df.RN.astype(str)
    
    # sort freq
    df.sort_values('freq', ascending=True, inplace=True)
    
    # plot
    plt.scatter('RN', 'freq', data=df)

     

     

    Best Regards,

    Stephen Tao

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.