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,
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,
The problem is that there are too many x-axis labels making the graph unreadable, see the image below,
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


