Search code examples
legendsubplotyellowbrick

Issues when using subplots with yellowbrick and losing legend and titles


I'm having issues when putting multiple yellowbrick charts into a subplot arrangement. The title and legend only show for the last chart. I've tried multiple ways to write the code but can't get all of them to show the legends and titles. I'm sure its straightforward to get to work.

Here's a piece of code:

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


Solution

  • @chris-mangum sorry that you have struggled with this. Besides show we have another method called finalize In this case, finalize is better than show -- show calls finalize and then either show or savefig which concludes the figure, so in a multi-axes plot like you have, you don't want to call 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()