0

I am new to python and with the help of Chatgpt I have created the code below.

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

# Load your data
df_diff = pd.read_csv("incremental_price_differences.csv")     # Contains *_diff columns
df_rank = pd.read_csv("Power_Prices_Ranked.csv")

techs = ["CCGT", "Hydrogen CCGT", "OCGT", "Hydrogen OCGT", "Engine", "CCS", "Elec_SMP"]

# Merge DataFrames
df = df_diff.merge(df_rank, on=["Date", "Hour"], suffixes=("_diff", "_rank"))
df = df.sort_values("Hour")

color_map = {
    "CCGT": "#1f77b4",
    "Hydrogen CCGT": "#ff7f0e",
    "OCGT": "#2ca02c",
    "Hydrogen OCGT": "#d62728",
    "Engine": "#9467bd",
    "CCS": "#8c564b",
    "Elec_SMP": "#e377c2",
}
plotted_techs = set()

fig, ax = plt.subplots(figsize=(20, 6))  # Increase figure width

# Loop hour-by-hour
for _, row in df.iterrows():
    hour = row["Hour"]
    tech_info = [(tech, row[f"{tech}_diff"], int(row[f"{tech}_rank"])) for tech in techs]
    tech_info_sorted = sorted(tech_info, key=lambda x: x[2])
    
    bottom = 0
    for tech, value, _ in tech_info_sorted:
        label = tech if tech not in plotted_techs else None
        ax.bar(hour, value, bottom=bottom, color=color_map[tech], label=label)
        bottom += value
        plotted_techs.add(tech)

ax.set_xlabel("Hour")
ax.set_ylabel("£/MWh")
ax.set_title("Power Technology Prices Per Hour")

# Show only every 50th hour label
unique_hours = sorted(df["Hour"].unique())
step = 50  # Changed from 5 to 50
ticks = [h for h in unique_hours if h % step == 0]  # Only show hours that are multiples of 50
ax.set_xticks(ticks)

ax.set_ylim(0, 1000)
ax.legend(title="Technology", bbox_to_anchor=(1.05, 1), loc="upper left")

# Rotate x-axis labels
ax.tick_params(axis='x', labelrotation=45)
plt.tight_layout()
plt.show()

The aim of the code is to take the 'incremental_price_differences' which has data formatted like the below,

enter image description here

and create a bar chart for every hour but the order of the bars should be based on the 'Power_Prices_Ranked' file, formatted like the below,

enter image description here

The problem is that there are too many x-axis labels making the graph unreadable, see the image below,

enter image description here

I have tried to edit the code to resolve this but no matter what I do the labels stay the same. The issue also occurs when I try to set the y_lim which also has no effect.

Why is my fix for the labels not working? How can I resolve the issue? Is there a better way to write my code?

Kind regards,

Joshua Dunn

3
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Commented Apr 16 at 11:31
  • 3
    One of the problems with using AI to write your code is that you end up with something that may not be exactly what you asked for. So go back to the pandas and matplotlib documentation where you are more likely to find useful information. Commented Apr 16 at 11:43
  • When you say "order of the bars", do you mean the column labels? It is unclear what you're aiming for. For one, I do not understand the need of two nested for loops? I suspect the second for loop to be the culprit. Why have you done the first three lines of pre-preocessing in the first for loop? It would be helpful to have some comments in the code. Commented Apr 16 at 15:55

0

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.