title: Any file (image, etc.) is saving but showing 0 KB only. What's wrong with my code?
body: <p>I'm trying to save my confusion matrix plot with different names for each model in a loop. But the extracted file were 0 KB and also had no extension for the file. And when I want to save my model as pickle, it also 0KB. But, my plot was showed right. Here's example of the plot in python notebook:</p>
  <p><a href="https://i.stack.imgur.com/9R973.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9R973.png" alt="example of plot in python notebook" /></a></p>
  <p>Here's the example of the saved file:</p>
  <p><a href="https://i.stack.imgur.com/4Zf51.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4Zf51.png" alt="example output file" /></a></p>
  <p>I already try to change the device to run code, add plt.clf() in beginning, put .show() not before saving it, and it still produce the same error. Now, I had no idea how to fix it.</p>
  <p>Can anyone please help me with this problem? Thanks.</p>
  <pre><code>def gbc_gscv_modelling(data, features, grid, title):
  y = data.label
  X = data[features]
  
  x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=.2, random_state=18)
  
  gbc_cv = GridSearchCV(estimator=gbc(random_state=42), param_grid=grid, cv=5, n_jobs=15, verbose=4)
  
  gbc_cv.fit(x_train, y_train)
  
  print(gbc_cv.best_params_)
  
  y_pred = gbc_cv.best_estimator_.predict(x_test)
  print(f&quot;Accuracy: {accuracy_score(y_test, y_pred):.3f}&quot;)
  print(f&quot;Precision: {precision_score(y_test, y_pred, average='macro'):.3f}&quot;)
  print(f&quot;Recall: {recall_score(y_test, y_pred, average='macro'):.3f}&quot;)
  plt.clf()
  cm = plot_confusion_matrix(gbc_cv.best_estimator_, X=x_test, y_true=y_test, labels=gbc_cv.classes_, cmap='Blues')
  cm.figure_.suptitle(title)
  cm.figure_.savefig(title+'.png')
  cm.figure_.show()
  
  print(classification_report(y_test, y_pred))
  
  report = classification_report(y_test, y_pred, output_dict=True)
  accuracy = accuracy_score(y_test, y_pred)
  avg_precision = precision_score(y_test, y_pred, average='macro')
  avg_recall = recall_score(y_test, y_pred, average='macro')
  precision_sit = report.get(&quot;sit&quot;).get(&quot;precision&quot;)
  precision_jog = report.get(&quot;jog&quot;).get(&quot;precision&quot;)
  precision_dws = report.get(&quot;dws&quot;).get(&quot;precision&quot;)
  precision_ups = report.get(&quot;ups&quot;).get(&quot;precision&quot;)
  precision_std = report.get(&quot;std&quot;).get(&quot;precision&quot;)
  precision_wlk = report.get(&quot;wlk&quot;).get(&quot;precision&quot;)
  recall_sit = report.get(&quot;sit&quot;).get(&quot;recall&quot;)
  recall_jog = report.get(&quot;jog&quot;).get(&quot;recall&quot;)
  recall_dws = report.get(&quot;dws&quot;).get(&quot;recall&quot;)
  recall_ups = report.get(&quot;ups&quot;).get(&quot;recall&quot;)
  recall_std = report.get(&quot;std&quot;).get(&quot;recall&quot;)
  recall_wlk = report.get(&quot;wlk&quot;).get(&quot;recall&quot;)
  
  df_result = pd.DataFrame([accuracy, avg_precision, avg_recall, precision_sit, precision_jog, precision_dws, \
         precision_ups, precision_std, precision_wlk, recall_sit, recall_jog, \
         recall_dws, recall_ups, recall_std, recall_wlk])
  return df_result
  </code></pre>
  <p>I don't know why this chunk of code not functioning properly on my code...</p>
  <pre><code>...
  cm.figure_.savefig(title+'.png')
  ...
  </code></pre>