0

I want to plot multiple graphs using python... but I get some issues with x-axis values that are not ordered in a correct way.

I used this code:

df=pd.read_csv('C:/Userscsv_grouped.csv',sep=';')
for col in df.columns:
    if col != 'JOUR(PSDATE)' and col != 'GNODEB':
        plt.style.use('seaborn')
        pivoted_df=df.pivot(index='JOUR(PSDATE)',columns='GNODEB', values='RACH_MSG2_SR')
        fig, ax = plt.subplots()
        pivoted_df.plot(ax=ax, kind='line', legend=True)

        ax.set_xlabel('JOUR(PSDATE)')
        ax.set_ylabel(col)
        ax.set_title(f'Evolution of {col} by GNODEB')
        plt.legend(title='GNODEB', bbox_to_anchor=(1.05, 1), loc='upper left')
        plt.show()
        break

Here is the result I get: enter image description here

1

1 Answer 1

0

Right now your index is of type string - pandas doesn't know that it should be interpreted as dates. Try explicit conversion with pd.to_datetime:

df=pd.read_csv('C:/Userscsv_grouped.csv',sep=';')
for col in df.columns:
    if col != 'JOUR(PSDATE)' and col != 'GNODEB':
        plt.style.use('seaborn')
        pivoted_df=df.pivot(index='JOUR(PSDATE)',columns='GNODEB', values='RACH_MSG2_SR')
        fig, ax = plt.subplots()
        pivoted_df.index = pd.to_datetime(pivoted_df.index, format="DD/MM/YYYY")
        pivoted_df.plot(ax=ax, kind='line', legend=True)

        ax.set_xlabel('JOUR(PSDATE)')
        ax.set_ylabel(col)
        ax.set_title(f'Evolution of {col} by GNODEB')
        plt.legend(title='GNODEB', bbox_to_anchor=(1.05, 1), loc='upper left')
        plt.show()
        break
Sign up to request clarification or add additional context in comments.

1 Comment

Hello Maria ...... I tried your solution and firstly get an errour of time data '01/02/2023' does not match format 'DD/MM/YYYY' then tried this format : format='%d/%m/%Y' and it worked . Thank You alooooot

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.