需要使用两个y轴重新定位多个条形系列的图例

时间:2019-09-17 15:53:48

标签: matplotlib position legend

这些是我项目的结果 我用字典来存储每个模型的结果 结果包括每个模型的评估指标值

我需要为使用两个y轴的多个条形图系列重新定位图例 现在,图例栏已隐藏。

Results = {'GBR' : {'MSE' : MSE_GBR, 'R2' : R2_GBR, 'Adj-R2' : Adj_R2_GBR},
      'RFR' : {'MSE': MSE_RFR,'R2': R2_RFR, 'Adj-R2' : Adj_R2_RFR}, 
      'DTreeRegres' : {'MSE': MSE_DTreeRegres, 'R2' : R2_DTreeRegres, 'Adj-R2' : Adj_R2_DTreeRegres},
      'LassoPolyallX' : {'MSE' : Results_PenlzPolyN["MSE"][1], 'R2' : Results_PenlzPolyN["R-sq"][1],'Adj-R2' : Results_PenlzPolyN["Adj_R_sq"][1]},
      'LassoAllX' : {'MSE' : MSE_LassoAllX, 'R2' : R2_LassoAllX, 'Adj-R2' : Adj_R_sq_LassoAllX},
      'LassoSubX' : {'MSE' : MSE_LassoRegressSubX, 'R2' : R2_LassoRegressSubX, 'Adj-R2' : Adj_R_sq_LassoRegressSubX},
    'PolyLinSubX' : {'MSE' : MSE_Poly2, 'R2' : R2_Poly2, 'Adj-R2' : Adj_R_sq_Poly2},
    'LinearSubX' : {'MSE' : MSE_LinearSubX, 'R2' : R2_LinearSubX, 'Adj-R2' : Adj_R2_LinearSubX} }   

# storing the individual values in a list 
ModelNames = [m[0] for m in Results.items()] 
MSE = [m[1]['MSE'] for m in Results.items()] 
R2 = [m[1]['R2'] for m in Results.items()]
Adj_R2 = [m[1]['Adj-R2'] for m in Results.items()]

# creating a dataframe to facilitate my plot
dfplot = pd.DataFrame()

dfplot['ModelNames']= ModelNames
dfplot['MSE'] =MSE
dfplot['R2'] =R2
dfplot['Adj-R2'] = Adj_R2

dfplot

dfplot.index_col =0

ax= dfplot.plot(kind= 'bar' , secondary_y= ['R2','Adj-R2'] , rot= 60)

# Set the labels for the x ticks
plt.xticks(np.arange(8), ModelNames)
# Visualizing plot only
plt.show()

enter image description here

0 个答案:

没有答案