Issue
I have the following barchart in Matplotlib
import matplotlib.pyplot as plt
import numpy as np
# Categories and methods
categories = ['25 kWh', '50 kWh', '80 kWh']
methods = ['Optimal Control', 'PSC', 'PSC-ANN']
#Input data
improvement_25_kWh = [13.3, 4.1, 5.4]
improvement_50_kWh = [13.8, 6.3, 4.4]
improvement_80_kWh = [14.3, 8.5, 3.8]
bar_width = 0.2
bar_positions_25_kWh = np.arange(len(categories))
bar_positions_50_kWh = bar_positions_25_kWh + bar_width
bar_positions_80_kWh = bar_positions_50_kWh + bar_width
plt.figure(figsize=(12, 7))
bars_25_kWh = plt.bar(bar_positions_25_kWh, improvement_25_kWh, color='blue', width=bar_width, label='Optimal Control')
bars_50_kWh = plt.bar(bar_positions_50_kWh, improvement_50_kWh, color='orange', width=bar_width, label='PSC')
bars_80_kWh = plt.bar(bar_positions_80_kWh, improvement_80_kWh, color='green', width=bar_width, label='PSC-ANN')
plt.xlabel('Building types', fontsize=17)
plt.ylabel('Improvement (%)', fontsize=17)
plt.xticks(bar_positions_50_kWh, categories, fontsize=15)
plt.yticks(fontsize=15)
plt.legend(fontsize=17)
plt.savefig(r'C:\Users\User1\Desktop\Result_Percentage_Improvements.png', bbox_inches='tight', dpi=200)
plt.show()
The problem is that the values are plotted in a wrong order. What I want is to have 3 categories ['25 kWh', '50 kWh', '80 kWh']
and for each category the values for the 3 methods methods = ['Optimal Control', 'PSC', 'PSC-ANN']
should be plotted. The input data e.g. improvement_25_kWh = [13.3, 4.1, 5.4]
always has the values in the order of the methods ['Optimal Control', 'PSC', 'PSC-ANN']. How can I do that?
Solution
In your appraoch, you are plotting bars for different kWh categories (25 kWh, 50 kWh, 80 kWh) side by side for each method (Optimal Control, PSC, PSC-ANN). Each category (25 kWh, 50 kWh, 80 kWh) was the primary division, and within each category, there were bars for each method.
The revised code here changes the primary grouping from kWh categories to methods.
Data Structure: The data is reorganized in a dictionary (improvements) with methods as keys and their corresponding improvements across categories as values. This allows for an iteration over methods rather than categories.
Bar Plotting: The plotting loop iterates over methods. For each method, it plots a set of bars across different kWh categories. This results in each method (Optimal Control, PSC, PSC-ANN) being the primary grouping, with their respective performance across different kWh categories displayed side by side.
The main change and help here is with your data structure. This allows you to design and plot your data as you have described above.
import matplotlib.pyplot as plt
import numpy as np
# Categories and methods
categories = ['25 kWh', '50 kWh', '80 kWh']
methods = ['Optimal Control', 'PSC', 'PSC-ANN']
# Input data organized by method
improvements = {
'Optimal Control': [13.3, 13.8, 14.3],
'PSC': [4.1, 6.3, 8.5],
'PSC-ANN': [5.4, 4.4, 3.8]
}
bar_width = 0.2
index = np.arange(len(categories))
plt.figure(figsize=(12, 7))
# Plotting bars for each method
for i, method in enumerate(methods):
plt.bar(index + i * bar_width, improvements[method], width=bar_width, label=method)
plt.xlabel('Building types', fontsize=17)
plt.ylabel('Improvement (%)', fontsize=17)
# Setting x-ticks to be in the middle of each group
plt.xticks(index + bar_width, categories, fontsize=15)
plt.yticks(fontsize=15)
plt.legend(fontsize=17)
### Legend on top instead remove above line and add this
# plt.legend(bbox_to_anchor=(0.5, 1.12), loc='upper center', ncol=3, fontsize=17)
# plt.subplots_adjust(top=0.85)
plt.savefig(r'C:\Users\user1\Desktop\Result_Percentage_Improvements.png', bbox_inches='tight', dpi=200)
plt.show()
Answered By - Markaye
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.