我在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()

问题是这些值的绘制顺序有误.我想要的是有3个类别['25 kWh', '50 kWh', '80 kWh'],对于每个类别,应该绘制3个方法methods = ['Optimal Control', 'PSC', 'PSC-ANN']的值.输入数据例如improvement_25_kWh = [13.3, 4.1, 5.4]总是具有按方法的顺序的值[‘最优控制’、‘PSC’、‘PSC-ANN’].我怎么能做到这一点?

推荐答案

在您的应用程序中,您正在为每种方法(最优控制、PSC、PSC-ANN)并排绘制不同千瓦时类别(25千瓦时、50千瓦时、80千瓦时)的条形图.每个类别(25千瓦时、50千瓦时、80千瓦时)是主要的划分,在每个类别中,每种方法都有栏.

修改后的代码将主要分组从kWh类别更改为方法.

  1. 数据 struct :数据在字典中重新组织(改进),将方法作为键,将其跨类别的相应改进作为值.这允许对方法而不是类别进行迭代.

  2. 条形图绘制:绘制循环遍历方法.对于每种方法,它都会绘制一组横跨不同千瓦时类别的条形图.这导致每种方法(最优控制、PSC、PSC-ANN)成为主要分组,它们在不同千瓦时类别上的各自性能并排显示.

这里的主要更改和帮助在于您的数据 struct .这允许您设计和绘制数据,如上所述.

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()

Updated Image of Improvement Chart

Python相关问答推荐

七段显示不完整

一切似乎都可以自己工作,但当我把它放在一起时,它会抛出RegexMatch错误

尽管进程输出错误消息,subProcess.check_call的CalledProcess错误.stderr为无

Python:MultiIndex Dataframe到类似json的字典列表

使用matplotlib pcolormesh,如何停止从一行绘制的磁贴连接到上下行?

如何使用Python中的clinicalTrials.gov API获取完整结果?

有条件地采样我的大型DF的最有效方法

如何从FDaGrid实例中删除某些函数?

对Numpy函数进行载体化

比较2 PD.数组的令人惊讶的结果

将jit与numpy linSpace函数一起使用时出错

Django mysql图标不适用于小 case

Python中的嵌套Ruby哈希

在Wayland上使用setCellWidget时,try 编辑QTable Widget中的单元格时,PyQt 6崩溃

NP.round解算数据后NP.unique

无法使用requests或Selenium抓取一个href链接

dask无groupby(ddf. agg([min,max])?''''

如何检测鼠标/键盘的空闲时间,而不是其他输入设备?

从列表中获取n个元素,其中list [i][0]== value''

ruamel.yaml dump:如何阻止map标量值被移动到一个新的缩进行?