If you use multiple Python installation, use the following in your code. This fixed the error in my case
%pip install matplotlib