Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Questions and answers begin here Logo Questions and answers begin here Logo
Sign InSign Up

Questions and answers begin here

Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Blog
  • Contact Us

ImportError: matplotlib requires numpy.

Home/ Questions/Q 389
Next
Answered
ImportError: matplotlib requires numpy.
hmngyang
hmngyang Teacher

I’ve been trying to import and use matplotlib to plot some simple data in my Python code, but I keep getting an error message whenever I try to `import matplotlib.pyplot as plt`. I’m really new to coding, so I don’t know what might be causing the issue. I suspect that the problem has to do with how I’m setting up my Python environment, but I don’t know how to fix it.
This is what my code looks like:

import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 30, 40]
plt.plot(x, y)
plt.show()

And this is what the error message looks like:

Traceback (most recent call last):
File "plot_data.py", line 1, in
import matplotlib.pyplot as plt
File "/Library/Python/2.7/site-packages/matplotlib/pyplot.py", line 29, in
import matplotlib.colorbar
File "/Library/Python/2.7/site-packages/matplotlib/colorbar.py", line 34, in
import matplotlib.contour as contour
File "/Library/Python/2.7/site-packages/matplotlib/contour.py", line 18, in
import matplotlib.text as text
File "/Library/Python/2.7/site-packages/matplotlib/text.py", line 28, in
from matplotlib.backend_bases import RendererBase
File "/Library/Python/2.7/site-packages/matplotlib/backend_bases.py", line 56, in
import matplotlib.textpath as textpath
File "/Library/Python/2.7/site-packages/matplotlib/textpath.py", line 22, in
from matplotlib.mathtext import MathTextParser
File "/Library/Python/2.7/site-packages/matplotlib/mathtext.py", line 28, in
import matplotlib._png as _png
ImportError: dlopen(/Library/Python/2.7/site-packages/matplotlib/_png.so, 2): Library not loaded: libpng16.16.dylib
Referenced from: /Library/Python/2.7/site-packages/matplotlib/_png.so
Reason: Incompatible library version: _png.so requires version 35.0.0 or later, but libpng16.16.dylib provides version 34.0.0

Can anybody help me understand what the problem might be, and how I can fix it? Thanks in advance!

debuggingImportErrormatplotlibpyplotpython
  • 127
  • 0 Followers
  • 1
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

3 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    ribalcomohr Teacher
    2019-07-27T06:27:49+00:00Added an answer about 4 years ago

    Hello there!
    From what I can gather, you are having trouble with importing matplotlib.pyplot as plt in your code. If that’s the case, I have some good news for you – this is a relatively common issue and there are a few ways you can go about resolving it.
    One common fix for this issue involves simply uninstalling and reinstalling Matplotlib. This can typically be done using pip, the package installer for Python. First, you’ll want to uninstall Matplotlib by running pip uninstall matplotlib in your terminal or command prompt. Once that’s done, you can reinstall the package by running pip install matplotlib. This may solve the issue, but if it doesn’t, there are a few other options to consider.
    Another thing to try is to check your environment variables to ensure that the Matplotlib library is properly installed and is on your system path. You can do this by navigating to the Environment Variables section in your system settings and adding the path to the Matplotlib library (which should be something like C:PythonLibsite-packagesmatplotlib) to your PATH variable.
    One more thing to keep in mind is that this error can sometimes be caused by installing a different version of Python after Matplotlib has already been installed. In this case, you may need to reinstall Matplotlib for the correct version of Python that you are using.
    I hope one of these solutions solves your issue with importing Matplotlib.pyplot as plt. If not, please let me know and we can explore further options. Happy coding!

    • 165
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. ryu.kanohara.ah Teacher
    2019-07-31T01:58:41+00:00Added an answer about 4 years ago

    You can try downgrading your version of Matplotlib to a previous version, as it could be a compatibility issue with your current version of Python. This has worked for some users experiencing similar issues with importing Matplotlib. However, it’s important to note that downgrading may cause other issues or bring incompatibilities with other dependencies. A better solution could be to try upgrading to the latest Matplotlib version or installing an earlier version of Python. Also, ensure that you have installed the relevant dependencies such as NumPy that Matplotlib requires to function correctly.

    • 34
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. darttk Teacher
    2019-07-31T09:18:46+00:00Added an answer about 4 years ago

    To resolve this issue, you can update the version of matplotlib by running this command in the command prompt: `pip install –upgrade matplotlib`. If the issue is still not resolved, try uninstalling the current version of matplotlib by running the command `pip uninstall matplotlib` and then reinstalling it using the command `pip install matplotlib`.

    Another possible issue could be a conflict with other installed packages. Try creating a virtual environment and installing only the necessary packages for your script. This can be achieved using virtualenv, which can be installed by running the command `pip install virtualenv`. Once installed, you can create a virtual environment using the command `virtualenv env_name`, where “env_name” is the name you want to give to your virtual environment. Then, activate the virtual environment using the command `source env_name/bin/activate` on Unix or `env_nameScriptsactivate` on Windows. Finally, install matplotlib using the command `pip install matplotlib` and run your script. This should resolve any package conflicts you may be facing.

    In my experience, package conflicts and outdated modules are common causes of import errors. Keeping your packages and modules up to date and resolving conflicts can go a long way in preventing such errors from occurring.

    • 14
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  4. david_benzie Teacher
    2019-07-31T12:59:28+00:00Added an answer about 4 years ago

    When facing an issue with importing matplotlib.pyplot as plt, it may be due to a missing module or a version incompatibility. One solution to try is to update the version of Matplotlib, which can be done by running the command “pip install –upgrade matplotlib” in the terminal. Another solution is to install the missing module, if there is one, by running the appropriate command.

    It’s also important to note that sometimes an issue like this can be caused by an error in the Python code itself. Double-checking the code to ensure that there are no syntax errors or missing dependencies can be helpful in resolving the problem.

    In my experience, I once faced a similar issue with importing Matplotlib, where it failed to import due to an outdated version. Upgrading the version fixed the problem, and I was able to continue with my project without issues.

    • 4
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  5. elzagha Begginer
    2019-07-29T14:27:07+00:00Added an answer about 4 years ago

    To solve the issue, you could try uninstalling and then reinstalling matplotlib. It’s possible that there may have been some issue with the installation or that the installation was incomplete.
    Another possible solution would be to check if there are any conflicting modules that might be causing the issue. You could check if there are any other Python installations on your system and see if they might be causing any conflicts. Additionally, you can check if there are any other modules installed that are using the name ‘pyplot’, and try renaming the conflicting module to something else. That might help resolve the issue. In general, it’s usually best practice to avoid using common module names to prevent naming conflicts.
    It’s also possible that there might be some issue with your Python environment or dependencies. You could try creating a new virtual environment and installing matplotlib there to see if that helps resolve the issue. Additionally, checking for updates to your Python packages and updating them might also help resolve the issue.

    • 2
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.