Pyplot running but not displaying graphs

I downloaded python via anaconda like the class suggested. I want to make a graph so my code looks like this:

import matplotlib.pyplot as plt
import matplotlib.pyplot


x = [1,2,3,4,5,6,7,8,9,10]
y = [14,19,3,6,7,3,9,12, 1, 11]

plt.plot(x,y)
plt.show

print(matplotlib.__file__)
print(matplotlib.__version__)

I added the file and version so you can see that info here:

C:\Users\user1\anaconda3\lib\site-packages\matplotlib\__init__.py
3.3.4

However, no graphs disyplay. There are no errors, the code runs, just no graphs pop up. I read online it has something to do with some backend issues but that a bit over my head. It seems like people say using anaconda for this is not a good idea.

Thanks!

Anconda should be fine for this purpose. I wouldn’t worry about using it here, if anything it may be easier than sorting it yourself via pip.

I think there is an error here in that plt.show should be plt.show().

Edit: plt.show() is designed to prevent the script continuing (it blocks further execution) matplotlib.pyplot.show — Matplotlib 3.4.2 documentation in the normal “non-interactive mode”.

How are you running this code at the minute? By and large matplotlib is designed to run in an interactive session; when the python script finishes exectuion all active matplotlib windows could be closed again without your input. This would appear like the script ran successfully (no errors) but you don’t see any plots, or they flash up very quickly and disappear.


Edit: Being lazy with links the first time around; I should probably mention that an interactive shell is generally suggested for working with matploblib but matplotlib’s interactive mode is not the same thing.

It’s very likely you’ll want to work in an interactive shell when starting out (unless you’re doing some form of batch processing or periodic figure creation there’s few reasons not to), you might want to work in interactive mode.


Try running with python -i (interactive inspection after running script) just to double check the graph appears. This is just a quick test rather than an ideal way of working.

If it is not working then you may need to change the backend (some of these require additional downloads from pip or anaconda) https://matplotlib.org/stable/tutorials/introductory/usage.html#backends

Long term you may prefer working with an interactive kernel like IPython instead of the standard python interactive interpreter. If you’re doing this a lot then you may also prefer changing to use Jupyter’s interactive IDE or something similar which is almost always a much nicer way of working like this. Have a wee web search for it or check some of cc’s articles:
https://www.codecademy.com/articles/how-to-use-jupyter-notebooks-py3
https://www.codecademy.com/articles/getting-started-with-jupyter
https://www.codecademy.com/articles/getting-more-out-of-jupyter-notebook

1 Like

One question: why are you importing matplotlib.pyplot twice?

Once is sufficient:
import matplotlib.pyplot as plt

Also, the code works for me in Colab. But, as @tgrtim said, you should use plt.show() with the parens. (The code runs for me both with and w/o the parens.)

1 Like

A combination of what the other two said:

  • Remember that plt.show() is a method and should be entered as such, with the parenthesis. It won’t function correctly without them, and should throw an exception, although it appears that doesn’t happen.
  • The plot may not show correctly when you aren’t running the right setup. Either do what @tgrtim said and be sure to run Python in interactive mode, or better yet, when running something like matplotlib, use a Jupyter Notebook, which comes included in Anaconda and is the gold standard for data science work in python.
  • Whoever told you anaconda for this was a bad idea was wrong. Anaconda is very useful, and saves a lot of headaches (specifically, using anaconda avoids having to deal with the “backend” issues you want to not deal with anyway). It does have limits and problems, but for what you are using it for, you are fine, don’t worry about those concerns at the moment.

Sorry i wrote this quick to demonstrate from my actual project.

It does not display when including the ().
ie:

import matplotlib.pyplot as plt

x = [1,2,3,4,5,6,7,8,9,10]
y = [14,19,3,6,7,3,9,12, 1, 11]

plt.plot(x,y)
plt.show()

print(matplotlib.__file__)
print(matplotlib.__version__)

This will run but not display anything, and not even get the the print files location/version lines.
This link here I believe is probably the issue: Resolved: Matplotlib figures not showing up or displaying - PyImageSearch

