I'm trying to use matplotlib and numpy to plot a graph but keep running into this error:
MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later.
plt.plot(cache_sizes, hit_rates[i])
Any idea how to solve it?
Here's the code:
#!/usr/bin/env python3
import os
import subprocess
import matplotlib.pyplot as plt
import numpy as np
# cache_sizes = np.arange(0, 120, 20)
cache_sizes = np.arange(1, 5)
policies = ["FIFO", "LRU", "OPT", "UNOPT", "RAND", "CLOCK"]
# these were acheived after running `run.sh`
hit_rates = [
# FIFO
[45.03, 83.08, 93.53, 97.42],
# LRU
[45.03, 88.04, 95.20, 98.30],
# OPT
[45.03, 88.46, 96.35, 98.73],
# UNOPT
# NOTE: was unable to finish running this one, as it took too long.
[45.03, None, None, None],
# RAND
[45.03, 82.06, 93.16, 97.36],
# CLOCK
[45.03, 83.59, 94.09, 97.73],
]
for i in range(len(policies)):
plt.plot(cache_sizes, hit_rates[i])
plt.legend(policies)
plt.margins(0)
plt.xticks(cache_sizes, cache_sizes)
plt.xlabel("Cache Size (Blocks)")
plt.ylabel("Hit Rate")
plt.savefig("workload.png", dpi=227)
Best I can tell, this is related to the backend you're using and how it generates the FigureCanvas object. Matplotlib has introduced a deprecation and some other softwares are still in the process of being updated accordingly. Sadly, for now, it's generating a warning that is not very user friendly and the matplotlib documentation provides no help.
I was seeing this error in PyCharm, which by default uses its own internal backend for matplotlib: 'module://backend_interagg'. Google provides a bunch of examples of people getting errors to do with FigureCanvas and backend_interagg. Fortunately, updating to the newest version of PyCharm solved it for me.
If your problem is not PyCharm related, then perhaps try checking which backend you're using, and perhaps use a different one.
import matplotlib
matplotlib.get_backend()
will show you which backend you're using
matplotlib.use('')
will give you a list of available backends to try and then use will also allow you to select one. e.g. matplotlib.use('GTK3Agg')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With