0

I am working on plotting time-series data stored in a Pandas DataFrame using Matplotlib. The initial attempt resulted in a plot with correct data but without proper time axis.

The index in the DataFrame should represent a certain number of hours based on the number of epochs. The original code is as follows:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

headers3 = ['Zero', 'Episodes', 'Steps','Generator One', 'Generator Two', 'Load', 'Renewable','Battery Power', 'Battery Soc']

NameTime2 = NameTime1 
Name3 = 'Grid-' + NameTime2 +'-Power-balance-lower' + '.csv'

columns8 = ['Episodes', 'Load', 'Generator One', 'Generator Two','Renewable', 'Battery Power', 'Battery Soc']

df = pd.read_csv(Name3, names=headers3, usecols=columns8)

episodesDisplay11 = episodesDisplay1
episodesDisplay21 = episodesDisplay2

df = df.loc[(df["Episodes"] >= episodesDisplay11) & (df["Episodes"] < episodesDisplay21)]

df = df.iloc[:, 1:]

df['index'] = df.index

df['Battery_Discharging'] = df['Battery Power'].clip(lower=0)
df['Battery_Charging'] = df['Battery Power'].clip(upper=0)

ax = plt.gca()

number_of_hours = (episodesDisplay21 - episodesDisplay11) * 6

df.set_index('index').plot(y='Battery Soc', ax=ax, label='Battery Soc', color='green', linestyle = 'dashed')
df.set_index('index').plot(y='Load', ax=ax, label='Load', color='black', linewidth=3)
df.set_index('index').plot.area(y=['Renewable', 'Generator One', 'Generator Two', 'Battery_Discharging', 'Battery_Charging'], stacked=True, alpha=0.7, ax=ax)

label=['Renewable','Generator One', 'Generator Two', 'Battery_Discharging','Battery_Charging'], stacked=True, alpha=0.7, ax=ax)

plt.title('Power Balance Lower Level')

plt.legend(fontsize=14)

# Save the plot before calling plt.show()
plt.savefig('Power_Balance-Lower.jpg')

# Show the plot
plt.show() 

Here is the image:

Correct data but no axis in hours

However, when attempting to set the correct timestamp, the plot shows the correct axis but no data. The modified code is:

    columns7 = ['Episodes', 'Load', 'Generator One', 'Generator Two','Renewable', 'Battery Power', 'Battery Soc']
    # columns7 = ['Episodes', 'Pl', 'Pm1', 'Pm2', 'Pr', 'eb','pb']
    #columns62 = ['Episodes',]
    
    df = pd.read_csv(Name2, names=headers2, usecols=columns7)
    
    episodesDisplay11 = episodesDisplay1
    episodesDisplay21 = episodesDisplay2
    
    df = df.loc[(df["Episodes"] >= episodesDisplay11) & (df["Episodes"] < episodesDisplay21)]
    
    df = df.iloc[:, 1:]
    
    df['index'] = df.index
    
    df['Battery_Discharging'] = df['Battery Power'].clip(lower=0)
    df['Battery_Charging'] = df['Battery Power'].clip(upper=0)
    
    number_of_hours = (episodesDisplay21 - episodesDisplay11)*6
    
    ax = plt.gca()
    
    # Set the x-axis range
    plt.xlim([0, number_of_hours])
    
    df.set_index('index').plot(y='Battery Soc', ax=ax, label='Battery Soc', color='green', linestyle = 'dashed')
    df.set_index('index').plot(y='Load', ax=ax, label='Load', color='black', linewidth=3)
    df.set_index('index').plot.area(y=['Renewable', 'Generator One', 'Generator Two', 'Battery_Discharging', 'Battery_Charging'], stacked=True, alpha=0.7, ax=ax)
    
    label=['Renewable', 'Generator One', 'Generator Two', 'Battery_Discharging','Battery_Charging'], stacked=True, alpha=0.7, ax=ax)
    
    plt.title('Power Balance Upper Level', fontsize=16)
    plt.xlabel("Time [hours]", fontsize=14)
    plt.xlabel("Power [p.u.]", fontsize=14)
    plt.legend(fontsize=14)
    plt.savefig('Power_Balance-Upper.jpg')
    plt.show()

Plot with proper timestamp but no showing data

Any suggestions on how to resolve the issue and display the correct data with the appropriate time axis are appreciated.