But is running on a differentoperating system so I dont know if I can use his solution.

Thanks, I will look at the backend link he sent.

I mentioned your last point because this same exact code runs on a different computer I use at work in which I installed python without anaconda.

The issue was not the (), sorry that was just a typo.

My code is written in notepad++, executed, and displayed in the plugin console NppExec. This exact same code runs on a different computer I use at work. The only difference is at work i downloaded conda and not anaconda, as I did on this computer.

The project I am working on is large enough where I think I have to run in script mode.

Have you tried outside NppExec? Did you try python -i? Have you checked what backend is in use for matplotlib or tried changing it?

You could equally save the figure with plt.savefig("name.png") or something similar to check it executes correctly (is the display the problem or the execution). Try testing a few things, possibly outside NppExec for now until you can work out what step is causing trouble then you can use it freely.

Okay, Sorry I am a complete beginner. I am trying to uninstall and reinstall everything right now the same way I did at work

After I uninstalled and reinstalled I get this now when I run it:
C:\Users\effa1\anaconda3\lib\site-packages\numpy_init_.py:143: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see GitHub - IntelPython/mkl-service: Python hooks for Intel(R) Math Kernel Library runtime control settin
from . import distributor_init
Traceback (most recent call last):
File "C:\Users\effa1\anaconda3\lib\site-packages\numpy\core_init
.py", line 22, in
from . import multiarray
File “C:\Users\effa1\anaconda3\lib\site-packages\numpy\core\multiarray.py”, line 12, in
from . import overrides
File “C:\Users\effa1\anaconda3\lib\site-packages\numpy\core\overrides.py”, line 7, in
from numpy.core._multiarray_umath import (
ImportError: DLL load failed while importing _multiarray_umath: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “matplot lib test.py”, line 1, in
import matplotlib.pyplot as plt
File “C:\Users\effa1\anaconda3\lib\site-packages\matplotlib_init_.py”, line 107, in
from . import cbook, rcsetup
File “C:\Users\effa1\anaconda3\lib\site-packages\matplotlib\cbook_init_.py”, line 28, in
import numpy as np
File “C:\Users\effa1\anaconda3\lib\site-packages\numpy_init_.py”, line 145, in
from . import core
File “C:\Users\effa1\anaconda3\lib\site-packages\numpy\core_init_.py”, line 48, in
raise ImportError(msg)
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  • The Python version is: Python3.8 from “C:\Users\effa1\anaconda3\python.exe”
  • The NumPy version is: “1.20.1”

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: DLL load failed while importing _multiarray_umath: The specified module could not be found.

I’d suggest having a web search for that error and checking the given troubleshooting link and advice provided in the error itself.

You are using an outdated version of conda, you need a version which runs on Python 3.8 or later for that specific function you are trying to use. I think a big part of the problem is that somewhere along the way, your Python environment got damaged, and that can happen without you even doing anything (if you install a game or program that uses Python, some of them are made by people who don’t care much about their customers and they just override important environmental variables).

I tend to recommend the overkill solution for these problems, since it creates a clean slate to work on:

  • Remove all copies of Python from your system, including any conda/Anaconda/regular Python/Python extensions for your editor or IDE, etc.
  • Download a fresh copy of your interpreter. I generally recommend getting Python 3.8 or Python 3.9 directly from python.org, since you guarantee a clean install without conflicts.
  • Download your IDE/editor’s Python extension (I side with Codecademy and recommend using VSCode as your editor because it has a very good and well-maintained Python extension and its basically an upgrade over N++ or Sublime, but its your preference that matters).
  • ensure you work inside a virtual environment from this point! If you don’t know how to use venv or poetry, Codecademy has a tutorial as we all other websites. You don’t need to really understand all the mechanics of it, just the basic commands (specifically the command to create a virtual env, the command to activate it, and the command to close it). This ensures that whatever work you do will not pollute your other projects.
  • Inside your virtual env, pip install the necessary packages. Unless the package specifically says to avoid using pip (Poetry is an example), you should always use pip to install a package to your project.
  • Isolate the error code as much as possible and begin debugging from there.