将子图与yellowbrick一起使用并丢失图例和标题时出现问题

时间:2019-04-19 05:23:03

标签: legend subplot yellowbrick

将多个Yellowbrick图表放入子图排列时,我遇到了问题。标题和图例仅显示最后一个图表。我尝试了多种方式编写代码,但无法全部显示图例和标题。我相信它很容易上手。

这是一段代码:

f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2,figsize=(14, 10))

viz = FeatureImportances(LinearRegression(), ax=ax1)
viz.fit(X_train, y_train)

viz = LearningCurve(LinearRegression(), scoring='r2',cv=10, ax=ax2)
viz.fit(X_train, y_train)

viz = ResidualsPlot(clf, ax=ax3)
viz.fit(X_train, y_train) 

viz = PredictionError(LinearRegression(), ax=ax4)
viz.fit(X_train, y_train) 
viz.score(X_test, y_test) 

viz.poof()

image of plots

1 个答案:

答案 0 :(得分:4)

@ chris-mangum很抱歉您为此感到苦恼。除了poof之外,我们还有另一种名为finalize的方法,在这种情况下,finalize优于poofpoof调用finalize,然后显示show或savefig得出结论该图,因此在像您这样的多轴绘图中,您不想调用poof。

f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2,figsize=(14, 10))

viz = FeatureImportances(LinearRegression(), ax=ax1)
viz.fit(X_train, y_train)
viz.finalize()

viz = LearningCurve(LinearRegression(), scoring='r2',cv=10, ax=ax2)
viz.fit(X_train, y_train)
viz.finalize()

viz = ResidualsPlot(clf, ax=ax3)
viz.fit(X_train, y_train) 
viz.finalize()

viz = PredictionError(LinearRegression(), ax=ax4)
viz.fit(X_train, y_train) 
viz.score(X_test, y_test) 

viz.finalize()