The data frame of the first two episodes for this example is:

    0,0.0,60.0,0.35003073846817784,0.31162205671810755,0.7669085438363514,0.10518409263557386,2.953193245502916e-15,0.9999999999999992
    0,0.0,90.0,0.31621088421241966,0.26884915268313436,0.691850567123612,0.10674058815807792,-8e-323,2e-323
    0,0.0,120.0,0.2854344661649568,0.26801047877633466,0.6551620964214824,0.10168177373370187,-8e-323,2e-323
    0,0.0,150.0,0.31867475272234086,0.3206352985002537,0.748030812091372,0.10865860982935448,-8.535038633286319e-212,2.0089783917530665e-212
    0,0.0,180.0,0.36817006909125416,0.34576618813786425,0.8006504919770473,0.0889762599292061,0.0023414619266986848,0.015609746177991305
    0,0.0,210.0,0.3075188932545438,0.280152463687341,0.6678957712517684,0.0801772342851853,-8e-323,2e-323
    0,0.0,240.0,0.33984199239821056,0.322208241363236,0.7389483458360644,0.07683051974578553,-8e-323,2e-323
    0,0.0,270.0,0.27742200526510036,0.2550396713906429,0.6030450975177633,0.07055140318493634,-8e-323,2e-323
    0,0.0,300.0,0.301777588034882,0.27882456948865625,0.6430246237361917,0.062375620806306244,-8e-323,2e-323
    0,0.0,330.0,0.2983643882845209,0.2763257794953646,0.6282243535493954,0.05348365417776617,-8e-323,2e-323
    0,1.0,30.0,0.24481879503613876,0.34343053729408357,0.7044679607704338,0.11615735259254727,-8e-323,2e-323
    0,1.0,60.0,0.33814704871788287,0.32349943078198695,0.7669085438363514,0.10518409263557386,-8e-323,2e-323
    0,1.0,90.0,0.303776342852677,0.2812763643102889,0.691850567123612,0.10674058815807792,-8e-323,2e-323
    0,1.0,120.0,0.28394437014220275,0.269492315045594,0.6551620964214824,0.10168177373370187,-8e-323,2e-323
    0,1.0,150.0,0.3099516618156297,0.32935251175367347,0.748030812091372,0.10865860982935448,-8e-323,2e-323
    0,1.0,180.0,0.3679523575768759,0.3436278212537128,0.8006504919770473,0.0889762599292061,-8e-323,2e-323
    0,1.0,210.0,0.30115049509468955,0.28651067046598344,0.6678957712517684,0.0801772342851853,-8e-323,2e-323
    0,1.0,240.0,0.33506621979189355,0.3269767817108871,0.7389483458360644,0.07683051974578553,-8e-323,2e-323
    0,1.0,270.0,0.27355676189012096,0.2588932593164111,0.6030450975177633,0.07055140318493634,-8e-323,2e-323
    0,1.0,300.0,0.284365127988658,0.296229045100913,0.6430246237361917,0.062375620806306244,-8e-323,2e-323
3
  • 2
    Maybe provide 10-15 lines of csv file so that people can run on their on machine Commented Jan 16, 2024 at 17:04
  • Did you try your second plot without plt.xlim([0, number_of_hours])? That call supposes the index is numerical, not date-time. Commented Jan 16, 2024 at 20:15
  • Could you add the column names to the CSV? Also what are the variables episode1, 2, 11, and 21? Commented Jan 16, 2024 at 20:17

1 Answer 1

0

If you are trying to plot the series (columns in your dataframe) with indexes, then the x-axis will be the index of the series. So, you can fix the issue with the x-axis by overwriting the index as:

    df["number_of_hours"] = (episodesDisplay21 - episodesDisplay11)*6
    ax = plt.gca()
    
    df.set_index('number_of_hours').plot(y='Battery Soc', ax=ax, label='Battery Soc', color='green', linestyle = 'dashed')
    df.set_index('number_of_hours').plot(y='Load', ax=ax, label='Load', color='black', linewidth=3)
    df.set_index('number_of_hours').plot.area(y=['Renewable', 'Generator One', 'Generator Two', 'Battery_Discharging', 'Battery_Charging'], stacked=True, alpha=0.7, ax=ax)
    
    label=['Renewable', 'Generator One', 'Generator Two', 'Battery_Discharging','Battery_Charging'], stacked=True, alpha=0.7, ax=ax)
    
    plt.title('Power Balance Upper Level', fontsize=16)
    plt.xlabel("Time [hours]", fontsize=14)
    plt.xlabel("Power [p.u.]", fontsize=14)
    plt.legend(fontsize=14)
    plt.savefig('Power_Balance-Upper.jpg')
    plt.show()

This would set the index of the dataframe as the number of hours, which you have computed yourself, and set it as the x-axis in the plots.

Sign up to request clarification or add additional context in comments.

Comments

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.