Python 将子批次的柱状图保存到PDF

Python 将子批次的柱状图保存到PDF,python,matplotlib,Python,Matplotlib,我想绘制两个字典的直方图:dict1={k1:v}和dict2={k2:v}其中k1==k2 结果正是我想要的,但当我尝试将其保存到pdf时,我得到一个错误“tuple”对象没有属性“savefig”。如何将此直方图保存为pdf格式 fig = plt.subplots(figsize =(40, 15)) barWidth = 0.25 EL= list(dict_graph_EL.values()) EA= list(dict_graph_EA.values()) #keys of di

我想绘制两个字典的直方图:
dict1={k1:v}和dict2={k2:v}
其中
k1==k2

结果正是我想要的,但当我尝试将其保存到pdf时,我得到一个错误
“tuple”对象没有属性“savefig”
。如何将此直方图保存为pdf格式

fig = plt.subplots(figsize =(40, 15))
barWidth = 0.25

EL= list(dict_graph_EL.values())
EA= list(dict_graph_EA.values())

#keys of dict_graph_EL == dict_graph_EA
br1 = np.arange(len(list(dict_graph_EL.keys()))) 
br2 = [x + barWidth for x in br1]

plt.bar(br1, EL, color ='blue', width = barWidth, edgecolor ='grey', label ='EL') 
plt.bar(br2, EA, color ='pink', width = barWidth, edgecolor ='grey', label ='EA') 

list_labels = list(dict_graph_EA.keys())
plt.xticks([r + barWidth for r in range(len(EL))], list_labels , rotation=60) 

plt.ylabel('Avg percentage of error on EL(blue)/EA(pink)')
plt.xlabel('Programs')

plt.legend(['Avg EL', 'Avg EA'], loc='upper left')

# Display the graph
plt.show(fig)

fig.savefig("avgELEA.pdf", bbox_inches='tight')

更换

fig = plt.subplots(figsize =(40, 15))


该函数是一个方便的包装器,可以在一行中创建一个图形和一组子地块。它返回一个元组
(图,轴)
,如果要使用对图形的引用进行保存,则需要如上所述对该元组进行解压缩。

为什么不
plt.savefig()
?@Grayrigel它不起作用。它是否给出任何错误?还是空白图?你从错误信息中理解了什么?你做过调试吗?
fig, ax = plt.subplots(figsize =(40, 15))