Issue
I have the following code
import matplotlib.pyplot as plt
from datetime import datetime
co2_prices = [100, 60]
electricity_prices = [-2.6, 157]
co2_timestamps = ["March 2023", "February 2024"]
electricity_timestamps = ["24.09.2023 14:00", "24.09.2023 19:00"]
co2_timestamps = [datetime.strptime(date, "%B %Y") for date in co2_timestamps]
electricity_timestamps = [datetime.strptime(date, "%d.%m.%Y %H:%M") for date in electricity_timestamps]
fig, ax = plt.subplots()
bar_width = 0.35
co2_positions = range(len(co2_prices))
electricity_positions = [pos + bar_width for pos in co2_positions]
# Plot bars for CO2 prices
ax.bar(co2_positions, co2_prices, bar_width, label='CO2 Prices (€/T)')
ax.bar(electricity_positions, electricity_prices, bar_width, label='Electricity Prices (€/MWh)')
all_positions = list(co2_positions) + list(electricity_positions)
all_labels = ["March 2023", "February 2024", "24.09.2023 14:00", "24.09.2023 19:00"]
ax.set_xticks(all_positions)
ax.set_xticklabels(all_labels, rotation=45, ha="right")
ax.set_xlabel('Timeline')
ax.set_ylabel('Prices')
ax.set_title('CO2 and Electricity Prices')
ax.legend()
plt.show()
to display a barchart with 4 entries. The entries for the
co2_prices = [100, 60]
should be next to each other with the corresponding labels
co2_timestamps = ["March 2023", "February 2024"]
and the entries for the
electricity_prices = [-2.6, 157]
should be next to each other with the corresponding labels
electricity_timestamps = ["24.09.2023 14:00", "24.09.2023 19:00"]
.
In the current code the bars for the co2_prices
are not next to each other, and I tried several solution to change it, but it did not work.
Solution
When you write the bar position you are putting the each electricity bar just after the co2 bar because you are distancing them only one bar width. Since the position for the co2 are 0 and 1, you should put the electricity in 2 and 3 with:
electricity_positions = [pos + 2 for pos in co2_positions]
Moreover if you want the bar for the same categories (electricity or co2) touching the bar width should be 1.
Complete working example:
import matplotlib.pyplot as plt
from datetime import datetime
co2_prices = [100, 60]
electricity_prices = [-2.6, 157]
co2_timestamps = ["March 2023", "February 2024"]
electricity_timestamps = ["24.09.2023 14:00", "24.09.2023 19:00"]
co2_timestamps = [datetime.strptime(date, "%B %Y") for date in co2_timestamps]
electricity_timestamps = [datetime.strptime(date, "%d.%m.%Y %H:%M") for date in electricity_timestamps]
fig, ax = plt.subplots()
bar_width = 0.8
tras = 3
co2_positions = range(len(co2_prices))
electricity_positions = [pos + tras for pos in co2_positions]
# Plot bars for CO2 prices
ax.bar(co2_positions, co2_prices, bar_width, label='CO2 Prices (€/T)')
ax.bar(electricity_positions, electricity_prices, bar_width, label='Electricity Prices (€/MWh)')
all_positions = list(co2_positions) + list(electricity_positions)
all_labels = ["March 2023", "February 2024", "24.09.2023 14:00", "24.09.2023 19:00"]
ax.set_xticks(all_positions)
ax.set_xticklabels(all_labels, rotation=45, ha="right")
ax.set_xlabel('Timeline')
ax.set_ylabel('Prices')
ax.set_title('CO2 and Electricity Prices')
ax.set_ylim([-20, 160]) # y axis limits
ax.legend()
ax.grid(alpha=0.5) # grid, alpha is the opacity of the lines
plt.tight_layout() # tight layout, allow for better visualization in the window
plt.show()
The tras
variable is the translation between the first box of the co2 and the first of the electricity and the same for the respective second box.
I added both the limits for the y axis (starting from -20 so you see also the negative values), the grid which I prefer for readibility and the tight layout because the plot was not fitting in the window properly.
Answered By - bona_rivers
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.