I’m working on a data visualization project in Jupyter notebook and keep running into this annoying warning. Every time I try to show my plots, I get an error saying matplotlib is using a non-GUI backend.
import matplotlib.pyplot as plt
%matplotlib inline
def plot_model_performance(train_data, train_labels, test_data, test_labels):
""" Creates performance charts for different model configurations """
print("Generating performance charts for various depth settings...")
# Set up the plot area
figure = plt.figure(figsize=(12, 9))
# Different sample sizes for training
sample_counts = np.rint(np.linspace(1, len(train_data), 45)).astype(int)
training_errors = np.zeros(len(sample_counts))
validation_errors = np.zeros(len(sample_counts))
# Test different depth values
for idx, tree_depth in enumerate([2, 4, 7, 12]):
for j, sample_size in enumerate(sample_counts):
# Create decision tree with specific depth
tree_model = DecisionTreeRegressor(max_depth=tree_depth)
# Train on subset of data
tree_model.fit(train_data[:sample_size], train_labels[:sample_size])
# Calculate training performance
training_errors[j] = calculate_error(train_labels[:sample_size],
tree_model.predict(train_data[:sample_size]))
# Calculate validation performance
validation_errors[j] = calculate_error(test_labels,
tree_model.predict(test_data))
# Create subplot for current depth
subplot = figure.add_subplot(2, 2, idx+1)
subplot.plot(sample_counts, validation_errors, lw=2, label='Validation Error')
subplot.plot(sample_counts, training_errors, lw=2, label='Training Error')
subplot.legend()
subplot.set_title('Tree Depth = %s'%(tree_depth))
subplot.set_xlabel('Training Sample Count')
subplot.set_ylabel('Error Rate')
subplot.set_xlim([0, len(train_data)])
# Final styling
figure.suptitle('Model Performance Analysis', fontsize=16, y=1.02)
figure.tight_layout()
figure.show()
When I execute the plot_model_performance()
function, I keep getting this warning message:
UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
The plots don’t display properly and I’m not sure how to fix this backend issue. Has anyone else encountered this problem?