I ran into a similar error and was able to fix it by installing an optional dependency. Specifically, in my situation, there's a 'bug' in distutil where loose version number comparisons can trigger an error in Python 3 because of implicit comparisons of string and integer types in distutils/version.py:343, which is called from Matplotlib's setup.py. See Issue 14894 for more details if you want them.
Since the optional dependency wasn't installed the version number check was returning a string ("Failed to identify version.") and of course that can't be compared to a numeric version, which threw the same exception you saw.
sudo apt-get install libfreetype6-dev
pip install matplotlib
installed libfreetype (an optional dependency), distutil's LooseVersion saw a version number and the comparison was typed correctly. Matplotlib installed fine thereafter.