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

Maria K
  • 1,491
  • 1
  • 3
  • 14
Rach
  • 1
  • 3
  • [Related/Duplicate](https://stackoverflow.com/questions/47642201/how-can-i-sort-dataframe-by-date-in-python) – rochard4u Jun 25 '23 at 20:34

2 Answers2

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
Maria K
  • 1,491
  • 1
  • 3
  • 14
  • 1
    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 – Rach Jun 25 '23 at 21:11
-3

It seems like the issue you're facing is related to the ordering of x-axis values in your graphs. By looking at your code, it appears that you are pivoting the DataFrame for each column separately within the loop. This may be causing the x-axis values to be sorted differently for each graph. To ensure consistent ordering of x-axis values across all graphs, you should pivot the DataFrame outside the loop and then iterate over the columns to create individual plots.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('C:/Users/csv_grouped.csv', sep=';')
pivoted_df = df.pivot(index='JOUR(PSDATE)', columns='GNODEB', values='RACH_MSG2_SR')

plt.style.use('seaborn')

for col in pivoted_df.columns:
    fig, ax = plt.subplots()
    pivoted_df[col].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()